[题目来源]:NOIP2004
[关键字]:数学
[题目大意]:求2p-1的位数,和后500位。
//======================================================================
[分析]:求位数可以用数学方法求:N的位数=log10(N),所以2p-1的位数=log10(2p-1)=p*log10(2)。第二问要用高精度求出后500位就行,但是要用快速幂。
[代码]:
无第一问
View Code
1 program Project1;
2 const up = 10000;
3 type
4 rec = record
5 len: longint;
6 dat: array[0..1000] of longint;
7 end;
8 var
9 ans: rec;
10 p: longint;
11
12 procedure cheng(a, b: rec);
13 var
14 c: rec;
15 k, i, j, tot, len1, len2: longint;
16 begin
17 len1 := a.len;
18 len2 := b.len;
19 k := 0;
20 c.len := 0;
21 fillchar(c.dat,sizeof(c.dat),0);
22 for i := 1 to len1 do
23 begin
24 for j := 1 to len2 do
25 begin
26 c.dat[i+j-1] := c.dat[i+j-1]+k+a.dat[i]*b.dat[j];
27 k := c.dat[i+j-1] div up;
28 c.dat[i+j-1] := c.dat[i+j-1] mod up;
29 end;
30 tot := len2+i-1;
31 while k > 0 do
32 begin
33 inc(tot);
34 c.dat[tot] := c.dat[tot]+k;
35 k := c.dat[tot] div up;
36 c.dat[tot] := c.dat[tot] mod up;
37 end;
38 end;
39 if tot > 125 then tot := 125;
40 ans := c;
41 ans.len := tot;
42 end;
43
44 procedure work(p: longint);
45 var
46 i, k: longint;
47 begin
48 if p = 1 then exit;
49 work(p div 2);
50 cheng(ans,ans); //ok
51 if odd(p) then
52 begin
53 k := 0;
54 for i := 1 to ans.len do
55 begin
56 ans.dat[i] := ans.dat[i]*2+k;
57 k := ans.dat[i] div up;
58 ans.dat[i] := ans.dat[i] mod up;
59 end;
60 while k > 0 do
61 begin
62 inc(ans.len);
63 ans.dat[ans.len] := ans.dat[ans.len]+k;
64 k := ans.dat[ans.len] div up;
65 ans.dat[ans.len] := ans.dat[ans.len] mod up;
66 end;
67 end;
68 end;
69
70 procedure print;
71 var
72 i: longint;
73 begin
74 ans.dat[1] := ans.dat[1]-1;
75 i := 1;
76 while ans.dat[i] < 0 do
77 begin
78 ans.dat[i] := ans.dat[i]+up;
79 ans.dat[i+1] := ans.dat[i+1]-1;
80 inc(i);
81 end;
82 for i := 125 downto 1 do
83 begin
84 //if (125-i mod 50 = 0) then writeln;
85 if ans.dat[i] < 10 then begin write('000',ans.dat[i]); continue; end;
86 if ans.dat[i] < 100 then begin write('00',ans.dat[i]); continue; end;
87 if ans.dat[i] < 1000 then begin write('0',ans.dat[i]); continue; end;
88 write(ans.dat[i]);
89 end;
90 end;
91
92 begin
93 assign(input,'d:\1.in');reset(input);
94 assign(output,'d:\1.out');rewrite(output);
95 readln(p);
96 ans.len := 1;
97 ans.dat[1] := 2;
98 work(p);
99 print; //ok
100 close(input);
101 close(output);
102 end.