uva10288(数学期望)

/*
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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值