栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

UVA10759 Dice Throwing【概率+DP】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UVA10759 Dice Throwing【概率+DP】

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++语言程序如下:


#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("1n");
        else {
            LL res1 = 0, res2 = pow(6, n);
            for (int i = x; i <= X; i++)
                res1 += dp[n][i];
            if (res1 == 0)
                printf("0n");
            else {
                LL t = __gcd(res1, res2);
                printf("%lld/%lldn", res1 / t, res2 / t);
            }
        }
    }

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/867440.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号