码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ1099-Lottery(数学期望 + 模拟)

时间:2017-06-01 21:40:57      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:==   gcd   clu   form   long   lin   tin   ace   line   

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

 技术分享

Code:

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 int n;
 7 
 8 int main()
 9 {
10     while(~scanf("%d" , &n))
11     {
12         if(n == 1)
13         {
14             puts("1");
15             continue;
16         }
17 
18         LL pre = __gcd(1 , 2);
19         LL lc = 2 / pre;
20 
21         for(LL i = 3 ; i <= n ; ++i)
22         {
23             LL tp = __gcd(lc , i);
24             lc = lc  / tp * i;
25         }
26 
27         pre = 0;
28 
29         for(int i = 1 ; i <= n ; ++i)
30         {
31             pre += (lc / i) * n;
32         }
33 
34         LL ac = __gcd(lc , pre);
35 
36         LL zheng = pre / lc;
37 
38 
39 
40         if(pre % lc != 0)
41         {
42             int zhengspace = 0;
43             int fenspace1 = 0;
44             int fenspace2 = 0;
45             LL tp = zheng;
46             while(tp)
47             {
48                 ++zhengspace;
49                 tp /= 10;
50             }
51 
52             for(int i = 0 ; i <= zhengspace ; ++i)
53                 printf(" ");
54 
55             LL shang = pre - zheng * lc;
56             LL g = __gcd(shang , lc);
57             LL xia = lc / g;
58             shang /= g;
59 
60             printf("%lld\n" , shang);
61 
62             tp = shang;
63             while(tp)
64             {
65                 ++fenspace1;
66                 tp /= 10;
67             }
68 
69             tp = xia;
70             while(tp)
71             {
72                 ++fenspace2;
73                 tp /= 10;
74             }
75 
76             printf("%lld " , zheng);
77 
78             int gang = max(fenspace1 , fenspace2);
79 
80             for(int i = 1 ; i <= gang ; ++i)
81             {
82                 printf("-");
83             }
84 
85             printf("\n");
86 
87             for(int i = 0 ; i <= zhengspace ; ++i)
88                 printf(" ");
89 
90             printf("%lld\n" , xia);
91         }
92         else
93         {
94             printf("%lld\n" , zheng);
95         }
96     }
97 }
View Code

 

HDOJ1099-Lottery(数学期望 + 模拟)

标签:==   gcd   clu   form   long   lin   tin   ace   line   

原文地址:http://www.cnblogs.com/jianglingxin/p/6930786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!