[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 = 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的更多相关文章
- UVa10288 Coupons 分数模版
UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...
- uva10288 Coupons 【概率 分数】
题目: 题意: 一共n种不同的礼券,每次得到每种礼券的概率相同.求期望多少次可以得到所有n种礼券.结果以带分数形式输出.1<= n <=33. 思路: 假设当前已经得到k种,获得新的一种的 ...
- 「SHOI2002」「LuoguP1291」百事世界杯之旅(UVA10288 Coupons)(期望,输出
题目描述 “……在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽奖活动,获得球星背包,随声听,更克赴日韩观看世界杯.还不赶 ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Uva 10288 Coupons
Description Coupons in cereal boxes are numbered \(1\) to \(n\), and a set of one of each is require ...
- Coupons
uva10288:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
- Coupons and Discounts
Coupons and Discounts time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- kubernetes 创建tomcat 容器
方案一: 使用k8s dashboard 创建rc 1. 界面操作 提示:暂时 忽略 查看: 2.测试 由于是外部服务 直接用 节点的ip访问: 同样也是 第二个端口可以访问.感觉 跟之前的提 ...
- 查询hadoop参数变量
[hadoop@master hadoop]$ hive -S -e 'set -v'|grep querylog|grep -E -v 'CLASSPATH|class'hive.querylog. ...
- UNIX的插头问题
UNIX的插头问题 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 你负责为联合国互联网执行组织(UNIX)的周年会议布置会议室. ...
- log4j输出多个自定义日志文件(转)
如果在实际应用中需要输出独立的日志文件,怎样才能把所需的内容从原有日志中分离,形成单独的日志文件呢? 先看一个常见的log4j.properties文件,它是在控制台和test.log文件中记录日志: ...
- tensorflow和python操作中的笔记
前一段时间做了一些项目,把一些笔记放在了txt中,现分享出来,自己也能够时长预习. 1) 读取文件时,将固定的文件地址,采用数组或者字符串的形式,提前表示出来,后期使用时候采用拼接操作 2) # 得到 ...
- MySQL 慢查询日志工具之pt-query-digest
1. 工具简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tc ...
- 点击劫持漏洞解决( Clickjacking: X-Frame-Options header missing)
点击劫持漏洞 X-Frame-Options HTTP 响应头, 可以指示浏览器是否应该加载一个 iframe 中的页面. 网站可以通过设置 X-Frame-Options 阻止站点内的页面被其他页面 ...
- nodejs Async详解之三:集合操作
Async提供了很多针对集合的函数,可以简化我们对集合进行异步操作时的步骤.如下: forEach:对集合中每个元素进行异步操作 map:对集合中的每个元素通过异步操作得到另一个值,得到新的集合 fi ...
- Flask系列(七)Flask中的wtforms使用
一.简单介绍flask中的wtforms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装: pip3 install wtforms 二.简单使用wtfo ...
- spark mllib和ml类里面的区别
mllib是老的api,里面的模型都是基于RDD的,模型使用的时候api也是有变化的(model这里是naiveBayes), (1:在模型训练的时候是naiveBayes.run(data: RDD ...