POJ 3289(高精度乘法)

高精度乘法


Program P3289;
const
   maxn=40000;
   F=10;
type
   arr=record
       d:array[1..maxn] of longint;
       len,doc:longint;
       end;
var
   r,m:arr;
   y:longint;
   i:longint;
   a,b:arr;
function max(a,b:longint):longint;
begin
   if a<b then exit(b) else exit(a);
end;
procedure multipy(a,b:arr;var c:arr);
var
   i,j,len:longint;
begin
   fillchar(c,sizeof(c),0);
   c.len:=a.len+b.len;

   for i:=1 to a.len do
      for j:=1 to b.len do
      begin
         inc(c.d[i+j-1],a.d[i]*b.d[j]);
         inc(c.d[i+j],c.d[i+j-1] div F);
         c.d[i+j-1]:=c.d[i+j-1] mod F;
      end;
   while (c.d[c.len]=0) and (c.len>1) do dec(c.len);
   c.doc:=a.doc+b.doc;


end;
procedure to_arr(doc:longint;var c:arr);
var
   x,i,n:longint;
begin
   read(x);
   i:=0;
   while (x>0) do
   begin
      inc(i);
      c.d[i]:=x mod F;
      x:=x div F;
   end;
   c.len:=i;
   c.doc:=doc;
end;
Procedure jie(a:arr;x:longint;var c:arr);
begin
   if x=1 then begin c:=a; exit; end;
   fillchar(c,sizeof(c),0);
   if (x mod 2=0) then
   begin
      jie(a,x div 2,c);
      multipy(c,c,c);
   end
   else
   begin
      jie(a,x div 2,c);
      multipy(c,c,c);
      multipy(a,c,c);
   end;
end;
begin
   to_arr(2,r);
   r.len:=3;r.d[3]:=1;
   to_arr(0,m);
   read(y);
   jie(r,y,r);
   multipy(m,r,m);


   for i:=m.len downto m.doc+1 do write(m.d[i]);
   writeln;

end.


你可能感兴趣的:(POJ 3289(高精度乘法))