首页 > 代码库 > HDOJ1099-Lottery(数学期望 + 模拟)
HDOJ1099-Lottery(数学期望 + 模拟)
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
2517
Sample Output
3 511 -- 12 34046358 ------ 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 else93 {94 printf("%lld\n" , zheng);95 }96 }97 }
HDOJ1099-Lottery(数学期望 + 模拟)