



'''simple docstring'''


lowercase							       =			range(2, 20 + 1)
lowercase							       =			[10**k for k in range(ks[-1] + 1)]
lowercase							       =			{}






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_       		=sum(a_i[j] for j in range(lowercase__       ,	len(lowercase__       )       )       )
     a_       		=sum(a_i[j] * base[j] for j in range(min(len(lowercase__       )       ,	lowercase__       )       )       )

     a_		,						a_       		=0, 0
     a_       		=n - i

     a_       		=memo.get(lowercase__       )

     if sub_memo is not None:
          a_       		=sub_memo.get(lowercase__       )

          if jumps is not None and len(lowercase__       ) > 0:
               # find and make the largest jump without going over
               a_       		=-1
               for _k in range(len(lowercase__       ) - 1       ,	-1       ,	-1       ):
                    if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
                         a_       		=_k
                         break

               if max_jump >= 0:
                    a_		,						a_		,						a_       		=jumps[max_jump]
                    # since the difference between jumps is cached, add c
                    a_       		=diff + c
                    for j in range(min(lowercase__       ,	len(lowercase__       )       )       ):
                         a_		,						a_       		=divmod(lowercase__       ,	1_0       )
                    if new_c > 0:
                         add(lowercase__       ,	lowercase__       ,	lowercase__       )

          else:
               a_       		=[]
     else:
          a_       		={c: []}
          a_       		=sub_memo

     if dn >= max_dn or c + diff >= base[k]:
          return diff, dn

     if k > ks[0]:
          while True:
               # keep doing smaller jumps
               a_		,						a_       		=next_term(lowercase__       ,	k - 1       ,	i + dn       ,	lowercase__       )
               diff += _diff
               dn += terms_jumped

               if dn >= max_dn or c + diff >= base[k]:
                    break
     else:
          # would be too small a jump, just compute sequential terms instead
          a_		,						a_       		=compute(lowercase__       ,	lowercase__       ,	i + dn       ,	lowercase__       )
          diff += _diff
          dn += terms_jumped

     a_       		=sub_memo[c]

     # keep jumps sorted by # of terms skipped
     a_       		=0
     while j < len(lowercase__       ):
          if jumps[j][1] > dn:
               break
          j += 1

     # cache the jump for this value digitsum(b) and c
     sub_memo[c].insert(lowercase__       ,	(diff, dn, k)       )
     return (diff, dn)






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     if i >= n:
          return 0, i
     if k > len(lowercase__       ):
          a_i.extend([0 for _ in range(k - len(lowercase__       )       )]       )

     # note: a_i -> b * 10^k + c
     # ds_b -> digitsum(b)
     # ds_c -> digitsum(c)
     a_       		=i
     a_		,						a_		,						a_       		=0, 0, 0
     for j in range(len(lowercase__       )       ):
          if j >= k:
               ds_b += a_i[j]
          else:
               ds_c += a_i[j]

     while i < n:
          i += 1
          a_       		=ds_c + ds_b
          diff += addend
          a_       		=0
          for j in range(lowercase__       ):
               a_       		=a_i[j] + addend
               a_		,						a_       		=divmod(lowercase__       ,	1_0       )

               ds_c += a_i[j]

          if addend > 0:
               break

     if addend > 0:
          add(lowercase__       ,	lowercase__       ,	lowercase__       )
     return diff, i - start_i






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     for j in range(lowercase__       ,	len(lowercase__       )       ):
          a_       		=digits[j] + addend
          if s >= 1_0:
               a_		,						a_       		=divmod(lowercase__       ,	1_0       )
               a_       		=addend // 1_0 + quotient
          else:
               a_       		=s
               a_       		=addend // 1_0

          if addend == 0:
               break

     while addend > 0:
          a_		,						a_       		=divmod(lowercase__       ,	1_0       )
          digits.append(lowercase__       )






def  UpperCAmelCase_       (		lowercase__ = 1_0**1_5       ):


     '''simple docstring'''







     a_       		=[1]
     a_       		=1
     a_       		=0
     while True:
          a_		,						a_       		=next_term(lowercase__       ,	2_0       ,	i + dn       ,	lowercase__       )
          dn += terms_jumped
          if dn == n - i:
               break

     a_       		=0
     for j in range(len(lowercase__       )       ):
          a_n += digits[j] * 1_0**j
     return a_n


if __name__ == "__main__":
  print(F"""{solution() = }""")







'''simple docstring'''


from __future__ import annotations






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     if b == 0:
          return (1, 0)
     ((a_)		,						(a_))       		=extended_euclid(lowercase__       ,	a % b       )
     a_       		=a // b
     return (y, x - k * y)






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     ((a_)		,						(a_))       		=extended_euclid(lowercase__       ,	lowercase__       )
     a_       		=na * na
     a_       		=ra * x * na + ra * y * na
     return (n % m + m) % m






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     ((a_)		,						(a_))       		=extended_euclid(lowercase__       ,	lowercase__       )
     if b < 0:
          a_       		=(b % n + n) % n
     return b






def  UpperCAmelCase_       (		lowercase__       ,	lowercase__       ,	lowercase__       ,	lowercase__       ):


     '''simple docstring'''







     a_		,						a_       		=invert_modulo(lowercase__       ,	lowercase__       ), invert_modulo(lowercase__       ,	lowercase__       )
     a_       		=na * na
     a_       		=ra * x * na + ra * y * na
     return (n % m + m) % m


if __name__ == "__main__":
  from doctest import testmod

  testmod(name='''chinese_remainder_theorem''', verbose=True)
  testmod(name='''chinese_remainder_theorem2''', verbose=True)
  testmod(name='''invert_modulo''', verbose=True)
  testmod(name='''extended_euclid''', verbose=True)


