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. tomcat配置JMX

    最近看JDK的命令行工具,使用Java VisualVM和Jconsole工具都可以监控java程序的运行情况(包括CUP和内存等的使用情况,线程的运行状态等) 在Java VisualVM 工具里可 ...

  2. Android通知栏的高度获取

    public static int getStatusBarHeight(Context context){ Class<?> c = null; Object obj = null; F ...

  3. java中的socket编程

    Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一.如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都是基于Socket实现的.本文会介绍一下基于TCP/IP的S ...

  4. OC开发_Storyboard——AutoLayout

    一.autolayout 自动布局: 1. 设置所有视图框架的三种方法,可以通过代码创建也可以storyboard设置 = 规则 (1 蓝线+约束:(位置) 使用蓝线,根据蓝线拖动控件,只是告诉Xco ...

  5. Use of ‘const’ in Functions Return Values

    Use of 'Const' in Function Return Values 为什么要在函数的返回值类型中添加Const? 1.Features Of the possible combinati ...

  6. Python将科学计数法数值转换为指定精度浮点数

    Python将科学计数法数值转换为指定精度浮点数 In [20]:money = 1190000.0 In [21]: traded_maket_value = 13824000000 In [22] ...

  7. 在一台server上部署多个Tomcat

    版权声明: https://blog.csdn.net/u011518709/article/details/27181665 在一台server上配置多个Tomcat的方法: 这几天因为在研究OGS ...

  8. python用pyinstaller打包成exe文件

    版本为Python2.7 一.安装Pyinstaller 1.安装pywin32 下载安装文件:查找到跟自己适用的python版本及window系统版本匹配的pywin32,下载后安装  使用pip命 ...

  9. 003-and design-在create-react-app项目中使用antd

    一.概述 create-react-app 是业界最优秀的 React 应用开发工具之一,本文会尝试在 create-react-app 创建的工程中使用 antd 组件,并自定义 webpack 的 ...

  10. android 的 ExpandableListView Example Tutorial

    https://www.journaldev.com/9942/android-expandablelistview-example-tutorial Welcome to Android Expan ...