传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1099

Lottery

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4319    Accepted Submission(s): 1921

Problem Description
Eddy's company publishes a kind of lottery.This set of lottery which are numbered 1 to n, and a set of one of each is required for a prize .With one number per lottery, how many lottery 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 lottery 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.
 
Sample Input
2
5
17
 
Sample Output
3
5
11 --
12
340463
58 ------
720720
 
Author
eddy
 
题目意思:
 
每次发行n张彩票,要买多少张才能集齐。
 
分析:
假如发行1张,买1次就集齐了。所以买1张。
假如发行2张,第一次买的序号是1,第二次买中剩下那张的概率是1/2,所以要买两张才能买到第二张,所以要买3张才能才能集齐。
假如发行3张,第一次发的序号是1,要买1张,第二次买中剩下的两张之一的概率是2/3,所以要买3/2张,第三次买剩中最后一张的概率是1/3,所以要买3张,所以要买5+1/2张。
假如发行n张,第一次买中没买过的概率是1,第二次是n-1/n,第三次是n-2/n,第n次是1/n,
而对应需要买的张数是第一次买1张,第二次买n/n-1张,第三次买n/n-2,第n次买n张,所以求的是n/n,n/n-1,……1/n的和。
 
所以就是求:n(1+1/2+1/3+......+1/n)
 
举个例子,当n=5时,第一张有用的概率为1,买一张就行了,第二张有用的概率为4/5,所以买5/4张彩票能买上对你有用的,一次类推,sum=1+5/4+5/3+5/2+5/1=11…5/12,
 
需要注意的就是格式问题:(这个很重要,w了几次)
结果是整数的话,直接输出整数
结果不是整数的话,分为两部分输出,一个整数,和一个真分数
格式请参考代码,讲起来太麻烦了。。。
code:
 
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define max_v 25
struct node
{
LL molecule;//分子
LL Denominator;//分母
};
LL gcd(LL a,LL b)//最大公约数
{
if(b==)
return a;
return gcd(b,a%b);
}
LL lcm(LL a,LL b)//最小公倍数
{
return a/gcd(a,b)*b;
}
LL numlen(LL x)//数字长度
{
LL c=;
while(x)
{
x=x/;
c++;
}
return c;
}
node f(int n)
{
node p;
p.molecule=;
p.Denominator=;
if(n==)
return p;
for(LL i=;i<=n;i++)
{
LL x=lcm(i,p.Denominator);
p.molecule=p.molecule*(x/p.Denominator)+(x/i);
p.Denominator=x;
LL y=gcd(p.Denominator,p.molecule);
p.Denominator=p.Denominator/y;
p.molecule=p.molecule/y;
// printf("fz=%I64d fm=%I64d 最小公倍数=%I64d\n",p.molecule,p.Denominator,x);
}
p.molecule=p.molecule*n;
LL y=gcd(p.Denominator,p.molecule);
p.Denominator=p.Denominator/y;
p.molecule=p.molecule/y; return p;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
node p=f(n);
if(p.molecule%p.Denominator==)
printf("%I64d\n",p.molecule/p.Denominator);
else
{
LL x=p.molecule/p.Denominator;
p.molecule=p.molecule-(x*p.Denominator);
LL y=gcd(p.Denominator,p.molecule);
p.Denominator=p.Denominator/y;
p.molecule=p.molecule/y; int l1=numlen(x);
int l2=numlen(p.Denominator);
for(int i=;i<=l1;i++)
printf(" "); printf("%I64d\n",p.molecule);
printf("%I64d ",x);
for(int i=;i<=l2;i++)
printf("-");
printf("\n");
for(int i=;i<=l1;i++)
printf(" ");
printf("%I64d\n",p.Denominator);
}
}
return ;
}

HDU 1099 Lottery (求数学期望)的更多相关文章

  1. hdu 1099 Lottery

    这是我第一次写博客,作为一个ACMer,经常进别人的博客,所以自己也想写写博客. HDU 1099 Lottery Time Limit: 2000/1000 MS (Java/Others)     ...

  2. HDU 4465 Candy (数学期望)

    题意:有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖.直到有一天打开盒子一看,这个盒子没有糖了.输入n,p,求此时另一个盒子里糖的个数的数学期望 ...

  3. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  4. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

  5. [CF912D]Fishes - 求数学期望,乱搞

    D. Fishes time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  6. uva 10828 高斯消元求数学期望

    Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name ...

  7. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  8. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  9. HDU 5984 数学期望

    对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...

随机推荐

  1. lua "诡异"的return用法

    https://yq.aliyun.com/articles/11387 lua "诡异"的return用法   德哥 2016-03-29 15:38:42 浏览5690 评论0 ...

  2. python制作 whl 源文件,并制作本地pip源

    制作whl 1.创建用于存放wheel文件目录 mkdir wheels 2.安装wheel库 pip install  wheel 3.进入wheels目录 cd wheels 4.使用pip wh ...

  3. Bootstrap table使用知识-转

    http://www.cnblogs.com/landeanfen/p/5005367.html 官方文档:http://bootstrap-table.wenzhixin.net.cn/zh-cn/ ...

  4. 深入理解JavaScript系列(16):闭包(Closures)

    介绍 本章我们将介绍在JavaScript里大家经常来讨论的话题 —— 闭包(closure).闭包其实大家都已经谈烂了.尽管如此,这里还是要试着从理论角度来讨论下闭包,看看ECMAScript中的闭 ...

  5. 探寻hashmap

    Hashmap源码 1.  构造器: a)获得默认数组大小:1>>4 :16 b) 获得负载因子:0.75:衡量hashmap的空间使用程度 i.过大:使用空间更加充分,但是查找效率变低, ...

  6. 进程和程序(Process and Program)

    原出处:http://oss.org.cn/kernel-book/ch04/4.1.htm ----------------------------------个人理解分割线------------ ...

  7. python中的字符串 列表 字典

    字符串     一个有序的字符集合  不可变 1,可以使用for in语句进行迭代循环,返回元素    2,in类是于str.find()方法但是是返回布尔结果        str.find()返回 ...

  8. 提取url中参数的方法(转换成json格式)

    还是直接上代码吧. //将url中的参数获取到并抓换成json格式 function serilizeUrl(url){ var urlObject={}; //1.正则匹配是不是以?结尾 if(/\ ...

  9. window.frames在不同浏览器中的用法

    document.frames 等同于 window.frames,用来取得当前页面内 window 对象的集合. 不支持Firefox,其他浏览器(chrome.opera.IE.360)均支持. ...

  10. 杂谈spring、springMVC

    一.背景 目前项目组都在用SSM(spring+springMVC+mybatis)开发项目 大家基本都停留在框架的基本使用阶段,对框架的职责并不清晰,导致配置文件出现了不少问题 在这简单讲解一下sp ...