小A买彩票
暴力枚举
枚举四种彩票的数量, 统计不亏本情况即可
(1) 总情况: 有 4^n = 2^(2n) = 1<<(2n) 种
(2) 数量分别为a,b,c,d时:
不亏本条件: -2a-b+0c+d>=0, d>=2a+b
该情况数量: 根据排列组合原理有 n! / (a! * b! * c! * d!)种, 累加求和
最后gcd约分输出即可
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger[] A = new BigInteger[31];// 阶乘
static {
A[0] = BigInteger.ONE;
for (int i = 1; i <= 30; i++) {
A[i] = A[i - 1].multiply(BigInteger.valueOf(i));
}
}
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
long cnt = 0;// 枚举个数并统计不亏本情况数
for (int a = 0; a <= n; a++) {
for (int b = 0; b <= n - a; b++) {
for (int c = 0; c <= n - a - b; c++) {
int d = n - a - b - c;
if (d >= 2 * a + b) {
cnt += A[n].divide(A[a]).divide(A[b]).divide(A[c]).divide(A[d]).longValue();
}
}
}
}
// 约分输出
long f = 1L << (2 * n);
long gcd = gcd(f, cnt);
System.out.println(cnt / gcd + "/" + f / gcd);
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
}