Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- uses
- SysUtils,
- Math;
- type
- TLongNumber = array of QWord;
- function GCD(a, b: QWord): QWord;
- begin
- while (a > 0) and (b > 0) do
- begin
- if (a > b) then
- a := a mod b
- else
- b := b mod a;
- end;
- Result := a + b;
- end;
- //function pow(a, n: QWord): QWord;
- //var
- // x: QWord;
- //begin
- // x := 1;
- // while (n <> 0) do
- // begin
- // if (n mod 2 = 1) then
- // begin
- // x := x * a;
- // end;
- // a := a * a;
- // n := n div 2;
- //
- // end;
- // Result := x;
- //end;
- operator * (a, b: TLongNumber) ans: TLongNumber;
- var
- i, j: integer;
- s, buf: QWord;
- begin
- s := 0;
- begin
- SetLength(Result, high(a) + high(b) + 1);
- for i := 0 to high(Result) do
- begin
- Result[i] := 0;
- end;
- for i := 0 to high(b) do
- begin
- s := 0;
- for j := 0 to high(a) do
- begin
- buf := b[i] * a[j] + s + Result[i+j];
- s := buf div 1000000000;
- Result[i+j]:=buf mod 1000000000;
- end;
- if (s > 0) then
- begin
- if i = high(b) then
- SetLength(Result, Length(Result) + 1);
- Result[i + high(a) + 1] := s;
- end;
- end;
- end;
- end;
- function Pow(num: TLongNumber; n: integer): TLongNumber;
- var
- x: TLongNumber;
- begin
- SetLength(x, 1);
- x[0] := 1;
- while (n <> 0) do
- begin
- if (n mod 2 = 1) then
- begin
- x := x * num;
- end;
- num := num * num;
- n := n div 2;
- end;
- Result := x;
- end;
- function Invers_str(str: ansistring): ansistring;
- var
- newS: ansistring;
- i: integer;
- begin
- for i := length(str) downto 1 do
- begin
- newS += str[i];
- end;
- Result := newS;
- end;
- //function Mult(lhs: TLongNumber; b: QWord): TLongNumber;
- //var
- // c: QWord;
- // buf, Q: QWOrd;
- // i: integer;
- //begin
- //
- // Q := 1000000000;
- // c := 0;
- // SetLength(Result, high(lhs) + 1);
- // for i := 0 to high(Result) do
- // begin
- // buf := lhs[i] * b + c;
- // c := buf div Q;
- // Result[i] := buf mod Q;
- // end;
- // if (c > 0) then
- // begin
- // SetLength(Result, high(Result) + 2);
- // Result[i + 1] := c;
- // end;
- //end;
- function fact(a: QWord; out n2, m5: QWord): boolean;
- begin
- n2 := 0;
- m5 := 0;
- while (a mod 2 = 0) do
- begin
- a := a div 2;
- n2 += 1;
- end;
- while (a mod 5 = 0) do
- begin
- a := a div 5;
- m5 += 1;
- end;
- if (a = 1) then
- exit(True)
- else
- exit(False);
- end;
- function ToLongNumber(a: QWord): TLongNumber;
- var
- n, i, len: integer;
- buf, str: string;
- begin
- str := IntToStr(a);
- len := (length(str) div 9) + 1;
- SetLength(Result, len);
- n := 0;
- i := 1;
- str := invers_str(str);
- while i <= length(str) do
- begin
- buf := copy(str, i, 9);
- buf := invers_str(buf);
- i += 9;
- Result[n] := StrToInt(buf);
- n += 1;
- end;
- end;
- function ToString(val: TLongNumber): ansistring;
- var
- i, j, Count: integer;
- begin
- Result := '';
- for i := high(val) downto 0 do
- begin
- if (length(IntToStr(val[i])) < 9) then
- begin
- Count := 9 - length(IntToStr(val[i]));
- for j := 1 to Count do
- Result += '0';
- end;
- Result += IntToStr(val[i]);
- end;
- i := 1;
- while Result[i] = '0' do
- begin
- if (Result = '0') then
- exit('0');
- Delete(Result, i, 1);
- end;
- end;
- function trim_zero(s: string): string;
- begin
- while s[length(s)] = '0' do
- begin
- Delete(s, length(s), 1);
- end;
- Result := s;
- end;
- function divmod(a, b: QWord; n2, m5: QWord): string;
- var
- zero, ans: string;
- i, c: integer;
- begin
- zero := '';
- if (m5 >= n2) then
- begin
- ans := toString((ToLongNumber(a) * pow(ToLongNumber(2), m5 - n2)));
- c := length(ans) - m5;
- if (c > 0) then
- begin
- Result := copy(ans, 1, c) + '.' + copy(ans, c + 1, m5);
- end;
- if (c <= 0) then
- begin
- for i := 1 to m5 - length(ans) do
- begin
- zero += '0';
- end;
- Result := '0' + '.' + zero + ans;
- end;
- end;
- if (n2 > m5) then
- begin
- ans := toString((ToLongNumber(a) * pow(ToLongNumber(5), n2 - m5)));
- c := length(ans) - n2;
- if (c > 0) then
- begin
- Result := copy(ans, 1, c) + '.' + copy(ans, length(ans) - n2 + 1, n2);
- end;
- if (c <= 0) then
- begin
- for i := 1 to n2 - length(ans) do
- begin
- zero += '0';
- end;
- Result := '0' + '.' + zero + ans;
- end;
- end;
- Result := trim_zero(Result);
- end;
- var
- A, B, NOD, rem, intPart, n2, m5: QWord;
- Dec: boolean;
- begin
- Assign(input, 'input.txt');
- Assign(output, 'output.txt');
- reset(input);
- rewrite(output);
- //while not EOF do
- begin
- readln(A);
- Readln(B);
- NOD := GCD(A, B);
- A := A div NOD;
- B := B div NOD;
- Dec := fact(B, n2, m5);
- if (B = 1) then
- writeln(A)
- else if (A < B) then
- begin
- if (Dec) then
- begin
- writeln(divmod(A, B, n2, m5));
- end
- else
- begin
- writeln(A, ' ', '/', ' ', B);
- end;
- end
- else if (B < A) then
- begin
- if (Dec) then
- begin
- Writeln(divmod(A, B, n2, m5));
- end
- else
- begin
- intPart := A div B;
- rem := A mod B;
- Writeln(intPart, ' ', '(', ' ', rem, ' ', '/', ' ', B, ' ', ')');
- end;
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement