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);
    }
    
    
  • 相关阅读:
    和阿文一起学H5-文字云制作
    uml与数据库设计
    设计模式——面向对象设计原则
    koajs框架解决的问题
    mongodb
    javascript 获取select选中text,2种方法
    带参数跳转
    express表单提交和参数接收4种方式
    ejs模版的4种输出方式
    nodejs事件
  • 原文地址:https://www.cnblogs.com/Alessandro/p/9580239.html
Copyright © 2011-2022 走看看