ZOJ 2949 Coins of Luck(概率dp求期望)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1948

Luck is the key to life. When I have to decide something, I will make my decision by flipping a coin. And then, I have two things to do. First, I have the decision made. Second, I go
to the nearest church and donate the coin.

But there are always too many things I have to decide. For example, what should I eat today for lunch? OK, now we are talking about a decision, so let us assume that I have two kinds
of food: quick-served noodle A and quick-served noodle B.

I just have bought N bowls of noodle A and N bowls of noodle B. If I have some space to make a decision (i.e. having two kinds of quick-served noodle to choose from),
then I will flip a coin to decide which kind of noodle to eat.

My question is, before I eat up all 2 * N bowls of quick-served noodle, what is the mathematical expectation of the number of coins I will have to donate.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1000) which is the number of test cases. And it will be
followed by T consecutive test cases.

Each test case contains an integer N (1 <= N <= 1000).

Output

Results should be directed to standard output. The output of each test case should be a single real number, which is the mathematical expectation of the number of coins I have to donate.
The result should be accurate to two digits after the fractional point.

Sample Input

2
1
2

Sample Output

1.00
2.50

Author: CHEN, Zhengguang

Source: Zhejiang University Local Contest 2008

题意:

有一个人要吃A和B两种面,分别n碗;每一次用抛硬币的方式决定吃哪一种面!

求吃完面需要跑硬币的次数的期望!

PS:

dp[i][j]:表示吃了A种面 i 碗,B种面j碗的期望!

dp[i][j] = dp[i-1][j] * 0.5 + dp[i][j-1]*0.5 + 1; 因为从dp[i][j] 变为dp[i-1][j] 或者dp[i][j-1]还需要跑一次硬币!

代码如下:

#include <cstdio>
#include <cstring>
double dp[1017][1017];
void init()
{
    dp[0][0] = 0;
    for(int i = 0; i <= 1000; i++)
    {
        dp[i][0] = 0;
        dp[0][i] = 0;
    }
    for(int i = 1; i <= 1000; i++)
    {
        for(int j = 1; j <= 1000; j++)
        {
            dp[i][j] = dp[i-1][j]*0.5 + dp[i][j-1]*0.5+1;
        }
    }
}
int main()
{
    int t;
    init();
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        printf("%.2lf\n",dp[n][n]);
    }
    return 0;
}
时间: 2024-08-01 22:43:01

ZOJ 2949 Coins of Luck(概率dp求期望)的相关文章

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

HDU4336-Card Collector(概率DP求期望)

Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2195    Accepted Submission(s): 1034 Special Judge Problem Description In your childhood, do you crazy for collecting the beautifu

HDU3853-LOOPS(概率DP求期望)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1864    Accepted Submission(s): 732 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help h

HDU4405-Aeroplane chess(概率DP求期望)

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1182    Accepted Submission(s): 802 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

HDU 4050 wolf5x (概率DP 求期望)

题意:有N个格子,1~N,起点在0,每个格子有一个状态(0,1,2,3),每次可以跨[a,b]步, 问走完N个格子需要步数的期望,每次尽量走小的步数,即尽量走a步,不能则走a+1,-- 状态0意味着你不能踏进对应的网格. 状态1意味着你可以??步入网格用你的左腿. 状态2意味着你可以??步入网格用你的右腿. 状态3意味着你可以进入网格用任何你的腿,而接下来的步骤中,您可以使用任何的腿;即你不需要遵循上述规则. 思路:借鉴了各路大神的思想理解了下. dp[i][j] :表示走到第 i 个格子在 j

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno

zoj 2949 - Coins of Luck

题目:有2中面条各n碗,每次抛硬币判断吃哪一种(到一种吃完为止),问抛硬币的数学期望. 分析:动态规划,概率dp.求出每种结束状态(即,有一种吃完)的概率,分别乘以步长即为期望. 大黄解法:状态位剩余的碗数,逆向求解,状态方程: DP[ i ][ j ] = (DP[ i-1 ][ j ]+DP[ i ][ j-1 ])/2 + 1 { orz~~ (全部20行代码就能搞定) }. 说明:(2011-09-27 03:22). #include <stdio.h> #include <s

zoj 3822 Domination 【概率DP 求期望】

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

概率DP——求期望

一般求期望类题目都是倒着来做的,dp[i]表示i状态下要达到要求状态的期望值,于是dp[0]就是我们要找的答案,我们可以通过此来推状态转移方程,可以得到dp[i]=Σ(dp[k>i]*p[k])+ 1,由于由k状态转移到i状态会多一步操作,这多的一步乘以每种概率再求和刚好等于1,所以转移公式后面有一个加一. POJ 2096题目链接:http://poj.org/problem?id=2096 一个软件有s个子系统,会产生n种bug某人一天发现一个bug,这个bug属于一个子系统,属于一个分类每