Code:
Program solon;
Const f1='';
f2='';
hecoso=10000;
nmax=510;
Type DigitType = longint;
Bignum = array[0..nmax] of DigitType;
Var fi,fo:text;
A,B,C,D:bignum;
atmp:array[-9..nmax*2] of integer;
Procedure Input;
Var i,so,j:integer;
ch:char;
Begin
Assign(fo,f1);reset(fo);
i:=0;
While not(eoln(fo)) do
Begin
read(fo,ch);
inc(i);
so:=ord(ch)-ord('0');
atmp[i]:=so;
end;
A[0]:=(i-1) div 4 +1;j:=i;i:=1;
While i<=a[0] do
begin
a[i]:=atmp[j-3]*1000+atmp[j-2]*100+atmp[j-1]*10+atmp[j];
inc(i);dec(j,4);
end;
readln(fo);i:=0;
While not(eoln(fo)) do
Begin
read(fo,ch);
inc(i);
so:=ord(ch)-ord('0');
atmp[i]:=so;
end;
b[0]:=(i-1) div 4 +1;j:=i;i:=1;
While i<=b[0] do
begin
b[i]:=atmp[j-3]*1000+atmp[j-2]*100+atmp[j-1]*10+atmp[j];
inc(i);dec(j,4);
end;
Close(fo);
end;
Procedure Output;
Var i:integer;
st:string;
Begin
write(fi,c[c[0]]);
For i:=c[0]-1 downto 1 do
Begin
str(c[i],st);
While length(st)<4 do st:='0'+st;
write(fi,st);
end;
writeln(fi);
end;
Procedure Plus(A,B:bignum;var c:bignum);
Var i,nho:integer;
Begin
nho:=0;c[0]:=a[0];
If a[0]<b[0] then c[0]:=b[0];
For i:=1 to c[0] do
begin
c[i]:=a[i]+b[i]+nho;
nho:=c[i] div hecoso;
c[i]:=c[i] mod hecoso;
end;
If nho>0 then
begin
inc(c[0]);c[c[0]]:=nho;
end;
end;
Procedure Minus(A,B:bignum;var C:bignum);
Var nho,i:integer;
Begin
nho:=0;c[0]:=a[0];
For i:=1 to a[0] do
begin
c[i]:=a[i]-b[i]-nho;
If c[i]<0 then
begin
c[i]:=c[i]+hecoso;nho:=1;
end
else nho:=0;
end;
While (C[c[0]]=0) and (c[0]>1) do dec(c[0]);
end;
Procedure Multimin(x:longint);
Var nho,p:qword;
i:integer;
Begin
nho:=0;d[0]:=a[0];p:=0;
For i:=1 to a[0] do
begin
p:=a[i]*x+nho;
nho:=p div hecoso;
d[i]:=p mod hecoso;
end;
While nho>0 do
begin
inc(d[0]);
d[d[0]]:= nho mod hecoso;
nho:=nho div hecoso;
end;
end;
Procedure Chuyen(var d:bignum;k:integer);
Var i:integer;
Begin
If (d[0]=1) and (d[1]=0) then d[0]:=d[0]+k-1
else d[0]:=d[0]+k;
For i:=d[0] downto 1 do
If i>k then d[i]:=d[i-k]
else d[i]:=0;
end;
Procedure Multimax;
Var i:integer;
begin
For i:=1 to b[0] do
begin
multimin(b[i]);
chuyen(d,i-1);
plus(C,D,C);
end;
end;
Function Compare(A,B:bignum):byte;
Var i:integer;
Begin
If a[0]<b[0] then begin compare:=1;exit;end;
If a[0]>b[0] then begin compare:=3;exit;end;
For i:=a[0] downto 1 do begin
If a[i]<b[i] then begin compare:=1;exit;end;
If a[i]>b[i] then begin compare:=3;exit;end;
end;
Compare:=2;
end;
Procedure Process;
Begin
Plus(a,b,c);output;
If (compare(a,b)=1) or (compare(a,b)=2) then
begin
Minus(b,a,c);
If c[c[0]]<>0 then write(fi,'-');
end
else minus(a,b,c);output;
Fillchar(c,sizeof(c),0);
Multimax;output;
end;
Begin
Input;
Assign(fi,f2);rewrite(fi);
process;
Close(fi);
end.
Cộng trừ nhân với số cực lớn hơn cả string
Bookmarks