





'''simple docstring'''






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






def      __A						(							a_						:  Optional[Any]      ,a_						:  Optional[Any]      ,a_						:  Tuple      ,a_						:  List[str]	):
		lowerCAmelCase     :    Optional[int]						=   sum(a_i[j] for j in range(a_      ,len(a_	)	)	)
		lowerCAmelCase     :    Dict						=   sum(a_i[j] * base[j] for j in range(min(len(a_	)      ,a_	)	)	)

		lowerCAmelCase	,		lowerCAmelCase     :    List[str]						=   0, 0
		lowerCAmelCase     :    Optional[int]						=   n - i

		lowerCAmelCase     :    str						=   memo.get(a_	)

		if sub_memo is not None:
				lowerCAmelCase     :    Union[str, Any]						=   sub_memo.get(a_	)

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

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

				else:
						lowerCAmelCase     :    Dict						=   []
		else:
				lowerCAmelCase     :    str						=   {c: []}
				lowerCAmelCase     :    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
						lowerCAmelCase	,		lowerCAmelCase     :    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
				lowerCAmelCase	,		lowerCAmelCase     :    Optional[int]						=   compute(a_      ,a_      ,i + dn      ,a_	)
				diff += _diff
				dn += terms_jumped

		lowerCAmelCase     :    Optional[int]						=   sub_memo[c]

		# keep jumps sorted by # of terms skipped
		lowerCAmelCase     :    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      __A						(							a_						:  Dict      ,a_						:  List[Any]      ,a_						:  List[Any]      ,a_						:  Optional[int]	):
		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)
		lowerCAmelCase     :    str						=   i
		lowerCAmelCase	,		lowerCAmelCase	,		lowerCAmelCase     :    str						=   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
				lowerCAmelCase     :    Union[str, Any]						=   ds_c + ds_b
				diff += addend
				lowerCAmelCase     :    Optional[int]						=   0
				for j in range(a_	):
						lowerCAmelCase     :    int						=   a_i[j] + addend
						lowerCAmelCase	,		lowerCAmelCase     :    Any						=   divmod(a_      ,1_0	)

						ds_c += a_i[j]

				if addend > 0:
						break

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






def      __A						(							a_						:  Dict      ,a_						:  str      ,a_						:  int	):
		for j in range(a_      ,len(a_	)	):
				lowerCAmelCase     :    Dict						=   digits[j] + addend
				if s >= 1_0:
						lowerCAmelCase	,		lowerCAmelCase     :    List[Any]						=   divmod(a_      ,1_0	)
						lowerCAmelCase     :    Tuple						=   addend // 1_0 + quotient
				else:
						lowerCAmelCase     :    Optional[Any]						=   s
						lowerCAmelCase     :    Optional[int]						=   addend // 1_0

				if addend == 0:
						break

		while addend > 0:
				lowerCAmelCase	,		lowerCAmelCase     :    List[str]						=   divmod(a_      ,1_0	)
				digits.append(a_	)






def      __A						(							a_						:  int = 1_0**1_5	):
		lowerCAmelCase     :    Any						=   [1]
		lowerCAmelCase     :    Optional[Any]						=   1
		lowerCAmelCase     :    int						=   0
		while True:
				lowerCAmelCase	,		lowerCAmelCase     :    Dict						=   next_term(a_      ,2_0      ,i + dn      ,a_	)
				dn += terms_jumped
				if dn == n - i:
						break

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


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










'''simple docstring'''






import argparse

from diffusers.pipelines.stable_diffusion.convert_from_ckpt import download_controlnet_from_original_ckpt


if __name__ == "__main__":
					lowerCAmelCase       		=      argparse.ArgumentParser()

					parser.add_argument(
					    """--checkpoint_path""", default=None, type=str, required=True, help="""Path to the checkpoint to convert."""
					)
					parser.add_argument(
					    """--original_config_file""",
					    type=str,
					    required=True,
					    help="""The YAML config file corresponding to the original architecture.""",
					)
					parser.add_argument(
					    """--num_in_channels""",
					    default=None,
					    type=int,
					    help="""The number of input channels. If `None` number of input channels will be automatically inferred.""",
					)
					parser.add_argument(
					    """--image_size""",
					    default=5_12,
					    type=int,
					    help=(
					        """The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2"""
					        """ Base. Use 768 for Stable Diffusion v2."""
					    ),
					)
					parser.add_argument(
					    """--extract_ema""",
					    action="""store_true""",
					    help=(
					        """Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights"""
					        """ or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield"""
					        """ higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."""
					    ),
					)
					parser.add_argument(
					    """--upcast_attention""",
					    action="""store_true""",
					    help=(
					        """Whether the attention computation should always be upcasted. This is necessary when running stable"""
					        """ diffusion 2.1."""
					    ),
					)
					parser.add_argument(
					    """--from_safetensors""",
					    action="""store_true""",
					    help="""If `--checkpoint_path` is in `safetensors` format, load checkpoint with safetensors instead of PyTorch.""",
					)
					parser.add_argument(
					    """--to_safetensors""",
					    action="""store_true""",
					    help="""Whether to store pipeline in safetensors format or not.""",
					)
					parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""")
					parser.add_argument("""--device""", type=str, help="""Device to use (e.g. cpu, cuda:0, cuda:1, etc.)""")






					def      __A						(							a_						:  List[str]	):
							if string == "True":
									return True
							elif string == "False":
									return False
							else:
									raise ValueError(f'''could not parse string as bool {string}'''	)

					parser.add_argument(
					    """--use_linear_projection""", help="""Override for use linear projection""", required=False, type=parse_bool
					)

					parser.add_argument("""--cross_attention_dim""", help="""Override for cross attention_dim""", required=False, type=int)

					lowerCAmelCase       		=      parser.parse_args()

					lowerCAmelCase       		=      download_controlnet_from_original_ckpt(
					    checkpoint_path=args.checkpoint_path,
					    original_config_file=args.original_config_file,
					    image_size=args.image_size,
					    extract_ema=args.extract_ema,
					    num_in_channels=args.num_in_channels,
					    upcast_attention=args.upcast_attention,
					    from_safetensors=args.from_safetensors,
					    device=args.device,
					    use_linear_projection=args.use_linear_projection,
					    cross_attention_dim=args.cross_attention_dim,
					)

					controlnet.save_pretrained(args.dump_path, safe_serialization=args.to_safetensors)



