vj p1032题解
此题就是考你的高精熟练度!
首先,要作出一个很简单的递推判断:
从个位开始找循环,后两位的循环节必然为后一位的整数倍,依此类推,找出后K位的循环节。
于是思路就很简明了。
有三点需要注意:
1.读入的时候只须记录后k位,运算过程中也只须记录后k位,所以此题最多是10进制下100位的高精。
2.记录结果的时候要注意,结果可能是高精,所以不光要设置一个不记录长度的伪*高精,还要记录一个真*高精,用于输出。
3.如果自乘超过10次,就找不到循环了....具体为何,说实话,我说不清楚,但是大家想想9的n次方,也许能领悟到那么些东西。
好,代码如下:
1 type
2 arra=record
3 da:array[0..210]of longint;
4 l:longint;
5 end;
6 var
7 k:longint;n,r:arra;
8 function min(a,b:longint):longint;
9 begin
10 if a<b then exit(a) else exit(b);
11 end;
12 procedure init;
13 var
14 s,s1,s2:string;
15 t,q,c,b,i:longint;
16 begin
17
18 readln(s);
19 fillchar(n,sizeof(n),0);
20 t:=pos(' ',s);
21 val(copy(s,t+1,length(s)-t),k);
22 s1:=copy(s,1,t-1);
23 if k>length(s1) then q:=length(s1) else q:=k;
24 for i:=1 to q do
25 begin
26 n.da[i]:=ord(s1[length(s1)-i+1])-ord('0');
27 end;
28 n.l:=k;
29 fillchar(r,sizeof(r),0);
30 for i:=1 to n.l do r.da[i]:=n.da[i];
31 r.l:=k;
32 while (r.da[r.l]=0)and(r.l>1) do dec(r.l);
33 end;
34 function check(a,r:arra;k:longint):boolean;
35 var
36 i:longint;
37 begin
38 for i:=1 to k do
39 begin
40 if a.da[i]<>r.da[i] then exit(false);
41 end;
42 exit(true);
43 end;
44 procedure mult(a,b:arra;var r:arra);
45 var
46 i,j:longint;c:arra;
47 begin
48 fillchar(r,sizeof(r),0);
49 fillchar(c,sizeof(c),0);
50 for i:=1 to min(a.l,k) do
51 for j:=1 to min(b.l,k) do
52 begin
53 c.da[i+j-1]:=a.da[i]*b.da[j]+c.da[i+j-1];
54 if c.da[i+j-1]>=10 then
55 begin
56 c.da[i+j]:=(c.da[i+j-1] div 10)+c.da[i+j];
57 c.da[i+j-1]:=c.da[i+j-1] mod 10;
58 end;
59 end;
60 for i:=1 to k do r.da[i]:=c.da[i];
61 r.l:=k;
62 end;
63 procedure accmult(a:arra;b:longint;var c:arra);
64 var
65 i:longint;
66 begin
67 fillchar(c,sizeof(c),0);
68 c.l:=a.l+2;
69 for i:=1 to c.l do
70 begin
71 c.da[i]:=a.da[i]*b+c.da[i];
72 if c.da[i]>=10 then
73 begin
74 c.da[i+1]:=(c.da[i] div 10)+c.da[i+1];
75 c.da[i]:=c.da[i] mod 10;
76 end;
77 end;
78 while (c.da[c.l]=0)and(c.l>1) do dec(c.l);
79 end;
80 procedure find;
81 var
82 i,j,times:longint;b:boolean;c,m,d,z:arra;
83 begin
84
85 fillchar(c,sizeof(c),0);
86 m:=n;
87 c.l:=k;
88 fillchar(z,sizeof(z),0);
89 z.l:=1;z.da[1]:=1;
90 if (r.l=1)and(r.da[1]=0) then
91 begin
92 writeln(1);
93 halt;
94 end
95 else
96 for i:=1 to k do
97 begin
98
99 times:=1;
100 repeat
101 c:=r;
102 mult(r,n,r);
103 mult(c,m,d);
104 inc(times);
105 b:=check(m,d,i);
106 until (b)or(times>10);
107 if b then
108 begin
109 accmult(z,times-1,z);
110 n:=c;
111 r:=c;
112 n.l:=k;
113 end
114 else
115 begin
116 writeln(-1);
117 halt;
118 end;
119 end;
120 for i:=z.l downto 1 do write(z.da[i]);
121
122 end;
123 begin
124 init;
125 find;
126 end.
2 arra=record
3 da:array[0..210]of longint;
4 l:longint;
5 end;
6 var
7 k:longint;n,r:arra;
8 function min(a,b:longint):longint;
9 begin
10 if a<b then exit(a) else exit(b);
11 end;
12 procedure init;
13 var
14 s,s1,s2:string;
15 t,q,c,b,i:longint;
16 begin
17
18 readln(s);
19 fillchar(n,sizeof(n),0);
20 t:=pos(' ',s);
21 val(copy(s,t+1,length(s)-t),k);
22 s1:=copy(s,1,t-1);
23 if k>length(s1) then q:=length(s1) else q:=k;
24 for i:=1 to q do
25 begin
26 n.da[i]:=ord(s1[length(s1)-i+1])-ord('0');
27 end;
28 n.l:=k;
29 fillchar(r,sizeof(r),0);
30 for i:=1 to n.l do r.da[i]:=n.da[i];
31 r.l:=k;
32 while (r.da[r.l]=0)and(r.l>1) do dec(r.l);
33 end;
34 function check(a,r:arra;k:longint):boolean;
35 var
36 i:longint;
37 begin
38 for i:=1 to k do
39 begin
40 if a.da[i]<>r.da[i] then exit(false);
41 end;
42 exit(true);
43 end;
44 procedure mult(a,b:arra;var r:arra);
45 var
46 i,j:longint;c:arra;
47 begin
48 fillchar(r,sizeof(r),0);
49 fillchar(c,sizeof(c),0);
50 for i:=1 to min(a.l,k) do
51 for j:=1 to min(b.l,k) do
52 begin
53 c.da[i+j-1]:=a.da[i]*b.da[j]+c.da[i+j-1];
54 if c.da[i+j-1]>=10 then
55 begin
56 c.da[i+j]:=(c.da[i+j-1] div 10)+c.da[i+j];
57 c.da[i+j-1]:=c.da[i+j-1] mod 10;
58 end;
59 end;
60 for i:=1 to k do r.da[i]:=c.da[i];
61 r.l:=k;
62 end;
63 procedure accmult(a:arra;b:longint;var c:arra);
64 var
65 i:longint;
66 begin
67 fillchar(c,sizeof(c),0);
68 c.l:=a.l+2;
69 for i:=1 to c.l do
70 begin
71 c.da[i]:=a.da[i]*b+c.da[i];
72 if c.da[i]>=10 then
73 begin
74 c.da[i+1]:=(c.da[i] div 10)+c.da[i+1];
75 c.da[i]:=c.da[i] mod 10;
76 end;
77 end;
78 while (c.da[c.l]=0)and(c.l>1) do dec(c.l);
79 end;
80 procedure find;
81 var
82 i,j,times:longint;b:boolean;c,m,d,z:arra;
83 begin
84
85 fillchar(c,sizeof(c),0);
86 m:=n;
87 c.l:=k;
88 fillchar(z,sizeof(z),0);
89 z.l:=1;z.da[1]:=1;
90 if (r.l=1)and(r.da[1]=0) then
91 begin
92 writeln(1);
93 halt;
94 end
95 else
96 for i:=1 to k do
97 begin
98
99 times:=1;
100 repeat
101 c:=r;
102 mult(r,n,r);
103 mult(c,m,d);
104 inc(times);
105 b:=check(m,d,i);
106 until (b)or(times>10);
107 if b then
108 begin
109 accmult(z,times-1,z);
110 n:=c;
111 r:=c;
112 n.l:=k;
113 end
114 else
115 begin
116 writeln(-1);
117 halt;
118 end;
119 end;
120 for i:=z.l downto 1 do write(z.da[i]);
121
122 end;
123 begin
124 init;
125 find;
126 end.