UVA - 10759 Dice Throwing

题目大意:给出n和x,计算在丢n个色子,出现的点数大于等于x的概率,要求分式最简。


解题思路:num[i][j] 与num[i][j + 1]中间的增长个数与用i个色子丢出的点数为j的情况有关,然而求用i个色子丢出点数的情况则是非常好求的:num[i][j]  = ∑(j + 1 ≤ k ≤ j + 6)num[i][k].

然后就可以进一步的去求丢出i个色子丢出的点数小于x的情况。


#include <cstdio>
#include <cstring>
long long dp[160][30];

long long dfs(long long m, int c) {
	if (dp[m][c] != -1) 
		return dp[m][c];
	dp[m][c] = 0;
	for (int i =1 ; i <= 6; i++)
		if (m - i > 0)
			dp[m][c] += dfs(m - i, c + 1);
	return dp[m][c];
}

long long gcd(long long a, long long b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	long long n, m, nu, tmp;
	while (scanf("%lld%lld", &n, &m), n) {
		long long de = 1;
		for (int i = 1; i <= n; i++)
			de *= 6;
		memset(dp, -1, sizeof(dp));
		for (int i = 0; i <= m; i++)
			dp[i][n] = 1;

		nu = de - dfs(m, 0);
		tmp = gcd(de, nu);
		if (nu == 0)
			printf("0\n");
		else if(nu == de)
			printf("1\n");
		else
			printf("%lld/%lld\n", nu / tmp, de / tmp);
	}
	return 0;
}


你可能感兴趣的:(UVA - 10759 Dice Throwing)