Throwing Dice
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
Output for Sample Input
7
3 9
1 7
24 24
15 76
24 143
23 81
7 38
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
PROBLEM SETTER: SHAHRIAR MANZOOR
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)
简单的dp预处理~~~~
#include<cstdio>
long long dp[26][150],pa[26];
void init()
{
int i,j,k;
pa[0] = 1;
for(i = 1 ; i < 25 ; i++)
pa[i] = pa[i - 1] * 6; // 预处理投掷 i 次的所有可能
for(i = 1 ; i <= 6 ; i++)
dp[1][i] = 1;
for(i = 2 ;i < 25 ; i++)
for(j = i - 1 ; j <= (i - 1) * 6 ; j++)
for(k = 1 ; k <= 6 ; k++)
dp[i][j + k] += dp[i - 1][j]; // 前 i 次投掷结果为 j + k 的所有可能
for(i = 1 ; i <= 24 ; i++)
for(j = i * 6 ; j >= 0 ; j--)
dp[i][j] += dp[i][j + 1]; // 前 i 次投掷结果大于等于 j 的所有可能
}
long long GCD(long long a,long long b){
return !b ? a : GCD(b,a % b);
}
int main()
{
int T,nl = 0,N,M;
long long ans,sum,cut;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
ans = pa[N];
sum = dp[N][M];
cut = GCD(sum,ans);
ans /= cut;
sum /= cut;
if(ans == 1) // 如果分母为 1
printf("Case %d: %lld\n",++nl,sum);
else
printf("Case %d: %lld/%lld\n",++nl,sum,ans);
}
return 0;
}