UVA10759 Dice Throwing【概率+DP】

UVA10759 Dice Throwing【概率+DP】,第1张

n common cubic dice are thrown. What is the probability that the sum of all thrown dice is at least x?
Input
The input file contains several test cases. Each test case consists two integers n (1 ≤ n ≤ 24) and x (0 ≤ x < 150). The meanings of n and x are given in the problem statement. Input is terminated by a case where n =0 and x =0. This case should not be processed.
Output
For each line of input produce one line of output giving the requested probability as a proper fraction in lowest terms in the format shown in the sample output. All numbers appearing in output are representable in unsigned 64-bit integers. The last line of input contains two zeros and it should not be processed.
Sample Input
3 9
1 7
24 24
15 76
24 56
24 143
23 81
7 38
0 0
Sample Output
20/27
0
1
11703055/78364164096
789532654692658645/789730223053602816
25/4738381338321616896
1/2
55/46656

问题链接:UVA10759 Dice Throwing
问题简述:计算n个骰子(六面体形状),投掷出总和为k时的概率。
问题分析:打表实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10759 Dice Throwing */

#include 

using namespace std;

typedef  long long LL;
const int N = 24;
const int X = 150;
LL dp[N + 1][X + 1];


void init()
{
    memset(dp, 0, sizeof dp);
    dp[0][0] = 1;
    for (int i = 1; i <= N; i++)
        for (int j = 0; j <= X; j++)
            if (dp[i - 1][j])
                for (int k = 1; k <= 6; k++)
                    dp[i][j + k] += dp[i - 1][j];
}

int main()
{
    init();

    int n, x;
    while (~scanf("%d%d", &n, &x) && (n || x)) {
        if (x <= n)
            printf("1\n");
        else {
            LL res1 = 0, res2 = pow(6, n);
            for (int i = x; i <= X; i++)
                res1 += dp[n][i];
            if (res1 == 0)
                printf("0\n");
            else {
                LL t = __gcd(res1, res2);
                printf("%lld/%lld\n", res1 / t, res2 / t);
            }
        }
    }

    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/875322.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存