UVA - 10288 Coupons (概率+递推)

Description

Download as PDF

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 ofn coupons?

Input

Input consists of a sequence of lines each containing a single positive integern, 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 ofn 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
题意:收集n个不同的优惠券的期望开箱次数
思路:设f[i]代表还有i个优惠券没有收集到的期望开箱次数,那么我们只要计算再没有取到的期望+上一个取到的期望+操作次数1,即f[i]=(n-i)/n*f[i]+i/n*f[i+1]+1,移项后是
f[i] = f[i+1] + i/n,然后就是一个分数计算的模板了
#include <cstdio>
#include <cstring> #include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 500; 

struct fraction {
	long long numerator; // 分子
	long long denominator; // 分母
	fraction() {
		numerator = 0;
		denominator = 1;
	}
	fraction(long long num) {
		numerator = num;
		denominator = 1;
	}
	fraction(long long a, long long b) {
		numerator = a;
		denominator = b;
		this->reduction();
	}

	void operator = (const long long num) {
		numerator = num;
		denominator = 1;
		this->reduction();
	}

	void operator = (const fraction &b) {
		numerator = b.numerator;
		denominator = b.denominator;
		this->reduction();
	}

	fraction operator + (const fraction &b) const {
		long long gcdnum = __gcd(denominator, b.denominator);
		fraction tmp = fraction(numerator*(b.denominator/gcdnum) + b.numerator*(denominator/gcdnum), denominator/gcdnum*b.denominator);
		tmp.reduction();
		return tmp;
	}

	fraction operator + (const int &b) const {
		return ((*this) + fraction(b));
	}

	fraction operator - (const fraction &b) const {
		return ((*this) + fraction(-b.numerator, b.denominator));
	}

	fraction operator - (const int &b) const {
		return ((*this) - fraction(b));
	}

	fraction operator * (const fraction &b) const {
		fraction tmp = fraction(numerator*b.numerator, denominator * b.denominator);
		tmp.reduction();
		return tmp;
	}

	fraction operator * (const int &b) const {
		return ((*this) * fraction(b));
	}

	fraction operator / (const fraction &b) const {
		return ((*this) * fraction(b.denominator, b.numerator));
	}

	void reduction() {
		if (numerator == 0) {
			denominator = 1;
			return;
		}
		long long gcdnum = __gcd(numerator, denominator);
		numerator /= gcdnum;
		denominator /= gcdnum;
	}
	void print() {
		if (denominator == 1) printf("%lld\n", numerator);
		else {
			long long num = numerator/denominator;
			long long tmp = num;
			int len = 0;
			while (tmp) {
				len++;
				tmp/=10;
			}

			for (int i = 0; i < len; i++) printf(" ");
			if (len != 0) printf(" ");
			printf("%lld\n",numerator%denominator);

			if (num != 0) printf("%lld ", num);
			tmp = denominator;
			while (tmp) {
				printf("-");
				tmp/=10;
			}
			puts("");

			for (int i = 0; i < len; i++) printf(" ");
			if (len != 0) printf(" ");
			printf("%lld\n",denominator);
		}
	}
} f[maxn];

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {

		f[0] = 0;
		for (int i = 1; i <= n; i++)
			f[i] = f[i-1] + fraction(1, i);
		f[n] = f[n] * n;

		f[n].print();
	}
	return 0;
}



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值