Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program sistemschislV2;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- var
- f, ff1, j, i, len: integer;
- s, ff, w, k, s2, res, s1, s3: string;
- boo: boolean;
- function inv(c: integer): char;
- begin
- if(c div 10 = 0) then inv := inttostr(c)[1]
- else inv := chr(c + 55);
- end;
- function inv1(c: char): integer;
- begin
- if(ord(c) <= 57) and (ord(c) >= 48)then inv1 := ord(c) - 48
- else inv1 := ord(c) - 55;
- end;
- function check(s, s1:string): boolean;
- var
- i: integer;
- begin
- while (length(s) > 1) and (s[1] = '0') do delete(s, 1, 1);
- while (length(s1) > 1) and (s1[1] = '0') do delete(s1, 1, 1);
- if (length(s) > length(s1)) then
- begin
- check := true;
- exit;
- end
- else if (length(s) < length(s1)) then
- begin
- check := false;
- exit;
- end;
- i := 1;
- while (s[i] = s1[i]) and (i <= length(s)) do inc(i);
- check := ((i = length(s) + 1) or (ord(s[i]) > ord(s1[i])));
- end;
- function perev(a, base: integer): string;
- var
- s: string;
- begin
- s := '';
- while (a >= base) do
- begin
- s := inv(a mod base) + s;
- a := a div base;
- end;
- s := inv(a) + s;
- perev := s;
- end;
- function todec(s: string; base: integer): integer;
- var
- i, p, zn: integer;
- begin
- p := 1;
- zn := 0;
- for i := length(s) downto 1 do
- begin
- inc(zn, inv1(s[i]) * p);
- p := p * base;
- end;
- todec := zn;
- end;
- function mns(s, f: string; base: integer): string;
- var
- i, len, c, j: integer;
- a: array [1..200000] of char;
- sf: string;
- k: char;
- begin
- if (length(s) > length(f)) then len := length(s)
- else len := length(f);
- k := inv(base - 1);
- for i := length(s) + 1 to len do s := '0' + s;
- for i := length(f) + 1 to len do f := '0' + f;
- for i := 1 to len do
- begin
- c := inv1(s[length(s) - i + 1]) - inv1(f[length(f) - i + 1]);
- if (c < 0) then
- begin
- j := length(s) - i;
- while (s[j] = '0') do
- begin
- s[j] := k;
- dec(j);
- end;
- s[j] := inv(inv1(s[j]) - 1);
- c := c + base;
- end;
- a[i] := inv(c);
- end;
- j := len;
- sf := '';
- while (a[j] = '0') and (j > 1) do dec(j);
- for i := j downto 1 do
- sf := sf + a[i];
- mns := sf;
- end;
- begin
- readln(f, ff1);
- readln(s);
- if (f = ff1) then
- begin
- writeln(s);
- halt(0);
- end;
- k := '';
- s2 := s;
- ff := perev(ff1, f);
- while (check(s2, ff)) do
- begin
- s := s2;
- res := '';
- len := 0;
- boo := true;
- while check(s, ff) do
- begin
- i := len + 1;
- j := 0;
- while (not check(copy(s, 1, i), ff))do
- if boo then inc(i)
- else
- begin
- inc(i);
- res := res + '0';
- end;
- boo := false;
- s1 := copy(s, 1, i);
- while check(s1, ff) do
- begin
- inc(j);
- s1 := mns(s1, ff, f);
- end;
- delete(s, 1, i);
- s3 := perev(j, f);
- res := res + s3;
- if (s1[1] = '0') then len := 0
- else len := length(s1);
- if not (check(s1 + s, ff)) then for j := 1 to length(s) do res := res + '0';
- if (s1[1] <> '0') then s := s1 + s;
- end;
- s2 := res;
- while (s1[1] = '0') and (length(s1) > 1) do delete(s1, 1, 1);
- k := inv(todec(s, F)) + k;
- end;
- if (s2 <>'0') then k := inv(todec(s2, f)) + k;
- if length(k) = 0 then k := '0';
- while (LENGTH(K) > 1) and (k[1] = '0') do delete(k , 1, 1);
- writeln(k);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement