

import re
from typing import Callable, List, Optional, Union

import tensorflow as tf


try:
	from tensorflow.keras.optimizers.legacy import Adam
except ImportError:
	from tensorflow.keras.optimizers import Adam



class        _a		(tf.keras.optimizers.schedules.LearningRateSchedule							):


					'''simple docstring'''






					def __init__(					self       ,			A__       ,			A__       ,			A__       ,			A__ = 1.0       ,			A__ = None       ,			):
						super().__init__()
						A__				:       List[Any]	  =						initial_learning_rate
						A__				:       Optional[int]	  =						warmup_steps
						A__				:       Dict	  =						power
						A__				:       str	  =						decay_schedule_fn
						A__				:       Optional[Any]	  =						name






					def __call__(					self       ,			A__     ):
						with tf.name_scope(self.name or """WarmUp"""     ) as name:
							# Implements polynomial warmup. i.e., if global_step < warmup_steps, the
							# learning rate will be `global_step/num_warmup_steps * init_lr`.
							A__				:       int	  =						tf.cast(a_       ,			tf.floataa     )
							A__				:       str	  =						tf.cast(self.warmup_steps       ,			tf.floataa     )
							A__				:       Tuple	  =						global_step_float / warmup_steps_float
							A__				:       Tuple	  =						self.initial_learning_rate * tf.math.pow(a_       ,			self.power     )
							return tf.cond(
							    global_step_float < warmup_steps_float       ,			lambda: warmup_learning_rate       ,			lambda: self.decay_schedule_fn(step - self.warmup_steps     )       ,			name=a_       ,			)






					def     __A     (					self     ):
						return {
						    "initial_learning_rate": self.initial_learning_rate,
						    "decay_schedule_fn": self.decay_schedule_fn,
						    "warmup_steps": self.warmup_steps,
						    "power": self.power,
						    "name": self.name,
						}



def 		UpperCamelCase   (lowercase_:					float       , lowercase_:					int       , lowercase_:					int       , lowercase_:					float = 0.0       , lowercase_:					float = 0.9       , lowercase_:					float = 0.999       , lowercase_:					float = 1E-8       , lowercase_:					Optional[float] = None       , lowercase_:					Optional[float] = None       , lowercase_:					float = 0.0       , lowercase_:					float = 1.0       , lowercase_:					Optional[List[str]] = None       , )	-> List[Any]:
	A__				:       Dict	  =						tf.keras.optimizers.schedules.PolynomialDecay(
	    initial_learning_rate=a_       , decay_steps=num_train_steps - num_warmup_steps       , end_learning_rate=init_lr * min_lr_ratio       , power=a_       , )
	if num_warmup_steps:
		A__				:       List[str]	  =						WarmUp(
		    initial_learning_rate=a_       , decay_schedule_fn=a_       , warmup_steps=a_       , )
	if weight_decay_rate > 0.0:
		A__				:       Optional[int]	  =						AdamWeightDecay(
		    learning_rate=a_       , weight_decay_rate=a_       , beta_a=a_       , beta_a=a_       , epsilon=a_       , clipnorm=a_       , global_clipnorm=a_       , exclude_from_weight_decay=["""LayerNorm""", """layer_norm""", """bias"""]       , include_in_weight_decay=a_       , )
	else:
		A__				:       Optional[Any]	  =						tf.keras.optimizers.Adam(
		    learning_rate=a_       , beta_a=a_       , beta_a=a_       , epsilon=a_       , clipnorm=a_       , global_clipnorm=a_       , )
	# We return the optimizer and the LR scheduler in order to better track the
	# evolution of the LR independently of the optimizer.
	return optimizer, lr_schedule



