hdu 1099 Lottery
题意:1~n编号的彩票,要买全,等概率条件下平均要买几张。
已经买了m张时,买中剩下的概率为1-m/n,则要买的张数为1/(1-m/n)
n=2,s=1+1/(1-1/2);n=3,s=1+1/(1-1/3)+1/(1-2/3)
s=1+1/(1-1/n)+1/(1-2/n)+1/(1-3/n)+……+1/(1-(n-1)/n)=n/n+n/(n-1)+n/(n-2)+……+n/1=sum(n/i),i=1~n
b/a+d/c=(bc+ad)/(ac)
然后递推着通分,化简;输出。

///实现 sum(n/i) (i=1~n)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
#define LL long long
LL gcd(LL a,LL b)
{
return b==?a:gcd(b,a%b);
}
struct fen{
LL a,b; };
fen& add(struct fen&a,struct fen&b)
{
a.a=a.a*b.b+a.b*b.a;
a.b=a.b*b.b;
LL t=gcd(a.a,a.b);
a.a/=t;
a.b/=t;
return a;
}
int ditNum(LL a)
{
int cnt=;
while(a)
{
cnt++;
a/=;
}
return cnt;
}
int main()
{
int n;
LL fenz=,fenm=;
struct fen f,f1;
while(~scanf("%d",&n))
{
f.a=n;f.b=;
for(int i=;i<=n;i++)
{
f1.a=n;f1.b=i;
f=add(f,f1);
}
if(f.a%f.b==)
{
cout<<f.a/f.b<<endl;
}
else
{
LL t=f.a/f.b;
int a=ditNum(t),b=ditNum(f.b);
a++;
int x=a;
while(a--)
cout<<" ";
cout<<f.a-t*f.b<<endl;
cout<<t<<" ";
while(b--)
cout<<"-";
cout<<endl;
while(x--)
cout<<" ";
cout<<f.b<<endl;
}
}
}
05-11 22:59