UVa10288 - Coupons

UVa10288 - Coupons

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.

这道题是问共n种不同的优惠券,每次取一个得到每种可能相同,问得到所有n种优惠券的次数的数学期望。
解:若已经得到k个优惠券,则P(得到剩下n-k个coupons) = (n-k)/n。因为是几何分布,所以E(得到剩下n-k个coupons) = 1/P(得到剩下n-k个coupons = n / (n-k) 。
对于每取到一个coupons,设该事件为xi,每个xi相对独立。根据期望的线性性,E(∑xi) = ∑E(xi)。所以总的取到所有n个conpons的期望是∑n/i
剩下就是分数的模拟了,感觉写的有点蠢。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> 
using namespace std;
typedef long long LL;

LL gcd(LL a, LL b){
    if (b == 0) return a;
    return gcd(b, a % b);
}

int places(LL num){
    int tmp = 0;
    while (num) {
        tmp++;
        num /= 10;
    }   
    return tmp;
}

struct Fraction{
    LL ne, de, in;
    void simplify(){
        in = ne / de;
        ne %= de;
    }
    Fraction operator + (const Fraction &b) const{
        Fraction res;
        LL bri1 = ne * b.de + b.ne * de;
        LL bri2 = de * b.de;
        LL tmp = gcd(bri1, bri2);
        res.ne = bri1 / tmp;
        res.de = bri2 / tmp;
        res.in = in + b.in + res.ne / res.de;
        res.ne = res.ne % res.de;
        return res;
    }
    void print(){
        int PL = places(in);
        for (int i = 1; i <= PL + 1; i++) printf(" ");
        printf("%lld\n", ne);
        printf("%lld ", in);
        for (int i = 1; i <= places(max(ne, de)); i++)
          printf("-");
        printf("\n");
        for (int i = 1; i <= PL + 1; i++) printf(" ");
        printf("%lld\n", de);
    }
};

int main()
{
    int n;
    while (~scanf("%d", &n)) {
      Fraction begin;
      begin.ne = begin.in = 0;
      begin.de = 1;
      for (int i = 1; i <= n; i++) {
        Fraction f;
        f.ne = n;
        f.de = i;
        f.in = 0;
        f.simplify();
        begin = begin + f;
      }
      if (begin.ne == 0){
        printf("%lld\n", begin.in);
        continue;
      }
      begin.print();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值