Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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; }