[题目来源]:NOIP基础题目集
[关键字]:数学
[题目大意]:每个瓶盖上有一个球星的名字,有N个不同的球星,平均情况下,要买多少瓶饮料才能集齐所有名字。
//===========================================================================================================
[分析]:对于要在剩下的 i 种瓶盖中收集到一种,买一瓶饮料收集到的概率为(i/n),所以平均应该买(n/i)瓶,所以平均总共要买 n(1/1+1/2+1/3+….+1/n)瓶。
[代码]:
View Code
1 program Project1;
2 var
3 n, lcm, fz: qword;
4
5 function gcd(x, y: qword):qword;
6 var
7 t: qword;
8 begin
9 if x < y then
10 begin
11 t := x;
12 x := y;
13 y := t;
14 end;
15 while y <> 0 do
16 begin
17 t := x mod y;
18 x := y;
19 y := t;
20 end;
21 exit(x);
22 end;
23
24 procedure work;
25 var
26 i: longint;
27 t: qword;
28 s: string;
29 begin
30 lcm := 2;
31 for i := 3 to n do
32 lcm := trunc(i*(lcm/gcd(i,lcm)));
33 fz := 0;
34 for i := 1 to n do
35 inc(fz,lcm div i);
36 fz := fz*n;
37 t := gcd(fz,lcm);
38 fz := fz div t;
39 lcm := lcm div t;
40 //writeln(fz,'/',lcm);
41 if fz mod lcm = 0 then
42 begin write(fz div lcm); exit; end;
43 s := '';
44 str(fz div lcm,s);
45 t := length(s);
46 for i := 1 to t do write('');
47 s := '';
48 str(lcm,s);
49 writeln(fz mod lcm);
50 write(fz div lcm);
51 for i := 1 to length(s) do write('-');
52 writeln;
53 for i:= 1 to t do write('');
54 writeln(lcm);
55 end;
56
57 begin
58 assign(input,'d:\in.txt');reset(input);
59 assign(output,'d:\out.txt');rewrite(output);
60 readln(n);
61 work;
62 close(input);
63 close(output);
64 end.