POJ 2109

题目大意:给出n,p, 求k^n=p;数据保证k,n都在合理范围内,p是高精度了。

解:ym c,有pow函数,直接开方就行了..我还是写了一个高精度+二分,气愤的是他居然有无解的情况,而且很容易爆数组上限,要加一个数位的判断,嘛,写的就这样了,有点乱。

View Code
  1 //Power of Ceyptography
  2 const
  3         inf='1.txt';
  4         start=1000000001;
  5 type
  6         data=array[0..200]of longint;
  7 var
  8         p: data;
  9         look: data;
 10         n, k: longint;
 11 
 12 operator =(a, b:data)z: boolean;
 13 var
 14         i: longint;
 15 begin
 16   if a[0]<>b[0] then exit(false);
 17   for i := 1 to a[0] do if a[i]<>b[i] then exit(false);
 18   exit(true);
 19 end;
 20 
 21 operator <(a, b: data)z: boolean;
 22 var
 23         i: longint;
 24 begin
 25   if a[0]>b[0] then exit(false)
 26     else if a[0]<b[0] then exit(true)
 27       else
 28         for i := a[0] downto 1 do
 29           if a[i]>b[i] then exit(false)
 30             else if a[i]<b[i] then exit(true);
 31   exit(false);
 32 end;
 33 
 34 operator *(a, b: data)z : data;
 35 var
 36         i, j, g: longint;
 37 begin
 38   fillchar(z, sizeof(z), 0);
 39   z[0] := a[0] + b[0] - 1;
 40   for i := 1 to a[0]+1 do begin
 41     g := 0;
 42     for j := 1 to b[0]+1 do begin
 43       g := g + a[i]*b[j] + z[i+j-1];
 44       z[i+j-1] := g mod 10;
 45       g := g div 10;
 46     end;
 47   end;
 48   while z[z[0]+1]<>0 do inc(z[0]);
 49 end;
 50 
 51 function pow(aa, b: longint): data;
 52 var
 53         base, a: data;
 54 begin
 55   fillchar(a, sizeof(a), 0);
 56   while aa>0 do begin
 57     inc(a[0]);
 58     a[a[0]] := aa mod 10;
 59     aa := aa div 10;
 60   end;
 61   base := a; fillchar(pow, sizeof(pow), 0);
 62   pow[0] := 1; pow[1] := 1;
 63   while b>0 do begin
 64     if 1 and b=1 then pow := pow * base;
 65     base := base *base;
 66     b := b >> 1;
 67   end;
 68 end;
 69 
 70 procedure init;
 71 var
 72         c: char;
 73         i: longint;
 74         a: data;
 75 begin
 76   fillchar(p, sizeof(p), 0);
 77   k := 0;
 78   read(n);
 79   read(c);
 80   while not eoln do begin
 81     read(c);
 82     inc(p[0]);
 83     a[p[0]] := ord(c) - 48;
 84   end;
 85   for i := 1 to p[0] do
 86     p[i] := a[p[0]-i+1];
 87 end;
 88 
 89 function wei(x: longint): longint;
 90 begin
 91   wei := 0;
 92   while x>0 do begin
 93     inc(wei); x := x div 10;
 94   end;
 95 end;
 96 
 97 procedure main;
 98 var
 99         left, right, mid: longint;
100         tmp: data;
101         key: longint;
102 begin
103   key := wei(n);
104   left := 1; //right := p[0] div w + 2;
105   right := start;
106   while {true}left<=right do begin
107     mid := (left+right)>>1;
108     if wei(mid)*key>p[0] then begin
109       right := mid -1; continue;
110     end;
111     tmp := pow(mid, n);
112     if tmp=p then begin writeln(mid); exit end
113       else if tmp<p then left := mid+1
114         else right := mid-1;
115   end;
116   writeln(right);
117 end;
118 
119 begin
120   assign(input,inf); reset(input);
121   while not seekeof do begin
122     init;
123     main;
124   end;
125 end.
posted @ 2012-05-08 19:00  F.D.His.D  阅读(165)  评论(0编辑  收藏  举报