Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Vahob;
- uses
- SysUtils,
- Math;
- type
- BigInt = array of uint64;
- const
- base = 10000 * 100000;
- var
- A, B, _GCD, _B, Whole: Uint64;
- Num1, Num2, num3, p, res: BigInt;
- k2, k5, _p, i, ss, minn, qw, ff: integer;
- outstring: string = '';
- f: Text;
- procedure getNum(S: string; var Abig: BigInt);
- var
- i, j: integer;
- tmp: string;
- begin
- i := Length(s);
- j := 0;
- if i mod 9 = 0 then
- SetLength(Abig, i div 9)
- else
- SetLength(Abig, (i div 9) + 1);
- while i >= 1 do
- begin
- if i <= 9 then
- tmp := copy(s, 1, i)
- else
- tmp := copy(s, i - 8, 9);
- val(tmp, Abig[j]);
- i -= 9;
- j += 1;
- end;
- end;
- function multyply(N1, N2: BigInt): BigInt;
- var
- i, j, Len: Uint64;
- buf, buffer: UInt64;
- M: BigInt;
- begin
- Len := Length(N1) + Length(N2);
- SetLength(M, Len);
- buf := 0;
- i := 0;
- while (i <= High(N1)) do
- begin
- buffer := 0;
- j := 0;
- while (j <= High(N2)) or (Buffer > 0) do
- begin
- if j <= High(N2) then
- buf := buffer + M[i + j] + N1[i] * N2[j]
- else
- buf := buffer + M[i + j];
- M[i + j] := buf mod Base;
- buffer := buf div Base;
- j := j + 1;
- end;
- i := i + 1;
- end;
- if M[High(M)] = 0 then
- SetLength(M, High(M));
- Result := M;
- end;
- procedure WriteBigInt(ABigInt: BigInt);
- var
- i: integer;
- fmt: string;
- begin
- for i := High(ABigInt) downto 0 do
- begin
- if i = High(ABigInt) then
- outstring += IntToStr(ABigInt[i])
- else
- begin
- fmt := Format('%.9d', [ABigInt[i]]);
- outstring += fmt;
- end;
- end;
- end;
- function FWriteBigInt(ABigInt: BigInt): string;
- var
- i: integer;
- fmt: string;
- begin
- for i := High(ABigInt) downto 0 do
- begin
- if i = High(ABigInt) then
- Result += IntToStr(ABigInt[i])
- else
- begin
- fmt := Format('%.9d', [ABigInt[i]]);
- Result += fmt;
- end;
- end;
- end;
- function gcd(q, w: uint64): uint64;
- begin
- if (w = 0) then
- gcd := q
- else
- gcd := gcd(w, q mod w);
- end;
- function Divs(var Ai: Uint64; d: Uint64): integer;
- var
- k: integer;
- begin
- k := 0;
- while (Ai mod d = 0) do
- begin
- Ai := Ai div d;
- k += 1;
- end;
- Divs := k;
- end;
- procedure sss;
- begin
- //if (outstring <> '') then //begin
- outstring += '.';
- for i := 1 to max(k2, k5) - Length(FWriteBigInt(res)) do
- outstring += '0';
- WriteBigInt(res);
- Write(f, outstring);
- end;
- begin
- Assign(f, 'input.txt');
- reset(f);
- Read(f, A, B);
- Close(f);
- Assign(f, 'output.txt');
- rewrite(f);
- if (A mod B = 0) then
- begin
- write(f, A div B);//первый случай
- end else begin
- _GCD := gcd(A, B);
- A := A div _GCD;
- B := B div _GCD;
- Whole := A div B;
- _B := B;
- k2 := Divs(_B, 2);
- k5 := Divs(_B, 5);
- if _B <> 1 then
- begin//второй случай
- if Whole <> 0 then
- write(f, Whole, ' ( ', A mod B, ' / ', B, ' )')
- else
- write(f, A, ' / ', B);
- end;
- if (_B = 1) then
- begin
- if k2 >= k5 then
- begin
- getNum(IntToStr(5), num2);
- _p := k2;
- end
- else
- begin
- getNum(IntToStr(2), num2);
- _p := k5;
- end;
- getNum(IntToStr(A - Whole * B), res);
- outstring := IntToStr(Whole);
- minn := min(k2, k5);
- while minn <> max(k2, k5) do
- begin
- res := multyply(res, Num2);
- minn += 1;
- end;
- sss;
- end;
- end;
- Close(f);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement