ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】

题目链接:https://www.jisuanke.com/contest/2870?view=challenges

题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m次后,硬币向上的期望值。

思路:

1.期望跟概率还是有点不同的,期望要枚举出抛的所有的情况,然后求sigma(i * dp[][])

2.dp[i][j]表示进行i次操作后,有j枚硬币向上的概率。这样就可以求最后的硬币向上的期望了。

3.值得注意的是,预处理的组合数要开 double 型。

代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define mem(a, b) memset(a, b, sizeof(a))
 4
 5 double C[110][110];//组合数
 6 double P[110]; //翻i个硬币的概率,因为正反都是 1 / 2,所以用一维数组表示
 7 double dp[110][110]; //表示操作i次,有j枚硬币正面向上的概率
 8 int n, m, k;
 9
10 int main()
11 {
12     //预处理组合数
13     C[0][0] = 1;
14     for(int i = 1; i <= 100; i ++)
15     {
16         C[i][0] =  1;
17         for(int j = 1; j <= i; j ++)
18         {
19             C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
20         }
21     }
22     //预处理i个硬币的概率
23     P[0] = 1.0;
24     for(int i = 1; i <= 100; i ++)
25         P[i] = 0.5 * P[i - 1];
26     int T;
27     scanf("%d", &T);
28     while(T --)
29     {
30         mem(dp, 0);
31         dp[0][0] = 1.0;
32         scanf("%d%d%d", &n, &m, &k);
33         for(int i = 0; i < m; i ++)//枚举操作次数
34         {
35             for(int j = 0; j <= n; j ++)//枚举硬币正面向上的个数
36             {
37                 if(dp[i][j] == 0)
38                     continue;
39                 for(int q = 0; q <= k; q ++)//枚举抛k枚硬币有多少枚硬币会朝上,枚举所有情况,才是求期望
40                 {
41                     if((n - j) >= k)
42                         dp[i + 1][j + q] += dp[i][j] * C[k][q] * P[k];
43                     else
44                         dp[i + 1][j + q - (k - (n - j))] += dp[i][j] * C[k][q] * P[k];
45                 }
46             }
47         }
48         double ans = 0.0;
49         for(int i = 0; i <= n; i ++)
50         {
51             ans += dp[m][i] * i;
52         }
53         printf("%.3lf\n", ans);
54     }
55     return 0;
56 }

原文地址:https://www.cnblogs.com/yuanweidao/p/10992250.html

时间: 2024-08-05 03:54:24

ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】的相关文章

ACM-ICPC 2017 Asia Urumqi A. Coins

Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any k of the coins and toss them into the air, replacing each of th

2017 ICPC Asia Urumqi A.coins (概率DP + 期望)

题目链接:Coins Description Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the

ACM-ICPC 2017 Asia Urumqi:A. Coins(DP) 组合数学

Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the air, replacing each of

ACM-ICPC 2017 Asia Urumqi(第八场)

A. Coins Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mmm times they select any kkk of the coins and toss them into the air, replac

UPC5431/acm icpc 2017 Tehran Column Addition

题目链接:http://exam.upc.edu.cn/problem.php?cid=1326&pid=7 题意:给你一个可能存在错误的加法等式,问最少删除多少列能使等式成立. eg: 思考:如果某一列已经成立,如上图的1+4=5,他一定可以加到前面最长的成立的等式上,类似于最长上升子序列,不过要n^2扫. 做题时WA了几发,因为没考虑到等式的最高位不能有进位,以及可能某一列一开始没有进位,但是后来由于前面低位的进位导致了自己的进位(这种情况只会出现在初始和为9,低位又进一的情况). 1 #i

[六省联考2017]分手是祝愿 题解(期望dp)

题目描述 B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为从 1 到 n 的正整数. 每个灯有两个状态亮和灭,我们用 1 来表示这个灯是亮的,用 0 表示这个灯是灭的,游戏的目标是使所有灯都灭掉. 但是当操作第 i 个开关时,所有编号为 i 的约数(包括 1 和 i)的灯的状态都会被改变,即从亮变成灭,或者是从灭变成亮. B 君发现这个游戏很难,于是想到了这样的一个策略,每次等概率随机操作一个开关,直到所有灯都灭掉. 这个策略需要的操作次数很多,B

ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在于状态转移方程的确立和SPFA的过程 1 //最短路:Floyd+SPFA(BFS)+DP 2 //Time:20Ms Memory:336K 3 //题目很好,数据较弱,网上部分代码有些问题却能够A掉 4 //题意:超级马里奥要从A+B处背着公主以最短路程到达1处,其中1-A是村庄,剩下的是城堡

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou