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 = n\sum_{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\n%I64d ", fenzi, ans);
for (int i = 1; i <= getdig(fenmu); ++ i) printf("-");
putchar('\n');
for (int i = 1; i <= getdig(ans) + 1; ++ i) printf(" ");
printf("%I64d\n", fenmu);
}
} int main() {
int n;
while(cin >> n)
work(n);
}

[POJ2625][UVA10288]Coupons的更多相关文章

  1. UVa10288 Coupons 分数模版

    UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...

  2. uva10288 Coupons 【概率 分数】

    题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...

  3. 「SHOI2002」「LuoguP1291」百事世界杯之旅(UVA10288 Coupons)(期望,输出

    题目描述 “……在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯.还不赶 ...

  4. codeforces754D Fedor and coupons

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. codeforces 754D. Fedor and coupons

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  7. Uva 10288 Coupons

    Description Coupons in cereal boxes are numbered \(1\) to \(n\), and a set of one of each is require ...

  8. Coupons

    uva10288:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...

  9. Coupons and Discounts

    Coupons and Discounts time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. CentOS配置bond

    Bonding的模式一共有7种: #defineBOND_MODE_ROUNDROBIN       0   (balance-rr模式)网卡的负载均衡模式 #defineBOND_MODE_ACTI ...

  2. @Apiimplicitparam的paramType

    转自:https://swagger.io/docs/specification/describing-parameters/ Parameter Types OpenAPI 3.0 distingu ...

  3. Spring.Net的使用

    1.Spring.Net的简单介绍 spring.net 框架是微软效仿java中的spring框架而推出的一种在.net中使用的框架,它使用配置的方式实现逻辑的解耦,它的主要功能集成在Spring. ...

  4. JS生成GUID方法

    function GUID() { this.date = new Date(); /* 判断是否初始化过,如果初始化过以下代码,则以下代码将不再执行,实际中只执行一次 */ if (typeof t ...

  5. pta 习题集5-17 家谱处理

    人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究.实验中,使用计算机处理家谱.为了实现这个目的,研究人员将家谱转换为文本文件.下面为家谱文本文件的实例: John Robert F ...

  6. Oracle体系结构之Oracle10gR2体系结构-内存、进程

    oracle体系结构图1 oracle体系结构图2 用户进程(访问oracle的客户端的总称) 工具的使用:sqlplus.pl/sql developer 如何访问数据库: 本机直接通过sock方式 ...

  7. QQ 空间过滤器 for V8

    最近 QQ空间升级到 V8 版本,做了很大的调整, 我也做了升级,由于时间关系,功能暂时只有 模块过滤,其他过滤请等待后续更新,谢谢大家的支持! 刚刚上线,不知道你们能否看到 https://chro ...

  8. Django 的 Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 Form类的使用: 1.定义规则: from ...

  9. MR的shuffle和Spark的shuffle之间的区别

    mr的shuffle mapShuffle 数据存到hdfs中是以块进行存储的,每一个块对应一个分片,maptask就是从分片中获取数据的 在某个节点上启动了map Task,map Task读取是通 ...

  10. uva11090 Bellman-Ford 运用

    给定一一个n个点m条边的加权有向图, 平均值最小的回路. 二分答案,对于每个二分的mid 做一次Bellman-Fprd , 假设有k条边组成的回路. 回路上各条边的权值为  w1 , w2 ..wk ...