首页 > 代码库 > 10288 - Coupons(赠券收集问题)(概率)

10288 - Coupons(赠券收集问题)(概率)

roblem F

Coupons

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

 

Coupons in cereal boxes are numbered 1 to n, and a set of one of each is required for a prize (a cereal box, of course). With one coupon per box, how many boxes on average are required to make a complete set of n coupons?

Input

Input consists of a sequence of lines each containing a single positive integer n, 1<=n<=33, giving the size of the set of coupons. Input is terminated by end of file.

Output

For each input line, output the average number of boxes required to collect the complete set of n coupons. If the answer is an integer number, output the number. If the answer is not integer, then output the integer part of the answer followed by a space and then by the proper fraction in the format shown below. The fractional part should be irreducible. There should be no trailing spaces in any line of output.

Sample Input

2
5
17

Sample Output

3 
   5
11 --
   12
   340463
58 ------
   720720


当有n种图案需要收集时,平均需要的次数就是这个——期望E(T);


本题还有个考查点:分数的表示:

看上一篇分数运算

代码:

#include <cassert>  
#include <cstdio>  
#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <cstring>  
#include <stdlib.h>  
  
using namespace std;  
typedef long long ll;  
  
ll gcd(ll a,ll b){  
    return b?gcd(b,a%b):a;  
}  
struct frac{  
    ll up,low;  
    frac(ll up=0,ll low=1){  
        if(low<0) up=-up,low=-low;  
        assert(low);  
        ll g=gcd(abs(up),low);  
        this->up=up/g,this->low=low/g;  
    }  
    frac operator + (const frac &b) const  
    {  
        return frac(up * b.low + low * b.up, low * b.low);  
    }  
    frac operator - (const frac &b) const  
    {  
        return frac(up * b.low - low * b.up, low * b.low);  
    }  
    frac operator * (const frac &b) const  
    {  
        return frac(up * b.up, low * b.low);  
    }  
    frac operator / (const frac &b) const  
    {  
        return frac(up * b.low, low * b.up);  
    }  
    bool operator < (const frac &b) const  
    {  
        return up * b.low < low * b.up;  
    }  
    bool operator == (const frac &b) const  
    {  
        return up * b.low == low * b.up;  
    }  
    bool operator > (const frac& b) const  
    {  
        return b < *this;  
    }  
    bool operator <= (const frac& b) const  
    {  
        return !(b < *this);  
    }  
    bool operator >= (const frac &b) const  
    {  
        return !(*this < b);  
    }  
    bool operator != (const frac &b) const  
    {  
        return up * b.low != low * b.up;  
    }  
    frac operator += (const frac &b)  
    {  
        return *this = *this + b;  
    }  
    frac operator -= (const frac &b)  
    {  
        return*this = *this - b;  
    }  
    frac operator *= (const frac &b)  
    {  
        return *this = *this * b;  
    }  
    frac operator /= (const frac &b)  
    {  
        return *this = *this / b;  
    }  
  
};  
  
int main(){  
    int n;  
    while(~scanf("%d",&n)){  
        frac ans[100];  
        ans[1]=frac(1,1);  
        for(int i=2;i<=n;i++){  
            ans[i]=ans[i-1]+frac(1,i);  
        }  
        ans[n]*=frac(n,1);  
        ll a,b,c;  
        a=ans[n].up/ans[n].low;  
        b=ans[n].up%ans[n].low;  
        c=ans[n].low;  
        if(a&&b){  
            ll t=1;  
            while(a>=t){  
                printf(" ");  
                t*=10;  
            }  
            printf(" ");  
        }  
        if(b) printf("%lld\n",b);  
        if(a) printf("%lld",a);  
        if(b){  
            printf(" ");  
            ll t=1;  
            while(c>=t){  
                printf("-");  
                t*=10;  
            }  
        }  
        printf("\n");  
        if(a&&b){  
            ll t=1;  
            while(a>=t){  
                printf(" ");  
                t*=10;  
            }  
            printf(" ");  
        }  
       if(b)printf("%lld\n",c);  
    }  
    return 0;  
} 

其实知道了公式,最难的还是分数的表示.............................................................