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. 6.Spring Cloud初相识-------Zool路由

    前言: 在生产环境中,我们不可能将每个服务的真实信息暴漏出去,因为这样太不安全. 我们会选择使用路由代理真实的服务信息,由它负责转发给真实的服务. 新建一个Zool: 1.添加依赖 <?xml ...

  2. Oracle数据库中游标的游标的使用

    本人不喜欢说概念啥的,就直接说明使用方法吧 案例1: DECALRE --声明游标 CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE ...

  3. java之递归学习

    递归思想(2018-10-22): 递归就是方法里调用自身 在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口 递归算法代码显得很简洁,但递归算法解题的运行效率较低.所以不提倡用递归设计程序 ...

  4. Oracle数据库补充

      约束: 什么是约束以及约束的作用: 为保证数据的完整性(一致性,准确性),需要对数据进行限制,这个限制就叫做约束 目的:保证数据的完整性(一致性,正确性),使数据符合业务规则(业务逻辑)   约束 ...

  5. TabbarController进行模块分类和管理

    iOS-CYLTabBarController[好用的TabbarController]   用TabbarController进行模块分类和管理,这里推荐一个CYLTabBarController, ...

  6. struts2入门第一天----------一个简单例

    搭建完环境后就可以动手去打代码了.首先创建一个简单的提交表单的jsp页面(html页面也可以), <%@ page language="java" import=" ...

  7. phpstorm代码提示不小心关了,如何开启

    在phpstrom右下角单击如图 ​ 出现event log窗口 ​ 如果不是​ 单击切换取消节电模式即可开启代码提示.

  8. jquery easyui alert闪一下的问题

    最近做项目使用了 jQuery EasyUI,版本是 1.4.3.x,在使用alert方法的时候如果alert后面执行页面跳转的话alert的消息只会闪一下,就跳到其他页面了 $.messager.a ...

  9. html 截图粘粘图片JS

    web前端socket聊天室功能和在线编辑器上传编辑内容的时候经常会需要上传一些图文信息,但是很多编辑器不支持截图粘粘的功能,这里参考了网友分享的可用方法做一个记录. <html> < ...

  10. unity独立游戏开发日志2018/09/22

    f::很头痛之前rm做的游戏在新电脑工程打不开了...只能另起炉灶... 还不知道新游戏叫什么名...暂且叫方块世界.(素材已经授权) 首先是规划下场景和素材文件夹的建立. unity常用的文件夹有: ...