1. 程式人生 > >HDU1099---數學 | 思維

HDU1099---數學 | 思維

code struct algorithm n) math scan hdu get amp

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==0?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=0; while(a) { cnt++; a/=10; } return cnt; } int main() { int n; LL fenz=0,fenm=0; struct fen f,f1; while(~scanf("%d",&n)) { f.a=n;f.b=1;
for(int i=2;i<=n;i++) { f1.a=n;f1.b=i; f=add(f,f1); } if(f.a%f.b==0) { 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; } } }

HDU1099---數學 | 思維