


"""simple docstring"""



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







def   __lowerCAmelCase						(						lowercase  :  List[Any]						,							lowercase  :  Tuple						,							lowercase  :  Optional[Any]						,							lowercase  :  Optional[int]			)     ->						Union[str, Any]:


    """simple docstring"""





    snake_case   :				Union[str, Any]				 =   sum(a_i[j] for j in range(_snake_case						,							len(_snake_case			)			)			)
    snake_case   :				Optional[Any]				 =   sum(a_i[j] * base[j] for j in range(min(len(_snake_case			)						,							_snake_case			)			)			)

    snake_case   :				int				 =   0, 0
    snake_case   :				Union[str, Any]				 =   n - i

    snake_case   :				List[str]				 =   memo.get(_snake_case			)

    if sub_memo is not None:
        snake_case   :				int				 =   sub_memo.get(_snake_case			)

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

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

        else:
            snake_case   :				Optional[Any]				 =   []
    else:
        snake_case   :				int				 =   {c: []}
        snake_case   :				Optional[int]				 =   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   :				Any				 =   next_term(_snake_case						,							k - 1						,							i + dn						,							_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
        snake_case   :				Union[str, Any]				 =   compute(_snake_case						,							_snake_case						,							i + dn						,							_snake_case			)
        diff += _diff
        dn += terms_jumped

    snake_case   :				Optional[Any]				 =   sub_memo[c]

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

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







def   __lowerCAmelCase						(						lowercase  :  int						,							lowercase  :  Any						,							lowercase  :  Dict						,							lowercase  :  str			)     ->						Optional[Any]:


    """simple docstring"""





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

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

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

            ds_c += a_i[j]

        if addend > 0:
            break

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







def   __lowerCAmelCase						(						lowercase  :  List[str]						,							lowercase  :  Optional[Any]						,							lowercase  :  Union[str, Any]			)     ->						Tuple:


    """simple docstring"""





    for j in range(_snake_case						,							len(_snake_case			)			):
        snake_case   :				Optional[Any]				 =   digits[j] + addend
        if s >= 10:
            snake_case   :				int				 =   divmod(_snake_case						,							10			)
            snake_case   :				Tuple				 =   addend // 10 + quotient
        else:
            snake_case   :				Any				 =   s
            snake_case   :				Optional[int]				 =   addend // 10

        if addend == 0:
            break

    while addend > 0:
        snake_case   :				Optional[Any]				 =   divmod(_snake_case						,							10			)
        digits.append(_snake_case			)







def   __lowerCAmelCase						(						lowercase  :  int = 10**15			)     ->						int:


    """simple docstring"""





    snake_case   :				Optional[int]				 =   [1]
    snake_case   :				Dict				 =   1
    snake_case   :				Dict				 =   0
    while True:
        snake_case   :				int				 =   next_term(_snake_case						,							20						,							i + dn						,							_snake_case			)
        dn += terms_jumped
        if dn == n - i:
            break

    snake_case   :				Optional[int]				 =   0
    for j in range(len(_snake_case			)			):
        a_n += digits[j] * 10**j
    return a_n


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









"""simple docstring"""



import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class _lowerCAmelCase    (				unittest.TestCase     ):







   @property
   def 			lowerCamelCase		(     self						)     ->		str:

       '''simple docstring'''




       torch.manual_seed(0						)
       snake_case   :				int				 =   UNetaDModel(
           block_out_channels=(32, 64)						,     layers_per_block=2						,     sample_size=32						,     in_channels=3						,     out_channels=3						,     down_block_types=("DownBlock2D", "AttnDownBlock2D")						,     up_block_types=("AttnUpBlock2D", "UpBlock2D")						,     )
       return model







   def 			lowerCamelCase		(     self						)     ->		Any:

       '''simple docstring'''




       snake_case   :				Any				 =   self.dummy_uncond_unet
       snake_case   :				Tuple				 =   KarrasVeScheduler()

       snake_case   :				int				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Optional[Any]				 =   torch.manual_seed(0						)
       snake_case   :				List[Any]				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Dict				 =   pipe(num_inference_steps=2						,     generator=UpperCamelCase__						,     output_type="numpy"						,     return_dict=UpperCamelCase__						)[0]

       snake_case   :				Tuple				 =   image[0, -3:, -3:, -1]
       snake_case   :				Tuple				 =   image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       snake_case   :				int				 =   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]						)

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







@slow
@require_torch
class _lowerCAmelCase    (				unittest.TestCase     ):







   def 			lowerCamelCase		(     self						)     ->		List[Any]:

       '''simple docstring'''




       snake_case   :				Optional[Any]				 =   "google/ncsnpp-celebahq-256"
       snake_case   :				List[str]				 =   UNetaDModel.from_pretrained(UpperCamelCase__						)
       snake_case   :				Optional[Any]				 =   KarrasVeScheduler()

       snake_case   :				Optional[int]				 =   KarrasVePipeline(unet=UpperCamelCase__						,     scheduler=UpperCamelCase__						)
       pipe.to(UpperCamelCase__						)
       pipe.set_progress_bar_config(disable=UpperCamelCase__						)

       snake_case   :				Dict				 =   torch.manual_seed(0						)
       snake_case   :				Union[str, Any]				 =   pipe(num_inference_steps=20						,     generator=UpperCamelCase__						,     output_type="numpy"						).images

       snake_case   :				int				 =   image[0, -3:, -3:, -1]
       assert image.shape == (1, 256, 256, 3)
       snake_case   :				Any				 =   np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]						)

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





