



"""simple docstring"""





__lowerCAmelCase       :					Any      					=					range(2, 20 + 1)
__lowerCAmelCase       :					Optional[int]      					=					[10**k for k in range(ks[-1] + 1)]
__lowerCAmelCase       :					dict[int, dict[int, list[list[int]]]]      					=					{}
def 	__lowerCAmelCase						(		__UpperCamelCase      :			Optional[Any]					, __UpperCamelCase      :			int					, __UpperCamelCase      :			Optional[int]					, __UpperCamelCase      :			Union[str, Any]    ):


						'''simple docstring'''






						snake_case_   : Optional[int]						=			sum(a_i[j] for j in range(__UpperCamelCase					, len(__UpperCamelCase    )    )    )
						snake_case_   : str						=			sum(a_i[j] * base[j] for j in range(min(len(__UpperCamelCase    )					, __UpperCamelCase    )    )    )

						snake_case_			,		snake_case_   : str						=			0, 0
						snake_case_   : int						=			n - i

						snake_case_   : Any						=			memo.get(__UpperCamelCase    )

						if sub_memo is not None:
												snake_case_   : Any						=			sub_memo.get(__UpperCamelCase    )

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

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

												else:
																		snake_case_   : Tuple						=			[]
						else:
												snake_case_   : List[Any]						=			{c: []}
												snake_case_   : Tuple						=			sub_memo

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

						if k > ks[0]:
												while True:
																		# keep doing smaller jumps
																		snake_case_			,		snake_case_   : Tuple						=			next_term(__UpperCamelCase					, k - 1					, i + dn					, __UpperCamelCase    )
																		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
												snake_case_			,		snake_case_   : List[str]						=			compute(__UpperCamelCase					, __UpperCamelCase					, i + dn					, __UpperCamelCase    )
												diff += _diff
												dn += terms_jumped

						snake_case_   : Optional[int]						=			sub_memo[c]

						# keep jumps sorted by # of terms skipped
						snake_case_   : Union[str, Any]						=			0
						while j < len(__UpperCamelCase    ):
												if jumps[j][1] > dn:
																		break
												j += 1

						# cache the jump for this value digitsum(b) and c
						sub_memo[c].insert(__UpperCamelCase					, (diff, dn, k)    )
						return (diff, dn)
def 	__lowerCAmelCase						(		__UpperCamelCase      :			str					, __UpperCamelCase      :			List[str]					, __UpperCamelCase      :			Any					, __UpperCamelCase      :			Any    ):


						'''simple docstring'''






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

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

						while i < n:
												i += 1
												snake_case_   : Union[str, Any]						=			ds_c + ds_b
												diff += addend
												snake_case_   : Tuple						=			0
												for j in range(__UpperCamelCase    ):
																		snake_case_   : Any						=			a_i[j] + addend
																		snake_case_			,		snake_case_   : Optional[int]						=			divmod(__UpperCamelCase					, 1_0    )

																		ds_c += a_i[j]

												if addend > 0:
																		break

						if addend > 0:
												add(__UpperCamelCase					, __UpperCamelCase					, __UpperCamelCase    )
						return diff, i - start_i
def 	__lowerCAmelCase						(		__UpperCamelCase      :			List[str]					, __UpperCamelCase      :			Tuple					, __UpperCamelCase      :			Optional[Any]    ):


						'''simple docstring'''






						for j in range(__UpperCamelCase					, len(__UpperCamelCase    )    ):
												snake_case_   : Tuple						=			digits[j] + addend
												if s >= 1_0:
																		snake_case_			,		snake_case_   : Optional[Any]						=			divmod(__UpperCamelCase					, 1_0    )
																		snake_case_   : Dict						=			addend // 1_0 + quotient
												else:
																		snake_case_   : Tuple						=			s
																		snake_case_   : Union[str, Any]						=			addend // 1_0

												if addend == 0:
																		break

						while addend > 0:
												snake_case_			,		snake_case_   : Dict						=			divmod(__UpperCamelCase					, 1_0    )
												digits.append(__UpperCamelCase    )
def 	__lowerCAmelCase						(		__UpperCamelCase      :			int = 1_0**1_5    ):


						'''simple docstring'''






						snake_case_   : List[str]						=			[1]
						snake_case_   : Any						=			1
						snake_case_   : str						=			0
						while True:
												snake_case_			,		snake_case_   : int						=			next_term(__UpperCamelCase					, 2_0					, i + dn					, __UpperCamelCase    )
												dn += terms_jumped
												if dn == n - i:
																		break

						snake_case_   : Tuple						=			0
						for j in range(len(__UpperCamelCase    )    ):
												a_n += digits[j] * 1_0**j
						return a_n


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






"""simple docstring"""





import unittest

from transformers import is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, require_tokenizers, require_torch, slow


if is_flax_available():
					import optax
					from flax.training.common_utils import onehot

					from transformers import AutoTokenizer, FlaxMTaForConditionalGeneration
					from transformers.models.ta.modeling_flax_ta import shift_tokens_right



@require_torch
@require_sentencepiece
@require_tokenizers
@require_flax
class    _lowerCAmelCase    (				unittest.TestCase		):





		"""simple docstring"""






		@slow
		def    UpperCAmelCase__							(					self   ) ->						Optional[int]:







								'''simple docstring'''

								snake_case_   : int						=			FlaxMTaForConditionalGeneration.from_pretrained("""google/mt5-small"""   )
								snake_case_   : List[Any]						=			AutoTokenizer.from_pretrained("""google/mt5-small"""   )

								snake_case_   : Dict						=			tokenizer("""Hello there"""							,    return_tensors="""np"""   ).input_ids
								snake_case_   : List[str]						=			tokenizer("""Hi I am"""							,    return_tensors="""np"""   ).input_ids

								snake_case_   : Optional[Any]						=			shift_tokens_right(_lowercase							,    model.config.pad_token_id							,    model.config.decoder_start_token_id   )

								snake_case_   : Tuple						=			model(_lowercase							,    decoder_input_ids=_lowercase   ).logits
								snake_case_   : Tuple						=			optax.softmax_cross_entropy(_lowercase							,    onehot(_lowercase							,    logits.shape[-1]   )   ).mean()

								snake_case_   : List[str]						=			-(labels.shape[-1] * loss.item())

								snake_case_   : Optional[int]						=			-84.9127
								self.assertTrue(abs(mtf_score - EXPECTED_SCORE   ) < 1E-4   )