class        _a		(_A							):


					'''simple docstring'''






					def __init__(					self       ,			A__ = 0.0_0_1       ,			A__ = 0.9       ,			A__ = 0.9_9_9       ,			A__ = 1e-7       ,			A__ = False       ,			A__ = 0.0       ,			A__ = None       ,			A__ = None       ,			A__ = "AdamWeightDecay"       ,			**A__       ,			):
						super().__init__(a_       ,			a_       ,			a_       ,			a_       ,			a_       ,			a_       ,			**a_     )
						A__				:       int	  =						weight_decay_rate
						A__				:       Any	  =						include_in_weight_decay
						A__				:       Tuple	  =						exclude_from_weight_decay






					@classmethod
					def     __A     (					cls       ,			A__     ):
						A__				:       str	  =						{"WarmUp": WarmUp}
						return super(a_       ,			cls     ).from_config(a_       ,			custom_objects=a_     )






					def     __A     (					self       ,			A__       ,			A__       ,			A__     ):
						super(a_       ,			self     )._prepare_local(a_       ,			a_       ,			a_     )
						A__				:       Optional[Any]	  =						tf.constant(
						    self.weight_decay_rate       ,			name="""adam_weight_decay_rate"""     )






					def     __A     (					self       ,			A__       ,			A__       ,			A__     ):
						A__				:       str	  =						self._do_use_weight_decay(var.name     )
						if do_decay:
							return var.assign_sub(
							    learning_rate * var * apply_state[(var.device, var.dtype.base_dtype)]["""weight_decay_rate"""]       ,			use_locking=self._use_locking       ,			)
						return tf.no_op()






					def     __A     (					self       ,			A__       ,			A__=None       ,			**A__     ):
						A__				:       str	  =						list(zip(*a_     )     )
						return super(a_       ,			self     ).apply_gradients(zip(a_       ,			a_     )       ,			name=a_       ,			**a_     )






					def     __A     (					self       ,			A__       ,			A__       ,			A__     ):
						if apply_state is None:
							return self._decayed_lr_t[var_dtype], {}

						A__				:       Dict	  =						apply_state or {}
						A__				:       int	  =						apply_state.get((var_device, var_dtype)     )
						if coefficients is None:
							A__				:       Optional[Any]	  =						self._fallback_apply_state(a_       ,			a_     )
							A__				:       Optional[int]	  =						coefficients

						return coefficients["lr_t"], {"apply_state": apply_state}






					def     __A     (					self       ,			A__       ,			A__       ,			A__=None     ):
						A__				:       Dict	  =						self._get_lr(var.device       ,			var.dtype.base_dtype       ,			a_     )
						A__				:       Dict	  =						self._decay_weights_op(a_       ,			a_       ,			a_     )
						with tf.control_dependencies([decay]     ):
							return super(a_       ,			self     )._resource_apply_dense(a_       ,			a_       ,			**a_     )






					def     __A     (					self       ,			A__       ,			A__       ,			A__       ,			A__=None     ):
						A__				:       List[str]	  =						self._get_lr(var.device       ,			var.dtype.base_dtype       ,			a_     )
						A__				:       Optional[Any]	  =						self._decay_weights_op(a_       ,			a_       ,			a_     )
						with tf.control_dependencies([decay]     ):
							return super(a_       ,			self     )._resource_apply_sparse(a_       ,			a_       ,			a_       ,			**a_     )






					def     __A     (					self     ):
						A__				:       Union[str, Any]	  =						super().get_config()
						config.update({"""weight_decay_rate""": self.weight_decay_rate}     )
						return config






					def     __A     (					self       ,			A__     ):
						if self.weight_decay_rate == 0:
							return False

						if self._include_in_weight_decay:
							for r in self._include_in_weight_decay:
								if re.search(a_       ,			a_     ) is not None:
									return True

						if self._exclude_from_weight_decay:
							for r in self._exclude_from_weight_decay:
								if re.search(a_       ,			a_     ) is not None:
									return False
						return True



class        _a		(_A							):


					'''simple docstring'''






					def __init__(					self     ):
						A__				:       Tuple	  =						[]
						A__				:       Dict	  =						None






					@property
					def     __A     (					self     ):
						if self._accum_steps is None:
							A__				:       Dict	  =						tf.Variable(
							    tf.constant(0       ,			dtype=tf.intaa     )       ,			trainable=a_       ,			synchronization=tf.VariableSynchronization.ON_READ       ,			aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA       ,			)

						return self._accum_steps.value()






					@property
					def     __A     (					self     ):
						if not self._gradients:
							raise ValueError("""The accumulator should be called first to initialize the gradients"""     )
						return [gradient.value() if gradient is not None else gradient for gradient in self._gradients]






					def __call__(					self       ,			A__     ):
						if not self._gradients:
							A__				:       Tuple	  =						self.step  # Create the step variable.
							self._gradients.extend(
							    [
							        tf.Variable(
							            tf.zeros_like(a_     )       ,			trainable=a_       ,			synchronization=tf.VariableSynchronization.ON_READ       ,			aggregation=tf.VariableAggregation.ONLY_FIRST_REPLICA       ,			)
							        if gradient is not None
							        else gradient
							        for gradient in gradients
							    ]     )
						if len(a_     ) != len(self._gradients     ):
							raise ValueError(F"""Expected {len(self._gradients     )} gradients, but got {len(a_     )}"""     )

						for accum_gradient, gradient in zip(self._gradients       ,			a_     ):
							if accum_gradient is not None and gradient is not None:
								accum_gradient.assign_add(a_     )

						self._accum_steps.assign_add(1     )






					def     __A     (					self     ):
						if not self._gradients:
							return
						self._accum_steps.assign(0     )
						for gradient in self._gradients:
							if gradient is not None:
								gradient.assign(tf.zeros_like(a_     )     )










'''simple docstring'''






lowerCAmelCase									=   [sum(int(c, 10) ** 2 for c in i.__str__()) for i in range(10_00_00)]






def   __A    (				a_    :	int						):
		lowerCAmelCase  :     Optional[int]       						= 0
		while number:
				# Increased Speed Slightly by checking every 5 digits together.
				sum_of_digits_squared += DIGITS_SQUARED[number % 1_0_0_0_0_0]
				number //= 1_0_0_0_0_0

		return sum_of_digits_squared


# There are 2 Chains made,
# One ends with 89 with the chain member 58 being the one which when declared first,
# there will be the least number of iterations for all the members to be checked.

# The other one ends with 1 and has only one element 1.

# So 58 and 1 are chosen to be declared at the starting.

# Changed dictionary to an array to quicken the solution
lowerCAmelCase									=   [None] * 10_00_00_00
lowerCAmelCase									=   True
lowerCAmelCase									=   False






def   __A    (				a_    :	int						):

		if CHAINS[number - 1] is not None:
				return CHAINS[number - 1]  # type: ignore

		lowerCAmelCase  :     Dict       						= chain(next_number(a_						)						)
		lowerCAmelCase  :     Union[str, Any]       						= number_chain

		while number < 1_0_0_0_0_0_0_0:
				lowerCAmelCase  :     Any       						= number_chain
				number *= 1_0

		return number_chain






def   __A    (				a_    :	int = 1_0_0_0_0_0_0_0						):
		for i in range(1   ,a_						):
				if CHAINS[i] is None:
						chain(i + 1						)

		return CHAINS[:number].count(a_						)


if __name__ == "__main__":
					import doctest

					doctest.testmod()
					print(F'''{solution() = }''')



