[hihoCoder太阁最新面经算法竞赛8]题目2 : Dice Possibility(简单记忆化搜索)

本文介绍了一个简单的记忆化搜索算法来解决计算N个六面骰子投掷结果之和等于M的可能性百分比的问题。输入包括骰子数量N和目标和M,输出结果精确到小数点后两位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间限制: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
题解:简单记忆化搜索

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m;
double tmp;
double dp[101][610];
double dfs(int pos,int last) {
    if(dp[pos][last]!=-1) return dp[pos][last];
    int s=max(last-pos*6,1);
    int e=min(last-pos*1,6);
    double ans=0;
    for(int i=s;i<=e;i++) {
        double tmp_ans=dfs(pos-1,last-i);
        if(tmp_ans!=0) ans+=tmp_ans*tmp;
    }
    dp[pos][last]=ans;
    return dp[pos][last];
}
int main()
{
    cin>>n>>m;
    tmp=1.0/6;
    for(int i=0;i<n;i++) {
        for(int j=1;j<=600;j++) {
            dp[i][j]=-1;
        }
    }
    for(int i=1;i<=6;i++) {
        dp[0][i]=tmp;
    }
    dfs(n-1,m);
    if(dp[n-1][m]==-1) printf("0.00\n");
    else printf("%.2f\n",dp[n-1][m]*100);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值