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 them either heads-up or heads-down with the same possibility. Their purpose is to gain as many coins heads-up as they can.

Input

The input has several test cases and the first line contains the integer t(1≤t≤1000) which is the total number of cases.

For each case, a line contains three space-separated integers n, m1≤n,m≤100) and k (1≤k≤n).

Output

For each test case, output the expected number of coins heads-up which you could have at the end under the optimal strategy, as a real number with the precision of 33 digits.

样例输入

6
2 1 1
2 3 1
5 4 3
6 2 3
6 100 1
6 100 2

样例输出

0.500
1.250
3.479
3.000
5.500
5.000

题目来源

ACM-ICPC 2017 Asia Urumqi

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7 #include <set>
 8 #include <map>
 9 #include <string>
10 #include <cmath>
11 #include <cstdlib>
12 #include <ctime>
13 using namespace std;
14 typedef long long ll;
15 int t,n,m,k;
16 const int N=110;
17 double dp[N][N];
18 double    c[N][N];//ll也会爆
19 double p[N];
20 double ans;
21 void C()
22 {
23     c[0][0]=1;
24     for(int i=1;i<=100;i++)
25     {
26         for(int j=0;j<=i;j++)
27         {
28             if(j==0||j==i) c[i][j]=1;
29             else {
30                 c[i][j]=c[i-1][j-1]+c[i-1][j];
31             }
32         }
33     }
34     p[0]=1;
35     for(int i=1;i<=100;i++) p[i]=p[i-1]/2;
36 }
37 int main()
38 {
39     scanf("%d",&t);
40     C();
41     /*
42     for(int i=1;i<=100;i++)
43     {   printf("%dllll\n",i);
44         for(int j=0;j<=i;j++)
45         {
46             printf("%d%c",c[i][j],j==i?‘\n‘:‘ ‘);
47         }
48         //c[33][j]就爆int 了。
49     }
50     */
51     while(t--)
52     {
53         scanf("%d%d%d",&n,&m,&k);
54         memset(dp,0,sizeof(dp));
55         //dp[i][j]: i次操作后,正面朝上的面数为j的概率
56         dp[0][0]=1;//初始都是反面
57         for(int i=0;i<m;i++)
58         {
59             for(int j=0;j<=n;j++)
60             {
61                for(int l=0;l<=k;l++)//取得k个里面l个正面朝上。
62                {   //反面的个数大于k,直接从反面的里面取k个
63                    if((n-j)>=k)  dp[i+1][j+l]+=dp[i][j]*c[k][l]*p[k];
64                    //反面的个数小于k,只能再从已经是正面的里面再取出k-(n-j)个。
65                    else  dp[i+1][j-(k-(n-j))+l]+=dp[i][j]*c[k][l]*p[k];
66                }
67             }
68         }
69         ans=0;
70         for(int i=1;i<=n;i++)ans+=i*dp[m][i];//期望
71         printf("%.3f\n",ans);
72     }
73     return  0;
74 }

原文地址:https://www.cnblogs.com/tingtin/p/9397610.html

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

ACM-ICPC 2017 Asia Urumqi A. Coins的相关文章

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 型. 代码:

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

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 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

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

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