Description

Bootstrap: Wondering how it's played? Will: It's a game of deception. But your bet includes all the dice, not just your own. What are they wagering? Bootstrap: Oh, the only thing we have. Years of service. Will: So any crew member can be challenged? Bootstrap: Aye. Anyone. Will: I challenge Davy Jones.
All that the pirates have on the Flying Dutchman is the years of service that are left for them. Every crewman wants to shorten it. That is why gambling is very popular on the ship, the winner have a chance to shorten his years of service significantly.
Pirates often gather to play “Roshambo”, also known as “rock-scissors-paper”. The game consists of several sets. In the beginning of each set players stand in a circle, count to three and show one of three gestures simultaneously, conventionally called as rock, scissors and paper. If everyone shows the same gesture or if each of the three gestures is shown, then nobody leave the game and they play another set. If among the shown gestures there are only two different then only players that chose the victorious gesture play the next set. Scissors beats rock, rock beats paper and paper beats scissors. The game continues until the only one player is left, and that pirate is called the winner. The winner’s time of service is shortened on the number of years that equals the number of the sets played, while the losers get extra years.
Bootstrap Bill decided to try his fortune. You should help him determine the expected value of prize in case of his victory. Pirates don’t know any complicated strategies for this game. So you can suppose that pirates show every gesture equiprobably.

Input

The only line contains integer n that is the number of sailors that are going to play, including Bill (2 ≤ n ≤ 100).

Output

Output the expected amount of years that will be taken off from winner. Absolute or relative error should be no more than 10−6.

题目大意:n个人玩石头剪刀布(一起上的),每一round若平局则继续,否则输的出局,赢的留下来,进入下一round。直到剩下一个人,问期望进行多少round。

思路:递推。令dp[n]代表n个人的期望结束盘数(也就是我们要的答案)。

令ret代表n个人,这一场不平手的结束盘数。枚举赢的人数 i ,那么赢的人数为 i 的概率 p 为从 n 个人里面选 i 个人赢,其中这 i 个人的选择有3种,这 i 个人都出前面选的那一种,概率为 (1/3)^i ,剩下的n-i个人,都选被前面选的那种打败的一种,概率为 (1/3)^(n-i)。然后赢了之后,就是剩下 i 个人,也就是 i 个人结束游戏的期望盘数dp[i] + 1,符合递推条件。

令q为平局的概率,这个很容易求,q = 1 - sum{p},p就是前面的所有非平局的概率。

ret = sum{(dp[i]+1) * c[n][i] * 3 * (1/3)^i * (1/3)^(n-i)} = sum{(dp[i]+1) * c[n][i] * (1/3)^(i-1)}

那么dp[n] = (1-q)*ret + q*(1-q)*(1+ret) + q^2*(1-q)*(2+ret) + q^3*(1-q)*(3+ret) + ……

//提取出1-q

=(1-q)(ret + (1+ret)*q + (2+ret)*q^2 + (3+ret)*q^3 + ……)

//把ret提出来,分别求极限

=(1-q)(ret/(1-q) + q/(1-q)^2)

=ret + q/(1-q)

然后一切就好办了。

PS:代码被我各种合并已经跟前面讲的大不一样了……

PS2:前面写得好乱不知道有没有写错的啊……

PS3:代码中的q是上面的1-q,也就是sum{p},因为不改成这样会出现精度误差导致无法AC。具体原因我想了一下,比如q原来是1,当q减去一个很小的数的时候,破了double的精度,那么q还是1,然后这个数就被丢掉了,造成了误差。

代码(31MS)(递推会快一点的……):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long long LL; const int MAXN = ;
const double EPS = 1e-; double dp[MAXN];
double c[MAXN][MAXN]; double C(int n, int m) {
if(c[n][m] > EPS) return c[n][m];
if(n == m) return c[n][m] = ;
if(m == ) return c[n][m] = n;
return c[n][m] = C(n - , m - ) + C(n - , m);
} double solve(int n) {
if(n == ) return ;
if(dp[n] > EPS) return dp[n];
double ret = , q = ;
for(int i = ; i < n; ++i) {
double p = C(n, i) * pow(./, n - );
ret += p * (solve(i));
q += p;
}
return dp[n] = (ret + ) / (q);
} int main() {
int n;
while(scanf("%d", &n) != EOF) {
cout.precision();
cout<<setiosflags(ios::fixed)<<solve(n)<<endl;
}
}

URAL 1936 Roshambo(求期望)的更多相关文章

  1. URAL 1936 Roshambo 题解

    http://acm.timus.ru/problem.aspx?space=1&num=1936 F - Roshambo Time Limit:1000MS Memory Limit:65 ...

  2. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  3. ZOJ 3822(求期望)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  4. sgu 495. Kids and Prizes (简单概率dp 正推求期望)

    题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ...

  5. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  6. HDU 5159 Card (概率求期望)

    B - Card Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  7. Poj 2096 (dp求期望 入门)

    / dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要 ...

  8. poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3523   Accepted: 1740 ...

  9. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. 浅谈二分查找 JavaScript

    算法介绍 二分查找,也称折半查找,是一种在有序数组中查找特定元素的搜索算法.查找过程经历一下步骤: (1)从有序数组的中间的元素开始搜索,如果该元素正好是目标元素,则停止搜索并返回该元素的索引值,否则 ...

  2. Ajax全接触(2)

    例子简介 1.查询员工信息,可以通过输入员工编号查询员工基本信息: 2.新建员工信息,包含员工姓名,员工编号,员工性别,员工职位: 实现: 1.纯html页面,用来实现员工查询和新建的页面: 2.ph ...

  3. IP检验字段为啥只检验地址部分

    在首部中的错误比在数据中的错误更重 如:一个错误的地址可能导致分组被投递到错误的主机.许多主机并不检查投递给它们的分组是否 确定是要投递给它们,它们假定网络从来不会把别人的分组包传递给自己.数据不参加 ...

  4. center os 创建用户、设置密码、修改用户、删除用户命令

    参考:https://www.linuxidc.com/Linux/2017-06/144916.htm useradd testuser  创建用户testuserpasswd testuser  ...

  5. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--K-密码

    链接:https://www.nowcoder.com/acm/contest/90/K 来源:牛客网 - 1.题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上. ...

  6. 转载:Spring使用p名称空间配置属性

    这篇博客简明扼要地介绍了Spring中p命名空间的使用,笔者在此转载一下. 原文链接:https://blog.csdn.net/liaomin416100569/article/details/49 ...

  7. 构建高可靠hadoop集群之5-服务级别授权

    本人翻译自: http://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-common/ServiceLevelAuth.html ...

  8. Lavavel5.5源代码 - 限流工具

    app('redis')->connection('default')->throttle('key000') // 每60秒,只能有10个资源被获取,在3秒内获取不到锁抛出异常 -> ...

  9. PHPPCRE正则解析

    一.前言 前面的博客里,有对字符集的解析.这里就不是字符集的事儿了,在PHP中很多函数的处理默认是unicode中的UTF-8编码格式.那么废话不多说,直接开始正题. 二.PHP函数mb_split解 ...

  10. hadoop生态搭建(3节点)-06.hbase配置

    # http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...