(期望dp) One Person Game

There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1, K2, K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have a counter, and the game is played as follow:
Set the counter to 0 at first.
Roll the 3 dice simultaneously. If the up-facing number of Die1 is a, the up-facing number of Die2 is b and the up-facing number of Die3 is c, set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
If the counter’s number is still not greater than n, go to step 2. Otherwise the game is ended.
Calculate the expectation of the number of times that you cast dice before the end of the game.

这些概率啥的期望啥的dp是真的难呀QAQ,借鉴:https://blog.csdn.net/winddreams/article/details/40537273
题意是我们有三个筛子,1-k1/k2/k2的数字,每个数字都是1/k1,1/k2,1/k3的可能性,我们有一个记分器,每次操作如果筛子1扔出a,筛子2扔出b,筛子3扔出c则将记分器变为0(初始为0),否则将积分器加上三个筛子的值,如果大于n则停止。求操作次数的期望
看kuangbin大佬的代码QAQ,dp[i]表示由i到大于n的操作期望,则枚举筛子的值j,有p[j]得可能性,则i数字变成大于n的期望就是dp[i + j] * p[j]由i + j到n的期望加上扔出a、b、c重置到0后由0到大于n的期望。dp[i] = ∑(p[j]*dp[i+j])+q*dp[0] + 1;
发现每次都可能扔出a、b、c回到0,而我们正要求从开始0到大于n的解,成为了一个环。大佬们引入了系数化解,高斯消元。。。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
    int t;
    cin >> t;
    while(t--) {
        int n, k1, k2, k3, a, b, c;
        cin >> n >> k1 >> k2 >> k3 >> a >> b >> c;
        vector<double> p(20), dpa(505), dpb(505);
        int sum = k1 + k2 + k3, cnt = 0;
        p[a + b + c] = -1.0; //抵消掉题目中回到0的那个情况
        for(int i = 1; i <= k1; ++i)
            for(int j = 1; j <= k2; ++j)
                for(int k = 1; k <= k3; ++k)
                    p[i + j + k] += 1.0, cnt++;
        for(int i = 3; i <= sum; ++i) p[i] /= cnt * 1.0;
        for(int i = n; i >= 0; --i) {
            dpa[i] = 1.0, dpb[i] = 1.0 / cnt;
            for(int j = 3; j <= k1 + k2 + k3; ++j) {
                dpa[i] += p[j] * dpa[i + j];
                dpb[i] += p[j] * dpb[i + j];
            }
        }
        cout << dpa[0] / (1 - dpb[0]) << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值