Advertisement
Guest User

g-task

a guest
Jan 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.86 KB | None | 0 0
  1. program Vahob;
  2.  
  3. uses
  4.   SysUtils,
  5.   Math;
  6.  
  7. type
  8.   BigInt = array of uint64;
  9. const
  10.   base = 10000 * 100000;
  11. var
  12.   A, B, _GCD, _B, Whole: Uint64;
  13.   Num1, Num2, num3, p, res: BigInt;
  14.   k2, k5, _p, i, ss, minn, qw, ff: integer;
  15.   outstring: string = '';
  16.   f: Text;
  17.  
  18.   procedure getNum(S: string; var Abig: BigInt);
  19.   var
  20.     i, j: integer;
  21.     tmp: string;
  22.   begin
  23.     i := Length(s);
  24.     j := 0;
  25.     if i mod 9 = 0 then
  26.       SetLength(Abig, i div 9)
  27.     else
  28.       SetLength(Abig, (i div 9) + 1);
  29.     while i >= 1 do
  30.     begin
  31.       if i <= 9 then
  32.         tmp := copy(s, 1, i)
  33.       else
  34.         tmp := copy(s, i - 8, 9);
  35.       val(tmp, Abig[j]);
  36.       i -= 9;
  37.       j += 1;
  38.     end;
  39.   end;
  40.  
  41.   function multyply(N1, N2: BigInt): BigInt;
  42.   var
  43.     i, j, Len: Uint64;
  44.     buf, buffer: UInt64;
  45.     M: BigInt;
  46.   begin
  47.     Len := Length(N1) + Length(N2);
  48.     SetLength(M, Len);
  49.     buf := 0;
  50.     i := 0;
  51.     while (i <= High(N1)) do
  52.     begin
  53.       buffer := 0;
  54.       j := 0;
  55.       while (j <= High(N2)) or (Buffer > 0) do
  56.       begin
  57.         if j <= High(N2) then
  58.           buf := buffer + M[i + j] + N1[i] * N2[j]
  59.         else
  60.           buf := buffer + M[i + j];
  61.         M[i + j] := buf mod Base;
  62.         buffer := buf div Base;
  63.         j := j + 1;
  64.       end;
  65.       i := i + 1;
  66.     end;
  67.     if M[High(M)] = 0 then
  68.       SetLength(M, High(M));
  69.     Result := M;
  70.   end;
  71.  
  72.   procedure WriteBigInt(ABigInt: BigInt);
  73.   var
  74.     i: integer;
  75.     fmt: string;
  76.   begin
  77.     for i := High(ABigInt) downto 0 do
  78.     begin
  79.       if i = High(ABigInt) then
  80.         outstring += IntToStr(ABigInt[i])
  81.       else
  82.       begin
  83.         fmt := Format('%.9d', [ABigInt[i]]);
  84.         outstring += fmt;
  85.       end;
  86.     end;
  87.   end;
  88.  
  89.   function FWriteBigInt(ABigInt: BigInt): string;
  90.   var
  91.     i: integer;
  92.     fmt: string;
  93.   begin
  94.     for i := High(ABigInt) downto 0 do
  95.     begin
  96.       if i = High(ABigInt) then
  97.         Result += IntToStr(ABigInt[i])
  98.       else
  99.       begin
  100.         fmt := Format('%.9d', [ABigInt[i]]);
  101.         Result += fmt;
  102.       end;
  103.     end;
  104.   end;
  105.  
  106.   function gcd(q, w: uint64): uint64;
  107.   begin
  108.     if (w = 0) then
  109.       gcd := q
  110.     else
  111.       gcd := gcd(w, q mod w);
  112.   end;
  113.  
  114.   function Divs(var Ai: Uint64; d: Uint64): integer;
  115.   var
  116.     k: integer;
  117.   begin
  118.     k := 0;
  119.     while (Ai mod d = 0) do
  120.     begin
  121.       Ai := Ai div d;
  122.       k += 1;
  123.     end;
  124.     Divs := k;
  125.   end;
  126.  
  127.   procedure sss;
  128.   begin
  129.     //if (outstring <> '') then //begin
  130.       outstring += '.';
  131.     for i := 1 to max(k2, k5) - Length(FWriteBigInt(res)) do
  132.       outstring += '0';
  133.     WriteBigInt(res);
  134.     Write(f, outstring);
  135.   end;
  136.  
  137. begin
  138.   Assign(f, 'input.txt');
  139.   reset(f);
  140.   Read(f, A, B);
  141.   Close(f);
  142.   Assign(f, 'output.txt');
  143.   rewrite(f);
  144.       if (A mod B = 0) then
  145.       begin
  146.         write(f, A div B);//первый случай
  147.       end else begin
  148.       _GCD := gcd(A, B);
  149.       A := A div _GCD;
  150.       B := B div _GCD;
  151.       Whole := A div B;
  152.       _B := B;
  153.       k2 := Divs(_B, 2);
  154.       k5 := Divs(_B, 5);
  155.       if _B <> 1 then
  156.       begin//второй случай
  157.         if Whole <> 0 then
  158.           write(f, Whole, ' ( ', A mod B, ' / ', B, ' )')
  159.         else
  160.           write(f, A, ' / ', B);
  161.       end;
  162.       if (_B = 1) then
  163.       begin
  164.         if k2 >= k5 then
  165.         begin
  166.           getNum(IntToStr(5), num2);
  167.           _p := k2;
  168.         end
  169.         else
  170.         begin
  171.           getNum(IntToStr(2), num2);
  172.           _p := k5;
  173.         end;
  174.         getNum(IntToStr(A - Whole * B), res);
  175.         outstring := IntToStr(Whole);
  176.         minn := min(k2, k5);
  177.         while minn <> max(k2, k5) do
  178.         begin
  179.           res := multyply(res, Num2);
  180.           minn += 1;
  181.         end;
  182.         sss;
  183.  
  184.       end;
  185.       end;
  186.   Close(f);
  187. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement