题意:有n个骰子,给出一个目标值x,求出得到的所有骰子点数之和大于x的概率。
题解:分母肯定是6^n,分子需要dp得到,开一个二维数组f[i][j]表示i个骰子组成和大于等于j的情况有多少种。状态转移方程是f[i][j + k] += f[i - 1][j],然后两个值求最大公约数约分一下就可以了。
#include <stdio.h>
#include <string.h>
#include <math.h>
#define ll long long
int n, sum;
ll f[25][160];
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
void init() {
memset(f, 0, sizeof(f));
f[0][0] = 1;
for (int i = 1; i <= 24; i++)
for (int j = 0; j <= 150; j++)
if (f[i - 1][j])
for (int k = 1; k <= 6; k++)
f[i][j + k] += f[i - 1][j];
}
int main() {
init();
while (scanf("%d%d", &n, &sum) && n + sum) {
if (sum <= n) {
printf("1\n");
continue;
}
ll res1 = 0, res2 = pow(6, n);
for (int i = sum; i <= 150; i++)
res1 += f[n][i];
if (res1 == 0) {
printf("0\n");
continue;
}
long long temp = gcd(res1, res2);
printf("%lld/%lld\n", res1 / temp, res2 / temp);
}
return 0;
}