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/ ...
随机推荐
- day-3
/* 快noip了!!!感觉还有好多事要做 上午考试 原题没做 自己找了套题 挺水的 T1模拟(然而没认真读题 90) T2 dp+简单优化 数据有点问题 T3 暴力状丫 然而写丑了 60分的要两秒多 ...
- php-fpm 的安装与LNMP测试
1. 下载并安装PHP 1.1 编译安装 1.1.1 安装前的准备 下载并安装libmcrypt 和 libmcrypt-devel[root@test /opt/soft]# wget ftp:// ...
- js 获取当前时间格式怎么转换?
toLocaleDateString() 得到的时间是 yyyy年MM月dd日 HH:ss:mm 格式的,怎么转换成yyyy-MM-dd HH:ss:mm 在js里面 仅针对这个问题来说,不需要那么大 ...
- PostgreSQL 修改列报错:cannot be cast automatically to type integer
如果你直接使用可视化工具修改一个varchar字段为int类型的时候,可能会报错, 这里就需要自己去写一个语句去修改了 调整执行语句:ALTER TABLE table_name ALTER COLU ...
- Unity3D 之3D游戏角色控制器运动
3D运动,绑定了人形控制器后的一个简单的运动方法. using UnityEngine; using System.Collections; public class PlayerMove : Mon ...
- Orchard 学习-手动安装Orchard
通过Orchard zip 文件手动配置网站 这篇文章将引导你如果通过Zip文件来安装Orchard. 我们会使用三种不同的方法来承载Orchard: IIS. WebMatrix and IIS E ...
- C# 操作数据库的几种方式(数据库使用SQL SERVER2008)
一:通过常规 T-SQL 语句 (只写删除操作,其他同理) string strConn = ConfigurationManager.ConnectionStrings["SiteConn ...
- JS修改JSON中key的方法
function modifyJosnKey(json,oddkey,newkey){ var val=json[oddkey]; delete json[oddkey]; json[newkey]= ...
- 03_JqueryAjax_异步请求Servlet
[Ajax 简述] jquery对Ajax提供了更方便的代码:$ajax({ops})来发送异步请求. 首先说一个Ajax的特性,它是永安里发送异步请求,请求的是服务器,但不会刷新页面. 例如在注册功 ...
- 计数排序之python 实现源码
old = [2, 5, 3, 0, 2, 3, 0, 3] new = [0, 0, 0, 0, 0, 0] for i in range(len(old)): new[old[i]] = new[ ...