HDU - 1099 - Lottery - 概率dp

http://acm.hdu.edu.cn/showproblem.php?pid=1099

最最简单的概率dp,完全是等概率转移。
设dp[i]为已有i张票,还需要抽几次才能集齐的期望。

那么dp[n]=0,因为我们已经集齐了。

\[dp[i]=(\frac{i}{n}*dp[i]+\frac{n-i}{n}*dp[i+1])+1\]

移项得答案。

然后写个分数类,注意约分。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct Fraction{
    ll fz;
    ll fm;

    Fraction(){}

    Fraction(ll z,ll m):fz(z),fm(m){}

    Fraction operator+(Fraction f){
        ll m=fm/__gcd(fm,f.fm)*f.fm;
        Fraction res;
        res.fz=fz*(m/fm)+f.fz*(m/f.fm);
        res.fm=m;
        res.tf();
        return res;
    }

    Fraction operator-(Fraction f){
        ll m=fm/__gcd(fm,f.fm)*f.fm;
        Fraction res;
        res.fz=fz*(m/fm)-f.fz*(m/f.fm);
        res.fm=m;
        res.tf();
        return res;
    }

    Fraction operator*(Fraction f){
        Fraction res;
        res.fz=fz*f.fz;
        res.fm=fm*f.fm;
        res.tf();
        return res;
    }

    void tf(){
        ll g=__gcd(fz,fm);
        fz/=g;
        fm/=g;
    }

    void show(){
        ll zs=fz/fm;
        ll ys=fz%fm;

        if(ys==0){
            cout<<zs<<endl;
        }
        else{
            string l1,l2,l3;
            l1=" ";
            l2=" ";
            l3=" ";
            while(zs){
                l2=char(zs%10+'0')+l2;
                zs/=10;
                l1+=" ";
                l3+=" ";
            }

            ll cfm=fm;

            string n3="";
            string n2="";
            while(cfm){
                n3=char(cfm%10+'0')+n3;
                cfm/=10;
                n2+="-";
            }

            l3+=n3;
            l2+=n2;

            string n1="";

            ll cfz=ys;

            while(cfz){
                n1=char(cfz%10+'0')+n1;
                cfz/=10;
            }

            l1+=n1;

            cout<<l1<<endl<<l2<<endl<<l3<<endl;
        }
    }
};

Fraction dp[23];

int main(){
    int n;
    while(cin>>n){
        dp[n]=Fraction(0,1);
        for(int i=n-1;i>=0;i--){
            dp[i]=dp[i+1]+Fraction(n,n-i);
        }
        dp[0].show();
    }
}

原文地址:https://www.cnblogs.com/Yinku/p/10739525.html

时间: 2024-10-13 12:43:15

HDU - 1099 - Lottery - 概率dp的相关文章

hdu 4870 Rating(概率DP&amp;高数消元)

Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 714    Accepted Submission(s): 452 Special Judge Problem Description A little girl loves programming competition very much. Recently, she

HDU 4035Maze(概率DP)

HDU 4035   Maze 体会到了状态转移,化简方程的重要性 题解转自http://blog.csdn.net/morgan_xww/article/details/6776947 /** dp求期望的题. 题意: 有n个房间,由n-1条隧道连通起来,实际上就形成了一棵树, 从结点1出发,开始走,在每个结点i都有3种可能: 1.被杀死,回到结点1处(概率为ki) 2.找到出口,走出迷宫 (概率为ei) 3.和该点相连有m条边,随机走一条 求:走出迷宫所要走的边数的期望值. 设 E[i]表示

HDU 3853 LOOPS (概率dp)

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2931    Accepted Submission(s): 1209 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wants to help

HDU 4089 Activation (概率dp 好题 + 难题)

Activation Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1842    Accepted Submission(s): 689 Problem Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out.

HDU 4599 Dice (概率DP+数学+快速幂)

题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n),这个用DP来推公式,d[i],表示抛 i 次连续的点数还要抛多少次才能完成.那么状态转移方程就是 d[i] = 1/6*(1+d[i+1]) + 5/6*(1+d[1]), 意思就是说在第 i 次抛和上次相同的概率是1/6,然后加上上次抛的和这一次,再加上和上次不同的,并且又得从第1次开始计算. 边界就是

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

HDU 4711 Weather 概率DP

题意:有个人,他在某个区域待了n天,这个区域有m个地方,有w种天气情况,先给出这个人行程的每天的天气情况,然后给出 从第i个地方到第j个地方的概率,也可以自身到自身,然后给出 某个地方 是某种天气的概率,问你 这个人最优可能的行程路线也就是每天待在哪个地方, 概率DP,求出哪些路线概率最大 再在其中取最小字典序的 假设方程 dp[i][j] 代表 第i天待在j城市,状态转移 dp[i][j] = max(dp[i][j],dp[i - 1][k] * mp[k][j] * pp[j][nnum[

HDU 3853 期望概率DP

期望概率DP简单题 从[1,1]点走到[r,c]点,每走一步的代价为2 给出每个点走相邻位置的概率,共3中方向,不动: [x,y]->[x][y]=p[x][y][0] ,  右移:[x][y]->[x][y+1]=p[x][y][1];  左移:[x][y]->[x+1][y]=p[x][y][2]; 问最后走到[r,c]的期望 dp[i][j]为从[i][j]点走到[r][c]的期望 有方程: dp[i][j]=    (dp[i][j]+2)*p[i][j][0]  +   (dp

HDU 4035 Maze 概率DP 好题

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 802Special Judge Problem Description When wake up, lxhgww find himself in a huge maze. The maze consisted by