C - Throwing Dice
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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,第一行是案例数 T ,然后 n , m 是n个骰子掷出至少为 m 的概率。
dp[i][j]代表 i 个骰子,掷出 j 种数
dp[i+1][j+k]=sigma(dp[i][j]) (1<=k<=6)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 using namespace std; 5 6 __int64 dp[30][155]; 7 8 __int64 gcd(__int64 x,__int64 y) 9 { 10 return y?gcd(y,x%y):x; 11 } 12 13 int main() 14 { 15 __int64 up,down,g; 16 int T,n,x,i,j,k; 17 18 for(i=1;i<=6;i++)//一个骰子 19 dp[1][i]=1; 20 for(i=1;i<25;i++)//骰子 21 { 22 for (j=i;j<=6*i;j++)//这个骰子所有的点数 23 { 24 for (k=1;k<=6;k++)//下个骰子的点数 25 dp[i+1][j+k]+=dp[i][j]; 26 } 27 } 28 29 scanf("%d",&T); 30 for(int cas=1;cas<=T;cas++) 31 { 32 scanf("%d%d",&n,&x); 33 if(x>n*6) 34 { 35 printf("Case %d: 0\n",cas); 36 continue; 37 } 38 if(x<=n) 39 { 40 printf("Case %d: 1\n",cas); 41 continue; 42 } 43 up=0,down=1; 44 for(i=x;i<=n*6;i++) 45 up+=dp[n][i]; 46 for(j=0;j<n;j++) down*=6;//所有骰子可能的情况 47 g=gcd(up,down); 48 printf("Case %d: %I64d/%I64d\n",cas,up/g,down/g); 49 } 50 return 0; 51 }