zoukankan      html  css  js  c++  java
  • (ZOJ 3329) One Person Game (概率DP)

    There is a very simple and interesting one-person game. You have 3 dice, namely Die1Die2 and Die3Die1 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 K1K2K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have a counter, and the game is played as follow:

    1. Set the counter to 0 at first.
    2. 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.
    3. 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.

    Input

    There are multiple test cases. The first line of input is an integer T (0 < T <= 300) indicating the number of test cases. Then T test cases follow. Each test case is a line contains 7 non-negative integers nK1K2K3abc (0 <= n <= 500, 1 < K1K2K3 <= 6, 1 <= a <= K1, 1 <= b <= K2, 1 <= c <= K3).

    Output

    For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.

    Sample Input

    2
    0 2 2 2 1 1 1
    0 6 6 6 1 1 1
    

    Sample Output

    1.142857142857143
    1.004651162790698
    

     同样的扔色子问题,dp[i]代表到达i分数时距离游戏结束还可以扔的期望值,同样是倒推。

    由于每个状态有概率能转化到初始状态,所以把每个状态的dp值用多项式来表达,即dp【i】=a*dp【0】+b;

    从而将dp数组变成二维的。1位存常数,2位存dp【0】前的系数;

    最后dp【0】=a*dp【0】+b;解出dp【0】即可;

    一次过,爽~

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<set>
    #include<map>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define LL long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    
    using namespace std;
    
    double num[100];
    double dp[600][3];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n;
            int k1,k2,k3,a,b,c;
            scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
            memset(num,0,sizeof(num));
            memset(dp,0,sizeof(dp));
            for(int i=1;i<=k1;i++)
                for(int j=1;j<=k2;j++)
                    for(int k=1;k<=k3;k++)
                    {
                        if(i==a&&j==b&&k==c)
                            continue;
                        num[i+j+k]++;
                    }
            int p=k1+k2+k3;
            double q=k1*k2*k3;
            for(int i=3;i<=p;i++)
            {
                num[i]/=q;
            }
            for(int i=n+p;i>=0;i--)
            {
                if(i>n)
                {
                    dp[i][1]=0;
                    dp[i][2]=0;
                }
                else
                {
                    for(int j=3;j<=p;j++)
                    {
                        dp[i][1]+=num[j]*dp[i+j][1];
                        dp[i][2]+=num[j]*dp[i+j][2];
                    }
                    dp[i][1]++;
                    dp[i][2]+=1/q;
                }
            }
            printf("%.15lf
    ",dp[0][1]/(1.0-dp[0][2]));
        }
        return 0;
    }
    此地非逐弃者之王座,彼方乃行愿者之归所。无限清澈,星界银波。
  • 相关阅读:
    怎么样通过API函数获取tooltip的内容(请高手帮忙)
    Ajax控件之Accordion控件学习
    C#中结构与类的区别
    C# ref and out 区别
    java 将PDF文件的首页提取为图片
    围住浮动元素(消除浮动)的三种方法
    myeclipse unable to install breakpoint
    C#调用C++编写的COM DLL
    高手教你玩53kf在线客服的指定客服功能
    让IIS支持Flv的详细设置方法
  • 原文地址:https://www.cnblogs.com/brotherHaiNo1/p/8458750.html
Copyright © 2011-2022 走看看