传送门:

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. C++11并发编程:原子操作atomic

    一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一 ...

  2. GridView, ListView 区别

    ListView, GridView部分的类层次结构 AbsListView的xml属性 android:listSelector 当前item高亮时,显示的drawable android:draw ...

  3. php之mongodb插入数据后如何返回当前插入记录ID

    <?php /** *插入记录 *参数: *$table_name:表名 *$record:记录 * *返回值: *成功:true *失败:false */ function insert($t ...

  4. attribute和property的区别

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  5. [转]Mysql几种索引类型的区别及适用情况

    此为转载文章,仅做记录使用,方便日后查看,原文链接:https://www.cnblogs.com/yuan-shuai/p/3225417.html Mysql几种索引类型的区别及适用情况   如大 ...

  6. SpringBoot ------ 使用AOP处理请求

    一.AOP统一处理请求日志 1.spring的两大核心:AOP ,  IOC 2.面向对象OOP关注的是将需求功能垂直,划分为不同的,并且相对独立的,   会封装成良好的类,并且类有属于自己的行为. ...

  7. vue授权页面登陆之后返回之前的页面

    import Vue from 'vue'import Router from 'vue-router'Vue.use(Router)import home from "@/pages/ho ...

  8. Linux虚拟系统安装——Ubuntu18.04 & CentOS6.5

    Linux虚拟系统安装--Ubuntu18.04 & CentOS6.5 摘要:Linux简介.虚拟系统安装.系统备份与文件介绍 1. Linux简介 (1)1968年,MIT.Bell实验室 ...

  9. String字符串操作

    char chars[] ={'a','b','c'}; String s = new String(chars); int len = s.length();//字符串长度 System.out.p ...

  10. 学习JVM虚拟机原理总结

    0x00:JAVA虚拟机的前世今生 1991年,在Sun公司工作期间,詹姆斯·高斯林和一群技术人员创建了一个名为Oak的项目,旨在开发运行于虚拟机的编程语言,允许程序多平台上运行.后来,这项工作就演变 ...