uva 10288 Coupons 概率

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1229

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;
}

  

转载于:https://www.cnblogs.com/tobec/p/p0001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值