# uva 10288 coupons

335人阅读 评论(0)

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

(吐槽一下原题目 ，英文的也很短 ，妈蛋看了半个小时没读懂，好歹也是过了六级的人，果然智商有限）

X = X1 + X2 + ... + Xk

Xi服从几何分布，由期望公示E(X+Y) = E(X) + E(Y) 就可以得到结果

（以上分析很有可能是不对的， 欢迎修正）

AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cassert>

using namespace std;

typedef long long LL;

LL n, r;
LL p, q;

LL gcd(LL a, LL b){
if(b == 0){
return a;
}
return gcd(b, a % b);
}
void reduction(LL &a, LL &b){
LL d = gcd(a, b);
a /= d;
b /= d;
}
p = p * b + a * q;
//cout << p << " " ;
q *= b;
reduction(p, q);
}
void printF(LL p){
LL rr = r;
while(rr){
printf(" ");
rr /= 10;
}
printf(" ");
printf("%lld\n", p);
}
int main(){
#ifdef DEBUG
FILE *fPtr = freopen("a.in", "r", stdin);
assert(fPtr != NULL);
fPtr = freopen("a.out", "w", stdout);
assert(fPtr != NULL);
#endif
while(scanf("%lld", &n) == 1){
p = 0LL, q = 1LL;
r = n;
for(LL i = 1; i <= n; i++){
}
//cout << p << "  " << q << endl;
reduction(r, q);
p *= r;
//cout << "p = " << r << endl;
r = p / q;
p %= q;
if(p % q == 0){
printf("%lld\n", r);
}
else{
printF(p);
printf("%lld ", r);
LL qq = q;
while(qq){
printf("-");
qq /= 10;
}
printf("\n");
printF(q);
}
}
return 0;
}


(不得不说uva一直很受伤人 有时候多一个空格都是wa  而不是pe

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2536次
• 积分：102
• 等级：
• 排名：千里之外
• 原创：8篇
• 转载：0篇
• 译文：0篇
• 评论：1条
文章存档
阅读排行
评论排行
最新评论