Throwing Dice
Description
n common cubic dice are thrown. What is the probability that the sum of all thrown dice is at least x?
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each test case contains two integers n (1 ≤ n < 25) and x (0 ≤ x < 150). The meanings of n and x are given in the problem statement.
Output
For each case, output the case number and the probability in 'p/q' form where p and q are relatively prime. If q equals 1 then print p only.
Sample Input
7
3 9
1 7
24 24
15 76
24 143
23 81
7 38
Sample Output
Case 1: 20/27
Case 2: 0
Case 3: 1
Case 4: 11703055/78364164096
Case 5: 25/4738381338321616896
Case 6: 1/2
Case 7: 55/46656
解题思路:
一个简单的dp计数问题 。
如果用dp[i][j]表示在投掷i次之后点数和为j的方案数,那么dp[i][j] = ∑dp[i - 1][j - k] (1 <= k <= min(6, j)) 。
初始dp[0][0]这样得到所有的投掷n次点数为m的方案数,直接求和即可得到满足题意的方案, 约分一下即可 。
由于涉及到大数,所以用Java比较方便。
AC代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String args[]){
int T, n, x;
BigInteger dp[][] = new BigInteger[30][200];
for(int i = 1; i <= 150; i++)
dp[0][i] = BigInteger.ZERO;
dp[0][0] = BigInteger.ONE;
for(int i = 1; i <= 25; i++){
for(int j = 0; j < i; j++)
dp[i][j] = BigInteger.ZERO;
for(int j = i; j <= 6*i; j++){
dp[i][j] = BigInteger.ZERO;
for(int k = 1; k <= 6; k++)
if(j >= k)
dp[i][j] = dp[i][j].add(dp[i - 1][j - k]);
}
for(int j = 6*i + 1; j <= 150; j++)
dp[i][j] = BigInteger.ZERO;
}
Scanner sca = new Scanner(System.in);
T = sca.nextInt();
for(int cas = 1; cas <= T; cas++){
n = sca.nextInt();
x = sca.nextInt();
BigInteger dem = BigInteger.valueOf(6).pow(n);
BigInteger num = BigInteger.ZERO;
for(int i = x; i <= 6*n; i++)
num = num.add(dp[n][i]);
BigInteger gcd = dem.gcd(num);
dem = dem.divide(gcd);
num = num.divide(gcd);
if(dem.compareTo(BigInteger.ONE) != 0)
System.out.println("Case " + cas + ": " + num + "/" + dem);
else
System.out.println("Case " + cas + ": " + num);
}
}
}