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);
    }
    
    
  • 相关阅读:
    计算机中的二进制运算
    面试题14:剪绳子
    面试题13:机器人的运动范围
    面试题12:矩阵中的路径
    面试题11:旋转数组的最小数字
    面试题10_2:跳台阶
    面试题10:斐波那契数列
    HDU 2202 最大三角形(凸包)
    刚装的系统C盘占空间特别大怎么办?关闭win7的系统还原和调整虚拟内存
    POJ 1113 Wall (凸包)
  • 原文地址:https://www.cnblogs.com/Alessandro/p/9580239.html
Copyright © 2011-2022 走看看