zoukankan      html  css  js  c++  java
  • [POJ2625][UVA10288]Coupons

    Description

    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<=22, giving the size of the set of coupons.

    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 ouput.

    题目分析

    这是一道求期望的题,假设当前已经有 (k) 种 Coupons, 那么获得新的 Coupons 的概率是 ((n-k)/n),所以需要步数的期望是 $n/(n-k) $。求和得到步数的期望是 (n/n + n/(n-1) + cdots + n/1 = nsum_{i=1}^{n} 1/i)
    这道题可以递推来做,但是我懒。(还有,luogu上的难度是 省选- 你怕是在逗我吧。)

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    typedef long long qword;
    
    const qword fen[] = {-1,1,2,6,12,60,60,420,840,2520,2520,27720,27720,360360,360360,360360,720720,12252240,12252240,232792560,232792560,232792560,232792560,5354228880,5354228880,26771144400,26771144400,80313433200,80313433200,2329089562800,2329089562800,72201776446800,144403552893600,144403552893600,};
    
    inline qword __gcd(qword a, qword b) {
        if (a == 1 || b == 1) return 1;
        if (b == 0) return a;
        else return __gcd(b, a % b);
    }
    int getdig(qword x) {
        int res = 0;
        while (x) {
            res ++;
            x /= 10;
        }
        return res;
    }
    
    inline void work(int n) {
        if (n == 1) {cout << 1 << endl; return;}
        qword fenzi = 0, fenmu = fen[n];
        qword ans = 0;
        for (qword i = 1; i <= n; ++ i) {
            if (i < n) fenzi += fenmu / i;
            if (fenzi >= fenmu) {ans += (fenzi / fenmu); fenzi %= fenmu;}
            if (i == n) {fenmu /= n;ans *= n; ans += 1;}
            if (fenzi >= fenmu) {ans += (fenzi / fenmu); fenzi %= fenmu;}
        }
        int uuu = __gcd(fenzi, fenmu);
        fenzi /= uuu;
        fenmu /= uuu;
    
        if (fenzi == 0) {
            cout << ans << endl;
        } else {
            for (int i = 1; i <= getdig(ans) + 1; ++ i) printf(" ");
            printf("%I64d
    %I64d ", fenzi, ans);
            for (int i = 1; i <= getdig(fenmu); ++ i) printf("-");
            putchar('
    ');
            for (int i = 1; i <= getdig(ans) + 1; ++ i) printf(" ");
            printf("%I64d
    ", fenmu);
        }
    }
    
    int main() {
        int n;
        while(cin >> n)
        work(n);
    }
    
    
  • 相关阅读:
    input 只能输入数字
    “学生宿舍管理系统”主要内容及特点
    web_03Java ee实现定时跳转,使用C3P0,DBUtils类重构数据库操作
    DBUtils工具类的使用
    C3P0连接池
    java ee 中 Jsp 页面的定时的跳转(数字倒数)
    JSP中实现网页访问统计的方法【转】
    Java web验证码
    web_02Java ee实现验证码,网站访问次数功能
    web_01Java ee实现登陆注册功能
  • 原文地址:https://www.cnblogs.com/Alessandro/p/9580239.html
Copyright © 2011-2022 走看看