11427 - Expect the Expected

Some mathematical background. This problem asks you to compute the expected value of a random
variable. If you haven’t seen those before, the simple definitions are as follows. A random variable is a
variable that can have one of several values, each with a certain probability. The probabilities of each
possible value are positive and add up to one. The expected value of a random variable is simply the
sum of all its possible values, each multiplied by the corresponding probability. (There are some more
complicated, more general definitions, but you won’t need them now.) For example, the value of a fair,
6-sided die is a random variable that has 6 possible values (from 1 to 6), each with a probability of 1/6.
Its expected value is 1/6 + 2/6 + . . . + 6/6 = 3.5. Now the problem.
I like to play solitaire. Each time I play a game, I have probability p of solving it and probability
(1 − p) of failing. The game keeps statistics of all my games – what percentage of games I have won.
If I simply keep playing for a long time, this percentage will always hover somewhere around p ∗ 100%.
But I want more.
Here is my plan. Every day, I will play a game of solitaire. If I win, I’ll go to sleep happy until
the next day. If I lose, I’ll keep playing until the fraction of games I have won today becomes larger
than p. At this point, I’ll declare victory and go to sleep. As you can see, at the end of each day, I’m
guaranteed to always keep my statistics above the expected p ∗ 100%. I will have beaten mathematics!
If your intuition is telling you that something here must break, then you are right. I can’t keep
doing this forever because there is a limit on the number of games I can play in one day. Let’s say that
I can play at most n games in one day. How many days can I expect to be able to continue with my
clever plan before it fails? Note that the answer is always at least 1 because it takes me a whole day
of playing to reach a failure.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one is a line containing
p (as a fraction) and n.
1 ≤ N ≤ 3000, 0 ≤ p < 1,
The denominator of p will be at most 1000,
1 ≤ n ≤ 100.
Output
For each test case, print a line of the form ‘Case #x: y’, where y is the expected number of days,
rounded down to the nearest integer. The answer will always be at most 1000 and will never be within
0.001 of a round-off error case.
Sample Input
4
1/2 1
1/2 2
0/1 10
1/2 3
Sample Output
Case #1: 2
Case #2: 2
Case #3: 1
Case #4: 2

题解:题意是一个人玩牌,每次胜率是p,她每天晚上最多玩n局,如果胜的频率大于p就睡,明天继续,如果玩了n局还没大于p

就戒了,以后就不玩了;平均情况下,他可以玩几天;求期望,先求出每天哭着睡觉的概率,然后期望就是:s+=i*Q*pow(1-Q,i-1);

s=Q+2Q(1-Q)+3Q*(1-Q)^2........;大神们通过一定的推算可以得到s=1/Q;

还可以假设期望是e天,情况分两类,第一天哭着睡觉:概率Q,期望1;第一天开心睡觉:期望1-Q,期望1+e;e=Q*1+(1-Q)*(1+e);

e=1/Q;

不过我还是不太理解,概率论没学好T_T;

两种代码:

代码1:直接套了1/Q

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=110;
double dp[MAXN][MAXN];
int main(){//dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p)
int T,px,py,n,kase=0;
scanf("%d",&T);
while(T--){
scanf("%d/%d %d",&px,&py,&n);
//printf("%d/%d %d\n",px,py,n);
double p=1.0*px/py,Q=0;
mem(dp,0);dp[0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j*py<=px*i;j++){
dp[i][j]=dp[i-1][j]*(1-p);
if(j)dp[i][j]+=dp[i-1][j-1]*p;
if(i==n)Q+=dp[i][j];
}
}
printf("Case #%d: %d\n",++kase,(int)(1/Q));
}
return 0;
}

  代码2:暴力趋近o~o;到10w就抄了,1w就ac了,1e-15;

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=110;
double dp[MAXN][MAXN];
int main(){//dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p)
int T,px,py,n,kase=0;
scanf("%d",&T);
while(T--){
scanf("%d/%d %d",&px,&py,&n);
//printf("%d/%d %d\n",px,py,n);
double p=1.0*px/py,Q=0;
mem(dp,0);dp[0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j*py<=px*i;j++){
dp[i][j]=dp[i-1][j]*(1-p);
if(j)dp[i][j]+=dp[i-1][j-1]*p;
if(i==n)Q+=dp[i][j];
}
}
double s=1e-15;
// printf("%lf\n",s);
for(int i=1;i<=10000;i++)s+=i*Q*pow(1-Q,i-1);
printf("Case #%d: %d\n",++kase,(int)s);
}
return 0;
}

  

