码迷,mamicode.com
首页 > 其他好文 > 详细

ACdream 1113 The Arrow (概率dp求期望)

时间:2016-04-27 22:05:39      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

E - The Arrow
Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu
Submit Status

Description

The history shows that We need heroes in every dynasty. For example, Liangshan Heroes. People hope that these heroes can punish the bad  guys and recover the justice. Nowadays, we also need heroes to provent us from being chopped, or being attacked by a bomb. 

Kuangbin is a very very very very very.... (very * 1e9 ) good boy, after he knows The Arrow, he want to be The Arrow of China. But he is also a little afraid of being killed by the bad guys. So he decides to throw dices to make the decision.

The dice is a cube with 1 2 3 4 5 6 on it‘s sides. When he throws a dice, every number is of the same probablity to appear. He will write down a number N in the paper at first, and then throw the dice. When the sum of the number he throwed is less than N, he will keep throwing. But if the sum exceeds N, this throwing does not count.

For example, If the sum is 5,and N is 6, if we throw 2, 5 + 2 > 6, then  the sum keeps to be 5.

If he can end the throwing in a certain time, he will make the decision to become The Arrow.

Now , kuangbin wonders that what‘s the expectation of the time of throwing dices.

Input

First line, The number of cases t <= 100

In the next t lines, there will be t numbers.

every number is not bigger than 100000

Output

Each test output a number rounded to the second digit.

Sample Input

1
1

Sample Output

6.00

题意:

初始状态在0,每次丢骰子[1,6],如果现在在x,丢的骰子数为y,如果x+y>n那么就还停留在x。

求从0到n所丢骰子次数的期望。

分析:

如果现在在x,丢的骰子数为y,如果x+y>n那么就还停留在x。

那么我们设dp[i]表示从i到n要丢的骰子次数的期望

那么

我们设每次有x次的可能留在原地

dp[i] =dp[i]*y/6+dp[i+1]/6+dp[i+2]/6..+1;

然后化简一下得到dp[i]的公式。

对特殊的几个,例:

dp[n-3] = dp[n-3]/2 + dp[n-2]/6 + dp[n-1]/6 + dp[n]/6 + 1;

即在n-3这个位置,有1/2的概率不变, 1/6的概率加1, 1/6的概率加2,

1/6的概率加3, 相乘后得到平均期望 然后加上其投出的这一次 就是投出i的期望值

这个状态转移方程的最后结果即是 dp[0]。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int main()
{
    int i,n,t;
    double dp[101005];
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        cin>>n;
        dp[n-1]=6;
        dp[n-2]=6;
        dp[n-3]=6;
        dp[n-4]=6;
        dp[n-5]=6;
        dp[n-6]=6;
        for(int i=n-7;i>=0;i--)
        {
            for(int j=1;j<=6;j++)
            {
                dp[i]+=dp[i+j]/6;
            }
            dp[i]+=1;
        }
        printf("%.2lf\n",dp[0]);
    }
    return 0;
}

 

 

ACdream 1113 The Arrow (概率dp求期望)

标签:

原文地址:http://www.cnblogs.com/Ritchie/p/5440268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!