时间限制: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[],b[];
int n,m;
void getdp()
{
for(int i=;i<=;i++) a[i]=;
for(int i=;i<=n;i++){
for(int j=m;j>=;j--)
for(int k=;k<=;k++)
if(j-k>) b[j]+=a[j-k];
for(int j=;j<=m;j++){
a[j]=b[j];
b[j]=;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
getdp();
ans=1.0*a[m];
for(int i=;i<=n;i++){
ans/=6.0;
}
ans*=;
printf("%.2lf\n",ans);
return ;
}
常规DP写法:
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
using namespace std;
double d[][];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
memset(d,,sizeof d);
d[][]=;
for(int i=;i<=n;i++)
{
for(int k=;k<=;k++)
{
for(int j=m;j>=k;j--)d[i][j]+=d[i-][j-k]/;
}
}
printf("%0.2lf\n",d[n][m]*);
return ;
}