Advertisement
savrasov

ZZZ

Dec 6th, 2017
3,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program sistemschislV2;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   f, ff1, j, i, len: integer;
  10.   s, ff, w, k, s2, res, s1, s3: string;
  11.   boo: boolean;
  12.  
  13. function inv(c: integer): char;
  14. begin
  15.   if(c div 10 = 0) then inv := inttostr(c)[1]
  16.     else inv := chr(c + 55);
  17. end;
  18.  
  19. function inv1(c: char): integer;
  20. begin
  21.   if(ord(c) <= 57) and (ord(c) >= 48)then inv1 := ord(c) - 48
  22.     else inv1 := ord(c) - 55;
  23. end;
  24.  
  25. function check(s, s1:string): boolean;
  26. var
  27.   i: integer;
  28. begin
  29.   while (length(s) > 1) and (s[1] = '0') do delete(s, 1, 1);
  30.   while (length(s1) > 1) and (s1[1] = '0') do delete(s1, 1, 1);
  31.   if (length(s) > length(s1)) then
  32.   begin
  33.     check := true;
  34.     exit;
  35.   end
  36.   else if (length(s) < length(s1)) then
  37.   begin
  38.     check := false;
  39.     exit;
  40.   end;
  41.   i := 1;
  42.   while (s[i] = s1[i]) and (i <= length(s)) do inc(i);
  43.   check := ((i = length(s) + 1) or (ord(s[i]) > ord(s1[i])));
  44. end;
  45.  
  46. function perev(a, base: integer): string;
  47. var
  48.   s: string;
  49. begin
  50.   s := '';
  51.   while (a >= base) do
  52.   begin
  53.     s := inv(a mod base) + s;
  54.     a := a div base;
  55.   end;
  56.   s := inv(a) + s;
  57.   perev := s;
  58. end;
  59.  
  60. function todec(s: string; base: integer): integer;
  61. var
  62.   i, p, zn: integer;
  63. begin
  64.   p := 1;
  65.   zn := 0;
  66.   for i := length(s) downto 1 do
  67.   begin
  68.     inc(zn, inv1(s[i]) * p);
  69.     p := p * base;
  70.   end;
  71.   todec := zn;
  72. end;
  73.  
  74. function mns(s, f: string; base: integer): string;
  75. var
  76.   i, len, c, j: integer;
  77.   a: array [1..200000] of char;
  78.   sf: string;
  79.   k: char;
  80. begin
  81.   if (length(s) > length(f)) then len := length(s)
  82.   else len := length(f);
  83.   k := inv(base - 1);
  84.   for i := length(s) + 1 to len do s := '0' + s;
  85.   for i := length(f) + 1 to len do f := '0' + f;
  86.   for i := 1 to len do
  87.   begin
  88.     c := inv1(s[length(s) - i + 1]) - inv1(f[length(f) - i + 1]);
  89.     if (c < 0) then
  90.     begin
  91.       j := length(s) - i;
  92.       while (s[j] = '0') do
  93.       begin
  94.         s[j] := k;
  95.         dec(j);
  96.       end;
  97.       s[j] := inv(inv1(s[j]) - 1);
  98.       c := c + base;
  99.     end;
  100.     a[i] := inv(c);
  101.   end;
  102.   j := len;
  103.   sf := '';
  104.   while (a[j] = '0') and (j > 1) do dec(j);
  105.   for i := j downto 1 do
  106.     sf := sf + a[i];
  107.   mns := sf;
  108. end;
  109.  
  110. begin
  111.   readln(f, ff1);
  112.   readln(s);
  113.   if (f = ff1) then
  114.   begin
  115.     writeln(s);
  116.     halt(0);
  117.   end;
  118.   k := '';
  119.   s2 := s;
  120.   ff := perev(ff1, f);
  121.   while (check(s2, ff)) do
  122.   begin
  123.     s := s2;
  124.     res := '';
  125.     len := 0;
  126.     boo := true;
  127.     while check(s, ff) do
  128.     begin
  129.       i := len + 1;
  130.       j := 0;
  131.       while (not check(copy(s, 1, i), ff))do
  132.       if boo  then inc(i)
  133.       else
  134.       begin
  135.         inc(i);
  136.         res := res + '0';
  137.       end;
  138.       boo := false;
  139.       s1 := copy(s, 1, i);
  140.       while check(s1, ff) do
  141.       begin
  142.         inc(j);
  143.         s1 := mns(s1, ff, f);
  144.       end;
  145.       delete(s, 1, i);
  146.       s3 := perev(j, f);
  147.       res := res + s3;
  148.       if (s1[1] = '0') then len := 0
  149.       else len := length(s1);
  150.       if not (check(s1 + s, ff)) then for j := 1 to length(s) do res := res + '0';
  151.       if (s1[1] <> '0') then s := s1 + s;
  152.     end;
  153.     s2 := res;
  154.     while (s1[1] = '0') and (length(s1) > 1) do delete(s1, 1, 1);
  155.     k := inv(todec(s, F)) + k;
  156.   end;
  157.   if (s2 <>'0') then k := inv(todec(s2, f)) + k;
  158.   if length(k) = 0 then k := '0';
  159.   while (LENGTH(K) > 1) and (k[1] = '0') do delete(k , 1, 1);
  160.   writeln(k);
  161. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement