【Uva10288】——Coupons概率

来源:互联网 发布:nginx启用gzip压缩 编辑:程序博客网 时间:2024/06/11 08:49

题意:一共有n种不同的Coupons,每次得到每种Coupons的概率是相同的,问期望多少次得到所有的n种Coupons

假设我们已经得到k种Coupons,那么我们得到新的Coupons的概率为nkn,所需的期望步数是nnk。求和我们得到总的期望步数nn+nn1+n1=n×i=1n1i,注意分式的化简

#include <bits/stdc++.h>using namespace std;typedef long long LL;int GetLen(LL s) {    int ans = 0;    while(s) {        ans++;        s/=10;    }    return ans;}int main() {    int n;    while(~scanf("%d",&n)) {        LL p =0, q =1;        for(int i  = 1; i<=n; i++) {            LL a = p*i+q;            LL b = q*i;            p =  a, q = b;            LL g = __gcd(p,q);            p/=g,q/=g;        }        p*=n;        LL g = __gcd(p,q);        p/=g,q/=g;        if(p%q ==0) {            printf("%lld\n",p/q);        } else {            LL a = p/q;            LL b = p%q;            int len = GetLen(q);            if(a == 0) {                printf("%lld\n",b);                for(int i = 1; i<=len; i++) printf("-");                printf("\n");                printf("%lld\n",q);            } else {                int len1 = GetLen(a)+1;                for(int i = 1; i<=len1; i++) printf(" ");                printf("%lld\n",b);                printf("%lld ",a);                for(int i = 1; i<=len; i++) printf("-");                printf("\n");                for(int i = 1; i<=len1; i++) printf(" ");                printf("%lld\n",q);            }        }    }    return 0;}
1 0
原创粉丝点击