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

析:先假设第一天晚上就不高兴的去睡觉的概率是 q,那么有期望公式可以得到 E = q + (1-q) * (E + 1),其中 E 就是数学期望,那么可以解得 E = 1/ q,所以答案就是 1 / q,这个公式是什么意思呢,把数学期望分成两类,第一类就是第一天晚上就不再玩了,概率是 q,期望就是 1,第二类就是第一天高兴的睡觉,概率就是 1 - q,期望就是 E + 1。现在问题就是怎么求 q,这就是一个概率DP,dp[i][j] 表示玩 i 局,胜了 j 局的概率,并且要保证,胜的比例不超过 p,这样最后把所有的概率加起来就是数学期望,转移方程是 dp[i][j] = dp[i-1][j] * (1-p) + dp[i-1][j-1] * p。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 100 + 2;
const LL mod = 100000000;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} double dp[maxn][maxn]; int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
int molecule, denominator;
scanf("%d/%d %d", &molecule, &denominator, &n);
double p = molecule * 1. / denominator;
ms(dp, 0); dp[0][0] = 1.;
for(int i = 1; i <= n; ++i){
dp[i][0] = dp[i-1][0] * (1-p);
for(int j = 1; denominator * j <= molecule * i; ++j)
dp[i][j] += dp[i-1][j-1] * p + dp[i-1][j] * (1-p);
}
double ans = dp[n][0];
for(int j = 1; denominator * j <= molecule * n; ++j)
ans += dp[n][j];
printf("Case #%d: %d\n", kase, (int)(1. / ans));
}
return 0;
}

  

UVa 11427 Expect the Expected (数学期望 + 概率DP)的更多相关文章

  1. UVA 11427 Expect the Expected (期望)

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

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

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

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

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

  4. ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望||概率DP)

    Background If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at ...

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

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

  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. POJ3682King Arthur's Birthday Celebration(数学期望||概率DP)

    King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. Th ...

随机推荐

  1. 干净的ssm框架项目

    其中数据库只有如下表与字段 访问效果: 项目下载: 干净的ssm框架项目.rar

  2. android项目安装报错:INSTALL_FAILED_CONFLICTING_PROVIDER

    这主要是由于调试的环境中已有一个同名的Provider存在. 解决方法是修改AndroidManifest.xml中的 <provider android:name="applockP ...

  3. 通过PHP CURL模拟请求上传文件|图片。

    现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...

  4. clickableSpan实现textView文字部分点击有响应

    先定义一个clickableSpan的子类 class MyClickText extends ClickableSpan{ private Context context; public MyCli ...

  5. mysql命令行导入结构化数据

    数据样本 103252765-|--|-stephanie_mt@hotmail.com-|-o/35+nGaNEU=-|-ion|-- 其中|为分隔符,每行的换行符\n mysql -uroot M ...

  6. Array Division 808D

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Windows MySQL5.7安装和配置

    http://www.leixuesong.cn/category/mysql MySQL5.7是MySQL是最新的MySQL大版本,也是官方认为目前性能最好的.MySQL5.7也有很多改动,很多新的 ...

  8. Python之路(第二十三篇) 面向对象初级:静态属性、静态方法、类方法

    一.静态属性 静态属性相当于数据属性. 用@property语法糖装饰器将类的函数属性变成可以不用加括号直接的类似数据属性. 可以封装逻辑,让用户感觉是在调用一个普通的数据属性. 例子 class R ...

  9. python 日志滚动 分文件

    import logging from logging.handlers import RotatingFileHandler import datetime import os def main() ...

  10. IOS初级:AFNetworking

    狗 日的,第三方框架真j8难搞 1.为什么NS_ASSUME_NONNULL_BEGIN在6.2报错,你他么的还挑IDE,你这是什么态度? 2.还有,你他么的自动给老子转json了,有问过我么? #i ...