ACdream 1113 The Arrow (概率DP)

传送门

The Arrow

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
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









































题目大意:

就是你在位置 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值