Skip to content
Narrow screen resolution Wide screen resolution Auto adjust screen size Increase font size Decrease font size Default font size default color grey color
         
 | 
VNOI - Olympic tin học Việt Nam

Điểm tin VOJ

Số thành viên:6040
Số bài tập:1001
Số bài nộp:722923
Bài nộp hôm nay:0

Top 10 thành viên xuất sắc

HạngThành viênĐiểm
1mr_invincible587.9
2white_cobra418.6
3hieult403.4
4phaleq384.0
5vodanh9x368.2
6con_nha_ngheo352.0
7flash_mt350.2
8darksabers349.8
9yenthanh132345.3
10rockman9x_94343.1
[VOJ] Mã nguồn #205 - [Lib] Hổ trợ tính toán số lớn (Cộng, trừ, nhân, chia, lấy dư, UCLN, BCNN)
Ngày: 23-03-2011
Cập nhật: 23-03-2011
Người gửi: yenthanh132
Ngôn ngữ: Pascal
Xem: 2042

Điểm: 4.4/5 (32 Phiếu)


Type BigNum=ansistring;
Function Sosanh(a,b:BigNum):ShortInt; {-1: a<b; 0: a=b; 1: a>b}
Begin
  If a=b then exit(0);
  While length(a)<length(b) do a:='0'+a;
  While length(b)<length(a) do b:='0'+b;
  If a<b then exit(-1) else exit(1);
End;
Function Cong(a,b:BigNum):BigNum;
Var s:BigNum;
    i,t:Longint;
Begin
  While length(a)<length(b) do a:='0'+a;
  While length(b)<length(a) do b:='0'+b;
  s:=''; t:=0;
  For i:=length(a) downto 1 do
    Begin
      t:=t+ord(a[i])+ord(b[i])-96;
      s:=chr(t mod 10 + 48)+s;
      t:=t div 10;
    End;
  If t>0 then s:=chr(t+48)+s;
  Exit(s);
End;
Function Tru(a,b:BigNum):BigNum;
Var s:BigNum;
    i,t:Longint;
Begin
  While length(a)<length(b) do a:='0'+a;
  While length(b)<length(a) do b:='0'+b;
  s:=''; t:=0;
  For i:=length(a) downto 1  do
    Begin
      t:=ord(a[i])-ord(b[i])+t;
      If t<0 then
        Begin
          Inc(t,10);
          s:=chr(t+48)+s;
          t:=-1;
        End
      Else
        Begin
          s:=chr(t+48)+s;
          t:=0;
        End;
    End;
  While (length(s)>1) and (s[1]='0') do Delete(s,1,1);
  Exit(s);
End;
Function NhanMin(a:BigNum;b:Longint):BigNum;
Var s:Bignum;
    i,t:Longint;
Begin
  s:=''; t:=0;
  For i:=length(a) downto 1 do
    Begin
      t:=t+(ord(a[i])-48)*b;
      s:=chr(t mod 10 + 48)+s;
      t:=t div 10;
    End;
  If t>0 then s:=chr(t+48)+s;
  While (length(s)>1) and (s[1]='0') do Delete(s,1,1);
  Exit(s);
End;
Function Nhan(a,b:BigNum):BigNum;
Var s,st:BigNum;
    i,j:Longint;
Begin
  s:='0';
  For i:=length(b) downto 1 do
    Begin
      st:=NhanMin(a,ord(b[i])-48);
      For j:=1 to length(b)-i do st:=st+'0';
      s:=Cong(s,st);
    End;
  While (length(s)>1) and (s[1]='0') do Delete(s,1,1);
  Exit(s);
End;
Function Chia(a,b:BigNum):BigNum;
Var c:array[0..9] of BigNum;
    i,j:Longint;
    s,t:BigNum;
Begin
  t:=''; s:='';
  c[0]:='0';
  For i:=1 to 9 do c[i]:=cong(c[i-1],b);
  For i:=1 to length(a) do
    Begin
      t:=t+a[i];
      For j:=9 downto 0 do
        If Sosanh(t,c[j])<>-1 then Break;
      s:=s+chr(j+48);
      t:=Tru(t,c[j]);
    End;
  While (length(s)>1) and (s[1]='0') do Delete(s,1,1);
  Exit(s);
End;
Function LayMod(a,b:BigNum):BigNum;
Var c:array[0..9] of BigNum;
    i,j:Longint;
    s,t:BigNum;
Begin
  t:=''; s:='';
  c[0]:='0';
  For i:=1 to 9 do c[i]:=cong(c[i-1],b);
  For i:=1 to length(a) do
    Begin
      t:=t+a[i];
      For j:=9 downto 0 do
        If Sosanh(t,c[j])<>-1 then Break;
      s:=s+chr(j+48);
      t:=Tru(t,c[j]);
    End;
  While (length(t)>1) and (t[1]='0') do Delete(t,1,1);
  Exit(t);
End;
Function UCLN(a,b:BigNum):BigNum;
Var t:BigNum;
Begin
  While b<>'0' do
    Begin
      a:=LayMod(a,b);
      t:=a; a:=b; b:=t;
    End;
  Exit(a);
End;
Function BCNN(a,b:BigNUm):BigNum;
Begin
  Exit(Chia(Nhan(a,b),UCLN(a,b)));
End;
BEGIN
 
END.