Uva 10288 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 \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的更多相关文章
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- UVa 10288 - Coupons(数学期望 + 递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 10288 Coupons (分数模板)
https://vjudge.net/problem/UVA-10288 大街上到处在卖彩票,一元钱一张.购买撕开它上面的锡箔,你会看到一个漂亮的图案. 图案有n种,如果你收集到所有n(n≤33)种彩 ...
- UVA 10288 Coupons (概率)
题意:有n种纸片无限张,随机抽取,问平均情况下抽多少张可以保证抽中所有类型的纸片 题解:假设自己手上有k张,抽中已经抽过的概率为 s=k/n:那抽中下一张没被抽过的纸片概率为 (再抽一张中,两张中,三 ...
- UVA 10288 Coupons 彩票 (数学期望)
题意:一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最简的分数形式表示答案.n<=33. 思路:这题实在好人,n<=33.用longlong ...
- UVa 10288 (期望) Coupons
题意: 每张彩票上印有一张图案,要集齐n个不同的图案才能获奖.输入n,求要获奖购买彩票张数的期望(假设获得每个图案的概率相同). 分析: 假设现在已经有k种图案,令s = k/n,得到一个新图案需要t ...
- uva 10288 gailv
Problem F Coupons Input: standard input Output: standard output Time Limit: seconds Memory Limit: MB ...
- UVA 10288 Coupons---概率 && 分数类模板
题目链接: https://cn.vjudge.net/problem/UVA-10288 题目大意: 一种刮刮卡一共有n种图案,每张可刮出一个图案,收集n种就有奖,问平均情况下买多少张才能中奖?用最 ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- JDK 动态代理分析
Java的代理有两种:静态代理和动态代理,动态代理又分为 基于jdk的动态代理 和 基于cglib的动态代理 ,两者都是通过动态生成代理类的方法实现的,但是基于jdk的动态代理需要委托类实现接口,基于 ...
- warning(包括PHP中的notice)必须改吗?
作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...
- 在Windows下自动运行Modelsim
首先声明:该文章是在刘志伟老师的<Modelsim的Tcl命令>的基础上写的,希望我们能越来越自动化. 1.编写好源文件.包含asyn_fifo.v.fifomem.v.rptr_empt ...
- Java json设置时间格式,Jackson设置时间格式,json设置单引号
Java json设置时间格式,Jackson设置时间格式,json设置单引号 >>>>>>>>>>>>>>> ...
- js 如何判断数据是数据还是对象
如果用typeof测试,数组和对象都是显示的Object, 测试方式:var mycars=new Array();mycars[0]="Saab";mycars[1]=" ...
- ecshop在PHP 5.4以上版本各种错误问题处理
在php5.4版本之后有很多的函数与功能进行丢弃与升级功能了,现在国内很多CMS都还未按php5.4标准来做了,下面我整理了一些在ecshop在PHP 5.4以上版本各种错误问题处理. 1.PHP 5 ...
- Asp.net中前台javascript与后台C#交互
方法一:使用Ajax开发框架,后台方法定义前添加[AjaxPro.AjaxMethod],然后就可以在前台js脚本中调用后台C#函数. 方法二:后台方法声明为public或者protected,然后前 ...
- 20160322 javaweb 学习笔记--response 重定向
//一般方法 response.setStatus(302); response.setHeader("Location", "/20160314/index.jsp&q ...
- 使用PDO连接多种数据库
在PHP 5之前,想要连接MySQL数据库就需要使用mysql或mysqli等一系列函数来操作数据库.例如,我们使用mysql系列数据库函数进行查询操作,对应的示例代码如下: <?php //创 ...
- SetTimer的使用
SetTimer函数用于创建一个计时器,KillTimer函数用于销毁一个计时器.计时器属于系统资源,使用完应及时销毁. SetTimer的函数原型如下:UINT_PTR SetTimer( HWND ...