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

时间:2022-03-01 09:33:04

题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 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&lpar;概率&rpar;

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

  3. UVA 11427 - Expect the Expected&lpar;概率递归预期&rpar;

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

  4. ZOJ3640Help Me Escape(师傅逃亡系列•一)(数学期望&vert;&vert;概率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 &lpar;概率dp)

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

  6. UVA&period;11427&period;Expect the Expected&lpar;期望&rpar;

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

  7. UVA 11427 Expect the Expected(DP&plus;概率)

    链接: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&&num;39&semi;s Birthday Celebration(数学期望&vert;&vert;概率DP)

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

随机推荐

  1. 记JavaScript的入门学习(三)

    2016.12.6晚上十点半完成JavaScript的第二章学习,看了点第三章的开头总述,都说原生js每一个知识点都可以分分钟钟让你放弃,而我在努力探索着.月末的时候就回家放假了,希望在家也可以有个小 ...

  2. html5语义化标签总结二

    HTML 5的革新之一:语义化标签二文本元素标签.分组元素标签. HTML 5的革新——语义化标签(一)中介绍了一些HTML5新加的一些节元素,一张页面中结构元素构成网页大体,但是也需要其他内容来填充 ...

  3. 自定义控件之SegmentControlView

    SegmentControlView配合PageView使用 效果图 核心代码 package com.example.segmentcontrolview; import android.conte ...

  4. js--获得当前系统时间

    window.onload = function () { var oBody = document.body; setInterval( fnTime, 1000 ); fnTime (); fun ...

  5. 1017 B&period; The Bits

    链接 [http://codeforces.com/contest/1017/problem/B] 题意 给你两个长度为n,包含0和1的字符串a和b,有一种操作swap a中的任意两个字符使得a&am ...

  6. 《Linux内核设计与实现》第四章学习笔记

    <Linux内核设计与实现>第四章学习笔记           ——进程调度 姓名:王玮怡  学号:20135116 一.多任务 1.多任务操作系统的含义 多任务操作系统就是能同时并发地交 ...

  7. qt编程

    http://www.zhihu.com/question/20054048 http://www.cnblogs.com/luoshupeng/archive/2011/05/01/2033743. ...

  8. SSM&lowbar;CRUD新手练习(5)测试mapper

    上一篇我们使用逆向工程生成了所需要的bean.dao和对应的mapper.xml文件,并且修改好了我们需要的数据库查询方法. 现在我们来测试一下DAO层,在test包下新建一个MapperTest.j ...

  9. MVC MVP MVVM 图解

    1.MVC (1)图解 解释: 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下: View 传送指令到 Controll ...

  10. DataS-2

    2.4 证明对任意常数k,(称此式为公式A) 证明: ①当k1<k2时,,因此只需证明正数对公式A成立: ②当k=0或1时,显然有和满足公式A: ③假设k<i (i>1)时,都满足公 ...