Description
Problem 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 question: how many out-of-the-box tickets are expected to be collected for n Different coupons? If f [I] is set, the expected number of out-of-the-box coupons are not collected, then, we only need to calculate the expectation that we have not obtained + the expectation of the previous operation + the number of operations 1, that is, F [I] = (n-I) /n * f [I] + I/N * f [I + 1] + 1, after moving the item, F [I] = (n-I) /n * f [I + 1] + 1)/(n-I)/n), then it is a score calculation template.# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace STD; const int maxn = 50; struct fraction {long numerator; // long denominator; // denominator fraction () {Numerator = 0; Denominator = 1;} fraction (long num) {Numerator = num; Denominator = 1 ;} fraction (long a, long B) {Numerator = A; Denominator = B; this-> ction ();} void operator = (const long Num) {Numerator = num; Denominator = 1; this-> ction ();} void operator = (const fraction & B) {Numerator = B. numerator; Denominator = B. denominator; this-> ction ();} fraction operator + (const fraction & B) const {long gcdnum = _ gcd (denominator, B. denominator); fraction TMP = fraction (numerator * (B. denominator/gcdnum) + B. numerator * (denominator/gcdnum), denominator/gcdnum * B. denominator); TMP. red Uction (); 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. Ction (); 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 callback ction () {If (Numerator = 0) {Denominator = 1; return;} Long gcdnum = _ gcd (numerator, denominator ); numerator/= gcdnum; denominator/= gcdnum;} void print () {If (denominator = 1) printf ("% LLD \ n", numerator); else {long num = numerator/denominator; 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 [N] = 0; For (INT I = n-1; I> = 0; I --) f [I] = (fraction (n-I, n) * f [I + 1] + 1)/fraction (n-I, n); F [0]. print ();} return 0 ;}
UV-10288 coupons (probability + recurrence)