hihocoder-Week174-Dice Possibility

hihocoder-Week174-Dice Possibility

Dice Possibility

时间限制: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

明显的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
");
			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
", ans ); 
	}
	return 0; 
}

  

原文地址:https://www.cnblogs.com/zhang-yd/p/7755873.html