




UpperCamelCase__ :   str						=							range(2, 20 + 1)
UpperCamelCase__ :   Dict						=							[10**k for k in range(ks[-1] + 1)]
UpperCamelCase__ :   Tuple						=							{}





def        __UpperCAmelCase						(							lowerCamelCase_ :     str  ,							lowerCamelCase_ :     List[str]  ,							lowerCamelCase_ :     Optional[int]  ,							lowerCamelCase_ :     Optional[int]  )       ->  Dict:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : Dict					  =      sum(a_i[j] for j in range(_a  ,							len(_a  )  )  )
			SCREAMING_SNAKE_CASE_   : str					  =      sum(a_i[j] * base[j] for j in range(min(len(_a  )  ,							_a  )  )  )

			SCREAMING_SNAKE_CASE_   : Any					  =      0, 0
			SCREAMING_SNAKE_CASE_   : List[Any]					  =      n - i

			SCREAMING_SNAKE_CASE_   : List[Any]					  =      memo.get(_a  )

			if sub_memo is not None:
						SCREAMING_SNAKE_CASE_   : Dict					  =      sub_memo.get(_a  )

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

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

						else:
									SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      []
			else:
						SCREAMING_SNAKE_CASE_   : str					  =      {c: []}
						SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      sub_memo

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

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

			SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      sub_memo[c]

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

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





def        __UpperCAmelCase						(							lowerCamelCase_ :     str  ,							lowerCamelCase_ :     Tuple  ,							lowerCamelCase_ :     Tuple  ,							lowerCamelCase_ :     Optional[Any]  )       ->  str:







			"""simple docstring"""





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

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

			while i < n:
						i += 1
						SCREAMING_SNAKE_CASE_   : str					  =      ds_c + ds_b
						diff += addend
						SCREAMING_SNAKE_CASE_   : str					  =      0
						for j in range(_a  ):
									SCREAMING_SNAKE_CASE_   : str					  =      a_i[j] + addend
									SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      divmod(_a  ,							10  )

									ds_c += a_i[j]

						if addend > 0:
									break

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





def        __UpperCAmelCase						(							lowerCamelCase_ :     Dict  ,							lowerCamelCase_ :     Optional[Any]  ,							lowerCamelCase_ :     Optional[int]  )       ->  int:







			"""simple docstring"""





			for j in range(_a  ,							len(_a  )  ):
						SCREAMING_SNAKE_CASE_   : Tuple					  =      digits[j] + addend
						if s >= 10:
									SCREAMING_SNAKE_CASE_   : List[Any]					  =      divmod(_a  ,							10  )
									SCREAMING_SNAKE_CASE_   : int					  =      addend // 10 + quotient
						else:
									SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      s
									SCREAMING_SNAKE_CASE_   : List[Any]					  =      addend // 10

						if addend == 0:
									break

			while addend > 0:
						SCREAMING_SNAKE_CASE_   : str					  =      divmod(_a  ,							10  )
						digits.append(_a  )





def        __UpperCAmelCase						(							lowerCamelCase_ :     Any = 10**15  )       ->  Optional[Any]:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : List[str]					  =      [1]
			SCREAMING_SNAKE_CASE_   : Any					  =      1
			SCREAMING_SNAKE_CASE_   : List[Any]					  =      0
			while True:
						SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      next_term(_a  ,							20  ,							i + dn  ,							_a  )
						dn += terms_jumped
						if dn == n - i:
									break

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


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






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






@slow
@require_torch_gpu
class _UpperCamelCase				(				unittest.TestCase ):
			'''simple docstring'''







			def 				__UpperCamelCase			(      self				:			Dict						)				->							Tuple:

				"""simple docstring"""

				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()
			def 				__UpperCamelCase			(      self				:			Optional[int]						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   List[Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   Tuple					  =							output.images

				SCREAMING_SNAKE_CASE :   Any					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_euler"						)

				SCREAMING_SNAKE_CASE :   List[str]					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   List[str]					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe([prompt]   ,   generator=a   ,   guidance_scale=9.0   ,   num_inference_steps=20   ,   output_type="np"						)

				SCREAMING_SNAKE_CASE :   List[Any]					  =							output.images

				SCREAMING_SNAKE_CASE :   Tuple					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 5e-1






			def 				__UpperCamelCase			(      self				:			Tuple						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Dict					  =							StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							sd_pipe.to(a						)
				sd_pipe.set_progress_bar_config(disable=a						)

				sd_pipe.set_scheduler("sample_dpmpp_2m"						)

				SCREAMING_SNAKE_CASE :   str					  =							"A painting of a squirrel eating a burger"
				SCREAMING_SNAKE_CASE :   Any					  =							torch.manual_seed(0						)
				SCREAMING_SNAKE_CASE :   str					  =							sd_pipe(
				    [prompt]   ,   generator=a   ,   guidance_scale=7.5   ,   num_inference_steps=15   ,   output_type="np"   ,   use_karras_sigmas=a   ,   )

				SCREAMING_SNAKE_CASE :   str					  =							output.images

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 512, 512, 3)
				SCREAMING_SNAKE_CASE :   int					  =							np.array(
				    [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2