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. 关于word文档转成html网页的方法

    在工作中,有时我们可能需要将一个word文档转换成html网页格式,如在写帮助文档的时候,采用office编写,最终却想以网页的格式传到网站的指定目录下供网友直接浏览 这时我们就需要对word文件进行 ...

  2. Python老王视频习题答案

    基础篇2:一切变量都是数据对象的引用sys.getrefcount('test') 查看引用计数变量命名不能以数字开头编码:ascii.unicode.utf-81.阅读str对象的help文档,并解 ...

  3. Android中textView自动识别电话号码,电子邮件,网址(自动加连接)

    extends:http://blog.csdn.net/wx_962464/article/details/8471195 其实这个是很简单的,在android中已经为我们实现了,但是我估计很多人都 ...

  4. Swift - 触摸事件响应机制(UiView事件传递)

    import UIKit class FatherView: UIView { override func hitTest(point: CGPoint, withEvent event: UIEve ...

  5. 微信小程序 --- 事件绑定

    事件类别: tap:点击事件: longtap:长按事件: touchstart:触摸开始: touchend:触摸结束: touchcansce:取消触摸: 事件绑定: bind绑定: catch绑 ...

  6. vue - 计算属性、表单输入绑定

    计算属性 computed:{} <!DOCTYPE html> <html> <head> <title></title> </he ...

  7. 一道题浅析 i++,++i,i+1及(引用)&i的区别

    我们可能很清楚i++,++i和i+1级&i的概念,但在实际运用中我们就有可能很容易搞混淆.特别是在递归中区别它们就显得尤为重要了.那首先我们先看一段利用递归逆序字符串的代码,你能回答出这段代码 ...

  8. 8.Git撤销修改

    有一个文件内容如下: $ cat README.md the first ... the second ... the third ... - 文件自修改后还没有被放到暂存区,现在,撤销修改就回到和版 ...

  9. Tomcat安装(linux系统)

    首先解压: 进入配置文件: 统一tomcat的编码,避免乱码的问题: 保存退出. 注意: 查看打开的端口: /etc/init.d/iptables status 开启端口: iptables -A ...

  10. 剑指offer总结

    1.实现Singleton模式 2.二维数组中的查找:每行从左到右递增,每列从上到下递增,输入一个数,判断数组中是否存在该数 1 2  8 9  2 4  9 12 4 7 10 13  6 8 11 ...