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. ASP.net导出EXCEL乱码?试试这个

    网上的方法有说加meta的,是有用,不过,不够直接, 甭管你asp文件是utf-8还是gb2312,加入下面两句就搞掂: response.Charset="GB2312" ses ...

  2. linux下svn服务搭建

    安装svn需要依赖apr和apr-util这两个软件,所以先安装这两个软件 下载安装APR wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz .ta ...

  3. Bootstrap的竞争对手Zurb Foundation

    Bootstrap并不是唯一的前端开发框架,比如还有JQuery UI.HTML5 Boilerplate等等.但对于Bootstrap来说,真正的竞争对手是Zurb Foundation.Boots ...

  4. C++学习(五)

    一.拷贝构造函数和拷贝赋值运算符1.拷贝构造:用一个已有的对象,构造和它同类型的副本对象——克隆.2.形如class X {  X (const X& that) { ... }};的构造函数 ...

  5. iis的路径

    每次打开iis管理器查看iis指定路径下的文件过于麻烦,而且iis管理器耗资源,以下是iis的路径,以及其对应在本地磁盘的地址 SP2013\Sites\SharePoint - 80\_contro ...

  6. 20160314 Request 和Response

    一.Response 1.Resonse的继承结构: ServletResponse--HttpServletResponse 2.Response代表响应,于是响应消息中的 状态码.响应头.实体内容 ...

  7. 再跟SQL谈一谈--基础篇

    1.简介 2.DDL & DML 3.SELECT ①DISTINCT ②WHERE ③AND & OR ④ORDER BY 4.INSERT 5.UPDATE 6.DELETE 1. ...

  8. Visual C++ 打印编程技术-内存设备环境

    1.内存设备环境 内存设备环境是一个没有设备与它联系的环境.一般利用与某个标准设备环境兼容的内存设备环境把一个位图复制到屏幕上去.为此可以先创建一个与某个标准设备环境兼容的内存设备环境,然后把所要显示 ...

  9. [firefly]暗黑源码解析

    一.架构 二.各模块详解 1.net 2.gate 3.game 4.db 三.启动 四.举例说明 五.性能测试

  10. C# 内存法图像处理

    内存法通过把图像储存在内存中进行处理,效率大大高于GetPixel方法,安全性高于指针法. 笔者当初写图像处理的时候发现网上多是用GetPixel方法实现,提到内存法的时候也没有具体实现,所以笔者在这 ...