标签:nta ble bre ane string strong als main lin
hihocoder-Week174-Dice Possibility
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
明显的DP问题
DP公式为: dp[i][j] = ( dp[i-1][j-1] + dp[i-1][j-2] + dp[i-1][j-3] + dp[i-1][j-4] + dp[i-1][j-5] + dp[i-1][j-6] ) / 6;
其中 dp[i][j] 表示有i个dice时,取得j点数的概率。
#include <cstdio> #include <cstring> #include <cstdlib> const int MAXN = 600 + 10; double dp[MAXN][MAXN]; int main(){ int n, m; while(scanf("%d %d", &n, &m) != EOF){ if(n > m || m > 6*n){ printf("0.00\n"); continue; } for(int i=0; i<=n; ++i){ for(int j=0; j<=m; ++j){ dp[i][j] = 0.0; } } for(int i=1; i<=6; ++i){ dp[1][i] = 1.0 / 6.0; } for(int i=2; i<=n; ++i){ for(int j=1; j<=m; ++j){ for(int k=1; k<=6; ++k){ if(j-k <= 0){ break; } dp[i][j] += dp[i-1][j-k] / 6.0; } } } double ans = dp[n][m] * 100; printf("%.2lf\n", ans ); } return 0; }
hihocoder-Week174-Dice Possibility
标签:nta ble bre ane string strong als main lin
原文地址:http://www.cnblogs.com/zhang-yd/p/7755873.html