What is possibility of rolling N dice and the sum of the numbers equals to M?
Input
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
Output
Output the possibility in percentage with 2 decimal places.
Sample Input
2 10
Sample Output
8.33
思路:dp[i][j]表示前i个筛子得到的和为j的概率
dp[i][j]+=dp[i-1][k]*1.0/6;
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000") //加栈
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
#define inf 0x3f3f3f3f
double dp[200][1000];
int n,m;
int main(int argc, char const *argv[])
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=6;i++)
{
dp[1][i]=1.0/6;
}
for(int i=2;i<=n;i++)
{
for(int j=i;j<=m;j++)
{
for(int k=1;k<=6;k++)
{
if(j-k>0)
{
dp[i][j]+=1.0*dp[i-1][j-k]/6;
}
}
}
}
printf("%.2lf\n",100*dp[n][m]);
}
return 0;
}