Light OJ 1064 - Throwing Dice
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1064
题意:一次掷n个筛子,求点数之和大于m的概率。
思路:f[i][j]表示前i个筛子点数之和为j的概率,由此DP即可。不过这道题不用每次都乘以六分之一,因为n个筛子最后的分母是6^n,所以最后除以6^n即可。
1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 #define ll unsigned long long 7 ll f[30][160],p[30]; 8 int n,m; 9 ll gcd(ll a,ll b){ 10 if (b==0) return a; 11 else return gcd(b,a%b); 12 } 13 int main(){ 14 int T; 15 scanf("%d",&T); 16 p[0]=1; 17 for (int i=1;i<=24;i++) p[i]=p[i-1]*6; 18 for (int i=1;i<=6;i++) f[1][i]=1; 19 for (int i=2;i<=24;i++) 20 for (int j=i-1;j<=(i-1)*6;j++) 21 for (int k=1;k<=6;k++) 22 f[i][j+k]+=f[i-1][j]; 23 for (int i=1;i<=24;i++) 24 for (int j=i*6;j>=0;j--) 25 f[i][j]+=f[i][j+1]; 26 for (int Tcase=1;Tcase<=T;Tcase++){ 27 scanf("%d%d",&n,&m); 28 ll ans1=f[n][m]; 29 ll ans2=p[n]; 30 ll t=gcd(ans1,ans2); 31 ans1/=t;ans2/=t; 32 if (ans2==1) 33 printf("Case %d: %lld\n",Tcase,ans1); 34 else 35 printf("Case %d: %lld/%lld\n",Tcase,ans1,ans2); 36 } 37 }