zoukankan      html  css  js  c++  java
  • UVa 11427 Expect the Expected (数学期望 + 概率DP)

    题意:某个人每天晚上都玩游戏,如果第一次就䊨了就高兴的去睡觉了,否则就继续直到赢的局数的比例严格大于 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
    ", kase, (int)(1. / ans));
      }
      return 0;
    }
    

      

  • 相关阅读:
    linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案
    MySQL.报错2059处理方法
    抽象工厂模式的优缺点和适用场景
    字节与字符的区别
    Kubernetes诞生及历史
    k8s-设计理念-原理图
    JSF中的状态模式
    关于spring框架JdbcTemplate中的命令模式
    浅谈springMVC中的设计模式(1)——责任链模式
    Spring中的观察者模式
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8511663.html
Copyright © 2011-2022 走看看