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. 配置maven使用nexus

    本文简单介绍使用配置maven使用nexus仓库,在团队中使用nexus,避免每个人都从中央仓库去下载依赖,节省带宽,提高下载速度,同时也减少了中央仓库的压力 配置在maven中使用nexus很简单( ...

  2. NAS设备是什么

    1.NAS含义: NAS(Network Attached Storage)网络存储基于标准网络协议实现数据传输,为网络中的Windows / Linux / Mac OS 等各种不同操作系统的计算机 ...

  3. 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP

    [BZOJ1630/2023][Usaco2007 Demo]Ant Counting 题意:T中蚂蚁,一共A只,同种蚂蚁认为是相同的,有一群蚂蚁要出行,个数不少于S,不大于B,求总方案数 题解:DP ...

  4. 获得当前正在显示的activity的类名

    需要加一个权限: <uses-permission android:name="android.permission.GET_TASKS"/> ActivityMana ...

  5. php 常用的系统函数

    字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...

  6. .NET中将中文符号转换成英文符号

    public static string ConvertToEn(string text) { const string s1 = ".:,?!.“”‘’"; const stri ...

  7. android.os.Handler

    android.os.handler A Handler allows you to send and process Message and Runnable objects associated ...

  8. Oracle Schema Objects——伪列ROWID Pseudocolumn(ROWNUM、ROWID)

    Oracle Schema Objects Oracle Schema Objects——Tables——Oracle Data Types Oracle伪列 在Oracle数据库之中为了实现完整的关 ...

  9. mysql float 浮点型

    定点数类型 DEC等同于DECIMAL 浮点类型:FLOAT DOUBLE 作用:存储薪资.身高.体重.体质参数等 m,d 都是设置宽度 =============================== ...

  10. python 面向对象 公有属性 用在哪里

    公有属性也可以叫做静态字段 如果每个对象都有一个共同的值 , 应该把它设置为公有属性 公有属性使用场景,每个对象中保存相同的东西时,可以使用公有属性 类找公有属性 过程