ACdream 113 The Arrow (概率DP)

传送门

The Arrow

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem

Problem 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

Source

wuyiqi

Manager

wuyiqi
Submit Statistic









































题目大意:

就是你在位置 0  处,你想到 n 位置,每次可以通过掷色子的方式来判断走几步,假设当前位置是 x 你置了是 y 点,那么如果 x+y > n 的话, 我们就待在原地不动,否则我们就走 y 步。让我们求得就是从 0 点走到 n 点掷色子的次数的期望


解题思路:

很明显这是一道概率的题,那么我们采用什么方式来做呢,应该是DP了,定义 DP [ i ] 表示的是:从 i 点 到 n 点的置的色子的期望,那么DP [ i ] 等于什么呢,要是我们把 这个状态转移方程写出来就行了。DP[ i ] = DP[ i+1 ]/6.0 + DP[ i+2 ] / 6.0 +... DP[ i+6 ] /6.0 + 1 + DP[ i ]*x/6(x 表示假设 有 x 次i+j > n的次数)如果写程序的话,我们只需要两层循环,外层循环就是 从 n-1 循环到 0 ,内层循环就是 从 1 循环到 6 ,if( i+j > n) 的话用一个数记录一下,最后就是 求 DP[n],还得移项,求一下 DP[ n ]就行了。


My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1e5+5;
double dp[MAXN];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        memset(dp, 0, sizeof(dp));
        for(int i=n-1; i>=0; i--)///注意从n-1开始
        {
            int cnt = 0;///i+j>n的次数也就是 原地不动的次数
            double sum = 0;
            for(int j=1; j<=6; j++)
            {
                if(i+j > n)
                    cnt++;
                else
                    dp[i] += dp[i+j]/6;
            }
            dp[i] = (dp[i]+1)/(6-cnt)*6;///移项求的
        }
        printf("%.2lf\n",dp[0]);
    }
    return 0;
}


你可能感兴趣的:(ACdream 113 The Arrow (概率DP))