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 \le n \le 33\), giving the size of the set of coupons. Input is terminated by end of file.

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 output.

Sample Input

2

5

17

Sample Output

3

5

11 --

12

340463

58 ------

720720

若当前已有\(k\)种Coupons,那么获得新的Coupons的概率为\(p = \frac{n-k}{n}\),所以获得一种新Coupons的期望步数为$$p+2p(1-p)+3p(1-p)^2+\cdots$$

用错位相消+无穷等比数列求和数列公式,化简得\(\frac{n}{n-k}\),所以$$ans = n\sum_{i = 1}^{n}\frac{1}{i}$$

#include<cstdio>
#include<cstdlib>
using namespace std; typedef long long ll;
int N; inline ll gcd(ll a,ll b) { if (!b) return a; return gcd(b,a%b); }
inline int ws(ll a) { int ret = 0; while (a) a /= 10,++ret; return ret; } struct node
{
ll a,b,c;
inline node() { c = 1; } inline void update()
{
ll g = gcd(b,c); b /= g,c /= g;
a += b/c; b %= c;
} friend inline node operator + (const node &x,node &y)
{
node ret; y.update();
ret.a = x.a+y.a; ret.c = x.c*y.c/gcd(x.c,y.c);
ret.b = x.b*(ret.c/x.c)+y.b*(ret.c/y.c);
ret.update(); return ret;
} friend inline node operator *(const node &x,const int &y)
{
node ret; ret.a = x.a*y;
ll g = gcd(x.c,y); ret.b = (y/g)*x.b; ret.c = x.c/g;
ret.update(); return ret;
} inline void print()
{
if (!b) printf("%lld\n",a);
else
{
int t = ws(a);
for (int i = t+1;i--;) putchar(' ');
printf("%lld\n",b);
printf("%lld ",a);
for (int i = ws(c);i--;) putchar('-');
puts("");
for (int i = t+1;i--;) putchar(' ');
printf("%lld\n",c);
}
}
}ans; int main()
{
freopen("10288.in","r",stdin);
freopen("10288.out","w",stdout);
while (scanf("%d\n",&N) != EOF)
{
ans.a = ans.b = 0; ans.c = 1;
for (int i = 1;i <= N;++i)
{
node tmp; tmp.a = 0,tmp.b = 1,tmp.c = i;
ans = ans + tmp;
}
ans = ans * N; ans.print();
}
fclose(stdin); fclose(stdout);
return 0;
}

Uva 10288 Coupons的更多相关文章

  1. UVA 10288 - Coupons(概率递推)

    UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...

  2. UVa 10288 - Coupons(数学期望 + 递推)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. uva 10288 Coupons (分数模板)

    https://vjudge.net/problem/UVA-10288 大街上到处在卖彩票,一元钱一张.购买撕开它上面的锡箔,你会看到一个漂亮的图案. 图案有n种,如果你收集到所有n(n≤33)种彩 ...

  4. UVA 10288 Coupons (概率)

    题意:有n种纸片无限张,随机抽取,问平均情况下抽多少张可以保证抽中所有类型的纸片 题解:假设自己手上有k张,抽中已经抽过的概率为 s=k/n:那抽中下一张没被抽过的纸片概率为 (再抽一张中,两张中,三 ...

  5. UVA 10288 Coupons 彩票 (数学期望)

    题意:一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最简的分数形式表示答案.n<=33. 思路:这题实在好人,n<=33.用longlong ...

  6. UVa 10288 (期望) Coupons

    题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t ...

  7. uva 10288 gailv

    Problem F Coupons Input: standard input Output: standard output Time Limit: seconds Memory Limit: MB ...

  8. UVA 10288 Coupons---概率 && 分数类模板

    题目链接: https://cn.vjudge.net/problem/UVA-10288 题目大意: 一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最 ...

  9. codeforces754D Fedor and coupons

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

随机推荐

  1. 关于Xcode7中的tbd文件

    tbd 是 text-based stub libraries的意思, 是苹果在Xcode7中使用的一个技术,便于减少Xcode7中SDK的体积. 下面讲解下Xcode7如何通过tbd这个技术减少SD ...

  2. 神奇的 BlocksKit(1):源码分析(上)

    高能预警:本篇文章非常长,因为 BlocksKit 的实现还是比较复杂和有意的.这篇文章不是为了剖析 iOS 开发中的 block 的实现以及它是如何组成甚至使用的,如果你想通过这篇文章来了解 blo ...

  3. 基于Windows的套接字相关函数及示例

    链接ws2_32.lib库 头文件#include <winsock2.h> int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSADa ...

  4. JQuery设置input属性(disabled、enabled)

    document.getElementById("removeButton").disabled = false; //普通Js写法 $("#removeButton&q ...

  5. ASP与ASP.NET转换Session数据桥的应用

    背景: 现有公司的产品OA是采用ASP早先的技术开发,需要与目前最新的ASP.NET产品进行数据交互的应用.现有的ASP应用程序往往采用“ASP Sessions”,这是一种经典的ASP内置模式,即允 ...

  6. Git 常用配置和使用

    Git:是一个分布式的源代码管理工具,Linux内核的代码就是用Git管理的所以它很强,也很快, 和 Vss/SVN比起来 本地Git初始化配置及其使用: 1. 初始化本地Git库:打开Git Bas ...

  7. Android应用Icon大小在不同分辨率下定义

    http://www.ard9.com/gsjj/204.html 对于Android平台来说,不同分辨率下Icon的大小设计有着不同的要求,对于目前主流的 HDPI即WVGA级别来说,通常hdpi的 ...

  8. MVC小系列(十四)【MVC+ZTree大数据异步树加载】

    ZTree是一个jquery的树插件可以异步加载 第一步定义一个标准的接口(指的是与ztree默认的数据元素保持一致) /// <summary> /// ZTree数据结构 /// &l ...

  9. cognos 10.2.2 导入samples数据源报错解决

    操作系统:windows 2008R2 X64 数据库:oracle 10.2.0.1 X32 sid:cognosdb86 安装完samples后,执行IBM安装目录webcontent\sampl ...

  10. 注册一个比较大小Handlebars

    1.显示的数据 var datas = { id:"number" } 2.temp模板 <script id="template" type=" ...