Roblem F
Coupons
Input:Standard Input
Output:Standard output
Time limit:2 seconds
Memory limit:32 MB
Coupons in cereal boxes are numbered1ToN, 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 setNCoupons?
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 setNCoupons. 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 shoshould be irreducible. there shoshould be no trailing spaces in any line of output.
Sample Input
2
5
17
Sample output
3
5
11 --
12
340463
58 ------
720720
When there are n patterns to be collected, the average number of times required is this -- expect E (t );
This question also has a test point: The score representation:
View the score calculation in the previous article
Code:
#include <cassert> #include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <stdlib.h> using namespace std; typedef long long ll; ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } struct frac{ ll up,low; frac(ll up=0,ll low=1){ if(low<0) up=-up,low=-low; assert(low); ll g=gcd(abs(up),low); this->up=up/g,this->low=low/g; } frac operator + (const frac &b) const { return frac(up * b.low + low * b.up, low * b.low); } frac operator - (const frac &b) const { return frac(up * b.low - low * b.up, low * b.low); } frac operator * (const frac &b) const { return frac(up * b.up, low * b.low); } frac operator / (const frac &b) const { return frac(up * b.low, low * b.up); } bool operator < (const frac &b) const { return up * b.low < low * b.up; } bool operator == (const frac &b) const { return up * b.low == low * b.up; } bool operator > (const frac& b) const { return b < *this; } bool operator <= (const frac& b) const { return !(b < *this); } bool operator >= (const frac &b) const { return !(*this < b); } bool operator != (const frac &b) const { return up * b.low != low * b.up; } frac operator += (const frac &b) { return *this = *this + b; } frac operator -= (const frac &b) { return*this = *this - b; } frac operator *= (const frac &b) { return *this = *this * b; } frac operator /= (const frac &b) { return *this = *this / b; } }; int main(){ int n; while(~scanf("%d",&n)){ frac ans[100]; ans[1]=frac(1,1); for(int i=2;i<=n;i++){ ans[i]=ans[i-1]+frac(1,i); } ans[n]*=frac(n,1); ll a,b,c; a=ans[n].up/ans[n].low; b=ans[n].up%ans[n].low; c=ans[n].low; if(a&&b){ ll t=1; while(a>=t){ printf(" "); t*=10; } printf(" "); } if(b) printf("%lld\n",b); if(a) printf("%lld",a); if(b){ printf(" "); ll t=1; while(c>=t){ printf("-"); t*=10; } } printf("\n"); if(a&&b){ ll t=1; while(a>=t){ printf(" "); t*=10; } printf(" "); } if(b)printf("%lld\n",c); } return 0; }
Actually, I know the formula, the most difficult part is the score representation .................................. ...........................