11427 - Expect the Expected(概率期望)的更多相关文章

  1. UVA 11427 - Expect the Expected(概率递归预期)

    UVA 11427 - Expect the Expected 题目链接 题意:玩一个游戏.赢的概率p,一个晚上能玩n盘,假设n盘都没赢到总赢的盘数比例大于等于p.以后都不再玩了,假设有到p就结束 思 ...

  2. uva 11427 - Expect the Expected(概率)

    题目链接:uva 11427 - Expect the Expected 题目大意:你每天晚上都会玩纸牌,每天固定最多玩n盘,每盘胜利的概率为p,你是一个固执的人,每天一定要保证胜局的比例大于p才会结 ...

  3. UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 p,并且他每局获胜的概率也是 p,但是你最玩 n 局,但是如果比例一直超不过 p 的话,你将不高兴 ...

  4. UVA - 11427 Expect the Expected (概率dp)

    Some mathematical background. This problem asks you to compute the expected value of a random variab ...

  5. UVA 11427 Expect the Expected (期望)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&pa ...

  6. UVA.11427.Expect the Expected(期望)

    题目链接 \(Description\) https://blog.csdn.net/Yukizzz/article/details/52084528 \(Solution\) 首先每一天之间是独立的 ...

  7. UVA 11427 Expect the Expected(DP+概率)

    链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 [思路] DP+概率 见白书. [代码] #include&l ...

  8. UVa 11427 - Expect the Expected

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVA11427 Expect the Expected 概率dp+全概率公式

    题目传送门 题意:小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉,第二天继续玩:否则继续玩,玩到赢的比例大于p才去睡:如果一直玩了n盘还没完成,就再也不玩了:问他玩游戏天数的期 ...

随机推荐

  1. sharepoint 自定义字段实现省市联动

    最后实现效果如下:设置栏如下:解决方案结构如下: fldtypes_RoyCustomField.xml 内容如下: <?xml version="1.0" encoding ...

  2. Jquery的一些简单使用记录

    //平滑滚动到底部 $(".list").scrollTo('100%', '100%', { easing: 'swing' }); //直接滚动至底部(无效果) $('.lis ...

  3. Mysql 正则表达式 判断字段值不包含数字

    SELECT * FROM (select replace(FlightId_IaTa,LEFT(FlightId_IaTa,2),'') as aa,FlightId_IaTa,FlightIdfr ...

  4. Java 网络编程(三) 创建和使用URL访问网络上的资源

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951877.html 创建和使用URL访问网络上的资源 URL(Uniform Reso ...

  5. C++ const 限定符

    C++ const 限定符 作用:把一个对象转换成一个常量 用法:const type name = value; 性质:1. 定义时必须初始化,定义后不能被修改.2. 类中的const成员变量必须通 ...

  6. servlet三种实现方式之一实现servlet接口

    servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...

  7. 在Linux中创建静态库和动态库 (转)

    我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态 库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而 ...

  8. addChildViewController ipad 中Controller的嵌套和叠加

    1.addChildViewController 在 base controller中添加子的controller,会自动调用子的controller中viewDidload,viewWillAppe ...

  9. GCD自己做的一些简单总结

    GCD总结 GCD  Grand Central Dispatch  牛逼的中枢调度器 GCD中各种队列的执行效果 想看线程  必须是异步函数  并且不是主队列 注意:使用sync函数往当前串行队列添 ...

  10. Gimp制作圆角透明图片

    用蒙版制作圆角透明图片,步骤如下: 1,用Gimp(2.8版本)打开图片 2,在图层窗口右键当前图层创建蒙版 3,选择蒙版类型黑色(全透明) 4,结果如下 5,用圆角矩形选择工具选择图片,设置圆角半径 ...