



"""simple docstring"""




_lowerCAmelCase    : List[Any]							=							range(2, 20 + 1)
_lowerCAmelCase    : Dict							=							[10**k for k in range(ks[-1] + 1)]
_lowerCAmelCase    : dict[int, dict[int, list[list[int]]]]							=							{}

def 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						Any						,  SCREAMING_SNAKE_CASE__     :						Dict						,  SCREAMING_SNAKE_CASE__     :						Optional[Any]						,  SCREAMING_SNAKE_CASE__     :						Dict    )  ->       Any:





     '''simple docstring'''

     _UpperCAmelCase      :     int        =   sum(a_i[j] for j in range(SCREAMING_SNAKE_CASE__						,  len(SCREAMING_SNAKE_CASE__    )    )    )
     _UpperCAmelCase      :     Union[str, Any]        =   sum(a_i[j] * base[j] for j in range(min(len(SCREAMING_SNAKE_CASE__    )						,  SCREAMING_SNAKE_CASE__    )    )    )

     _UpperCAmelCase    ,	_UpperCAmelCase      :     Tuple        =   0, 0
     _UpperCAmelCase      :     Any        =   n - i

     _UpperCAmelCase      :     Optional[Any]        =   memo.get(SCREAMING_SNAKE_CASE__    )

     if sub_memo is not None:
          _UpperCAmelCase      :     int        =   sub_memo.get(SCREAMING_SNAKE_CASE__    )

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

               if max_jump >= 0:
                    _UpperCAmelCase    ,	_UpperCAmelCase    ,	_UpperCAmelCase      :     Dict        =   jumps[max_jump]
                    # since the difference between jumps is cached, add c
                    _UpperCAmelCase      :     List[Any]        =   diff + c
                    for j in range(min(SCREAMING_SNAKE_CASE__						,  len(SCREAMING_SNAKE_CASE__    )    )    ):
                         _UpperCAmelCase    ,	_UpperCAmelCase      :     Any        =   divmod(SCREAMING_SNAKE_CASE__						,  10    )
                    if new_c > 0:
                         add(SCREAMING_SNAKE_CASE__						,  SCREAMING_SNAKE_CASE__						,  SCREAMING_SNAKE_CASE__    )

          else:
               _UpperCAmelCase      :     str        =   []
     else:
          _UpperCAmelCase      :     int        =   {c: []}
          _UpperCAmelCase      :     Dict        =   sub_memo

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

     if k > ks[0]:
          while True:
               # keep doing smaller jumps
               _UpperCAmelCase    ,	_UpperCAmelCase      :     Optional[Any]        =   next_term(SCREAMING_SNAKE_CASE__						,  k - 1						,  i + dn						,  SCREAMING_SNAKE_CASE__    )
               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
          _UpperCAmelCase    ,	_UpperCAmelCase      :     int        =   compute(SCREAMING_SNAKE_CASE__						,  SCREAMING_SNAKE_CASE__						,  i + dn						,  SCREAMING_SNAKE_CASE__    )
          diff += _diff
          dn += terms_jumped

     _UpperCAmelCase      :     Dict        =   sub_memo[c]

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

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

def 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						int						,  SCREAMING_SNAKE_CASE__     :						Optional[int]						,  SCREAMING_SNAKE_CASE__     :						Dict						,  SCREAMING_SNAKE_CASE__     :						int    )  ->       List[Any]:





     '''simple docstring'''

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

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

     while i < n:
          i += 1
          _UpperCAmelCase      :     Dict        =   ds_c + ds_b
          diff += addend
          _UpperCAmelCase      :     Tuple        =   0
          for j in range(SCREAMING_SNAKE_CASE__    ):
               _UpperCAmelCase      :     Any        =   a_i[j] + addend
               _UpperCAmelCase    ,	_UpperCAmelCase      :     List[str]        =   divmod(SCREAMING_SNAKE_CASE__						,  10    )

               ds_c += a_i[j]

          if addend > 0:
               break

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

def 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						List[Any]						,  SCREAMING_SNAKE_CASE__     :						Dict						,  SCREAMING_SNAKE_CASE__     :						Tuple    )  ->       Union[str, Any]:





     '''simple docstring'''

     for j in range(SCREAMING_SNAKE_CASE__						,  len(SCREAMING_SNAKE_CASE__    )    ):
          _UpperCAmelCase      :     Dict        =   digits[j] + addend
          if s >= 10:
               _UpperCAmelCase    ,	_UpperCAmelCase      :     int        =   divmod(SCREAMING_SNAKE_CASE__						,  10    )
               _UpperCAmelCase      :     Union[str, Any]        =   addend // 10 + quotient
          else:
               _UpperCAmelCase      :     Optional[int]        =   s
               _UpperCAmelCase      :     Optional[int]        =   addend // 10

          if addend == 0:
               break

     while addend > 0:
          _UpperCAmelCase    ,	_UpperCAmelCase      :     Dict        =   divmod(SCREAMING_SNAKE_CASE__						,  10    )
          digits.append(SCREAMING_SNAKE_CASE__    )

def 						__snake_case						(				SCREAMING_SNAKE_CASE__     :						int = 10**15    )  ->       int:





     '''simple docstring'''

     _UpperCAmelCase      :     Any        =   [1]
     _UpperCAmelCase      :     str        =   1
     _UpperCAmelCase      :     int        =   0
     while True:
          _UpperCAmelCase    ,	_UpperCAmelCase      :     Tuple        =   next_term(SCREAMING_SNAKE_CASE__						,  20						,  i + dn						,  SCREAMING_SNAKE_CASE__    )
          dn += terms_jumped
          if dn == n - i:
               break

     _UpperCAmelCase      :     int        =   0
     for j in range(len(SCREAMING_SNAKE_CASE__    )    ):
          a_n += digits[j] * 10**j
     return a_n


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





"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


_lowerCAmelCase    : Union[str, Any]							=							{}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 _lowerCAmelCase    : Optional[Any]							=							["GPTSw3Tokenizer"]


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_gpt_swa import GPTSwaTokenizer

else:
 import sys

 _lowerCAmelCase    : List[Any]							=							_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
