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 <stdlib.h>

double p[ 1002 ][ 1002 ];
double q[ 1002 ];

int main()
{
    for ( int i = 0 ; i <= 1000 ; ++ i )
    for ( int j = 0 ; j <= 1000 ; ++ j )
        p[ i ][ j ] = 0.0;
    p[ 0 ][ 0 ] = 1.0;
    for ( int i = 0 ; i <= 1000 ; ++ i )
    for ( int j = 0 ; j <= 1000 ; ++ j ) {
        p[ i+1 ][ j ] += p[ i ][ j ]*0.5;
        p[ i ][ j+1 ] += p[ i ][ j ]*0.5;
    }

    for ( int i = 1 ; i <= 1000 ; ++ i ) {
        double sum = p[ i ][ 0 ]*i;
        for ( int j = i-1 ; j >= 1 ; -- j )
            sum += (i+j)*(p[ i ][ j ]-p[ i ][ j-1 ]*0.5);
        q[ i ] = sum*2.0;
    }

    int t,n;
    while ( scanf("%d",&t) != EOF )
    while ( t -- ) {
        scanf("%d",&n);
        printf("%.2lf\n",q[ n ]);
    }
    return 0;
}
时间: 2024-10-12 22:28:59

zoj 2949 - Coins of Luck的相关文章

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 g

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

ZOJ 3211 Dream City (J) DP

Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins

zoj 3627#模拟#枚举

Treasure Hunt II Time Limit: 2 Seconds                                     Memory Limit: 65536 KB There are n cities(1, 2, ... ,n) forming a line on the wonderland. city i and city i+1 are adjacent and their distance is 1. Each city has many gold coi

ZOJ 3793 First Digit

rt,直接猜1 First Digit Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Benford's Law, also called the First-Digit Law, refers to the frequency distribution of digits in many (but not all) real-life sources of data. In this distribut

[计算几何] zoj 3728 Collision

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3728 Collision Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge There's a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make