/*
translation:
每张彩票上面都有一种图案,共有n种,问在平均情况下最少需要买多少张彩票才能集齐n种。
solution:
期望
假设已经集齐了k张图案,所以要找到一张新的图案平均需要购买n/(n-k)张彩票。所以总次数为
sum{n/(n-i)}其中i取值范围[1, n]。
note:
date: 2016.10.8
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 40;
typedef long long ll;
int n;
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
int getLen(ll x) {
int res = 0;
while(x) {
res++;
x /= 10;
}
return res;
}
void printAns(ll dis, ll up, ll down) {
if(up == 0) cout << dis << endl;
else {
int len = getLen(dis); ++len;
while(len--) cout << " ";
cout << up << endl;
cout << dis << " ";
len = getLen(down);
while(len--) cout << "-";
cout << endl;
len = getLen(dis); ++len;
while(len--) cout << " ";
cout << down << endl;
}
}
int main()
{
//freopen("in.txt", "r", stdin);
while(cin >> n) {
ll tmp = 1;
for(int i = 2; i <= n; i++)
tmp = lcm(tmp, (ll)i);
ll down = tmp;
ll up = 0;
for(int i = 1; i <= n; i++) {
up += (tmp/i);
}
up *= n;
ll g = gcd(up, down);
up /= g; down /= g;
ll dis = up / down;
up = up % down;
printAns(dis, up, down);
}
return 0;
}
uva10288(数学期望)
最新推荐文章于 2018-08-29 00:33:12 发布