博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ2625][UVA10288]Coupons
阅读量:5309 次
发布时间:2019-06-14

本文共 2578 字,大约阅读时间需要 8 分钟。

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 
#include
#include
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);}

转载于:https://www.cnblogs.com/Alessandro/p/9580239.html

你可能感兴趣的文章
MySQL基础3
查看>>
云计算数据与信息安全防护
查看>>
全局设置导航栏
查看>>
RxJS & Angular
查看>>
面向对象(多异常的声明与处理)
查看>>
MTK笔记
查看>>
ERROR: duplicate key value violates unique constraint "xxx"
查看>>
激活office 365 的启动文件
查看>>
9款免费的Windows远程协助软件
查看>>
Maven(八) Maven项目和testng结合应用
查看>>
iOS 的 set.get.构造方法
查看>>
无法根据中文查找
查看>>
文件编码,文件或文件名编码格式转换(转)
查看>>
[简讯]phpMyAdmin项目已迁移至GitHub
查看>>
redis的hash与string区别
查看>>
转载 python多重继承C3算法
查看>>
初用Ajax
查看>>
zabbix 2.2.20 安装详解(Centos6.9)
查看>>
【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
查看>>
css文本溢出显示省略号
查看>>