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 of n coupons?
Input
Input consists of a sequence of lines each containing a single positive integer n, 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 of n 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
(Math Lovers’ Contest, Source: University of Alberta Local Contest)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long ll a , s , m , n; ll gcd(ll a,ll b) { if(a == 0) return b; return gcd(b % a , a); } int wei(ll a) { int aa = 0; while(a) { aa ++; a /= 10; } return aa; } int main() { while(cin >> n) { a = 0; s = m = 1; for(ll i=2;i<=n;i++) { s = s * i + m; m *= i; ll t = gcd(s , m); s /= t; m /= t; } ll t = gcd(n , m); n /= t; m /= t; s *= n; a = s / m; s %= m; if(s == 0) { cout << a << endl; continue; } int wa = wei(a) , ws = wei(s) , wm = wei(m); for(int i=0;i<=wa;i++) printf(" "); cout << s << endl; cout << a; printf(" "); for(int i=0;i<wm;i++) printf("-"); puts(""); for(int i=0;i<=wa;i++) printf(" "); cout << m << endl; } return 0; }