公式E(x)=n Σ 1/i
因为当已经拿到k张不同的时候 拿到不同牌的期望是 (n-k)/n ,1除于这个概率就是 n/(n-k) 然后从1到n加一下就可以了
Problem 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
(Math Lovers’ Contest, Source: University of Alberta Local Contest)
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cassert>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
struct Fraction
{
long long num;
long long den;
Fraction(long long num=0,long long den=1)
{
if(den<0)
{
num=-num;
den=-den;
}
long long g=gcd(abs(num),den);
this->num=num/g;
this->den=den/g;
}
Fraction operator +(const Fraction &o)const
{
return Fraction(num*o.den+den*o.num,den*o.den);
}
Fraction operator -(const Fraction &o)const
{
return Fraction(num*o.den-den*o.num,den*o.den);
}
Fraction operator *(const Fraction &o)const
{
return Fraction(num*o.num,den*o.den);
}
Fraction operator /(const Fraction &o)const
{
return Fraction(num*o.den,den*o.num);
}
bool operator <(const Fraction &o)const
{
return num*o.den<den*o.num;
}
bool operator ==(const Fraction &o)const
{
return num*o.den==den*o.num;
}
};
int n;
int main()
{
while(cin>>n)
{
Fraction ans(n,1);
Fraction tmp(0,1);
for(int i=1;i<=n;i++)
tmp=tmp+Fraction(1,i);
ans=ans*tmp;
long long a,b,c;
a=ans.num;
b=ans.den;
if(a/b!=0)
{
long long gd=gcd(a,b);
a/=gd;
b/=gd;
c=a/b;
a=a-c*b;
}
else
c=0;
if(a==0)
printf("%lld\n",c);
else
{
int l1,l2,l3;
long long t1=c,t2=a,t3=b;
l1=0;
while(t1)
{
t1/=10;
l1++;
}
l2=0;
while(t2)
{
t2/=10;
l2++;
}
l3=0;
while(t3)
{
t3/=10;
l3++;
}
if(l1!=0)
{
for(int i=0;i<=l1;i++) printf(" ");
printf("%lld\n",a);
printf("%lld ",c);
int len=max(l2,l3);
for(int i=0;i<len;i++) printf("-");
printf("\n");
for(int i=0;i<=l1;i++) printf(" ");
printf("%lld\n",b);
}
else
{
printf("%I64d\n",a);
int len=max(l2,l3);
for(int i=0;i<len;i++)
printf("-");
printf("\n");
printf("%I64d\n",b);
}
}
}
return 0;
}