时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
What is possibility of rolling N dice and the sum of the numbers equals to M?
输入
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
输出
Output the possibility in percentage with 2 decimal places.
- 样例输入
-
2 10
- 样例输出
-
8.33
母函数写法:
#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; double ans; double a[1010],b[1010]; int n,m; void getdp() { for(int i=1;i<=6;i++) a[i]=1; for(int i=2;i<=n;i++){ for(int j=m;j>=1;j--) for(int k=1;k<=6;k++) if(j-k>0) b[j]+=a[j-k]; for(int j=1;j<=m;j++){ a[j]=b[j]; b[j]=0; } } } int main() { scanf("%d%d",&n,&m); getdp(); ans=1.0*a[m]; for(int i=1;i<=n;i++){ ans/=6.0; } ans*=100; printf("%.2lf\n",ans); return 0; }
常规DP写法:
#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; using namespace std; double d[101][601]; int main() { int n,m; scanf("%d%d",&n,&m); memset(d,0,sizeof d); d[0][0]=1; for(int i=1;i<=n;i++) { for(int k=1;k<=6;k++) { for(int j=m;j>=k;j--)d[i][j]+=d[i-1][j-k]/6; } } printf("%0.2lf\n",d[n][m]*100); return 0; }
时间: 2024-11-05 12:29:24