



import math
from dataclasses import dataclass
from typing import Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, randn_tensor
from .scheduling_utils import SchedulerMixin







@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->UnCLIP
class 				_a				(   _lowerCAmelCase					):
			A      		=	42
			A      		=	None







def  lowerCAmelCase_	(lowerCAmelCase__:		Union[str, Any]		,						lowerCAmelCase__:		List[Any]=0.999		,						lowerCAmelCase__:		Optional[int]="cosine"		,						):





			"""simple docstring"""


			if alpha_transform_type == "cosine":

						def alpha_bar_fn(lowerCAmelCase__:		Optional[Any]  ):
									return math.cos((t + 0.008) / 1.008 * math.pi / 2  ) ** 2

			elif alpha_transform_type == "exp":

						def alpha_bar_fn(lowerCAmelCase__:		Optional[int]  ):
									return math.exp(t * -12.0  )

			else:
						raise ValueError(F'Unsupported alpha_tranform_type: {alpha_transform_type}'  )

			UpperCAmelCase_:		Optional[Any]							      =     []
			for i in range(lowerCAmelCase__  ):
						UpperCAmelCase_:		List[Any]							      =     i / num_diffusion_timesteps
						UpperCAmelCase_:		Union[str, Any]							      =     (i + 1) / num_diffusion_timesteps
						betas.append(min(1 - alpha_bar_fn(lowerCAmelCase__  ) / alpha_bar_fn(lowerCAmelCase__  )		,						lowerCAmelCase__  )  )
			return torch.tensor(lowerCAmelCase__		,						dtype=torch.floataa  )







class 				_a				(   _lowerCAmelCase ,		_lowerCAmelCase					):

			@register_to_config
			def __init__(self,   SCREAMING_SNAKE_CASE_ = 1000,   SCREAMING_SNAKE_CASE_ = "fixed_small_log",   SCREAMING_SNAKE_CASE_ = True,   SCREAMING_SNAKE_CASE_ = 1.0,   SCREAMING_SNAKE_CASE_ = "epsilon",   SCREAMING_SNAKE_CASE_ = "squaredcos_cap_v2",   )       ->   int:
						if beta_schedule != "squaredcos_cap_v2":
									raise ValueError("""UnCLIPScheduler only supports `beta_schedule`: 'squaredcos_cap_v2'"""					)

						UpperCAmelCase_:		Union[str, Any]							      =     betas_for_alpha_bar(SCREAMING_SNAKE_CASE_					)

						UpperCAmelCase_:		Optional[int]							      =     1.0 - self.betas
						UpperCAmelCase_:		int							      =     torch.cumprod(self.alphas,   dim=0					)
						UpperCAmelCase_:		Dict							      =     torch.tensor(1.0					)

						# standard deviation of the initial noise distribution
						UpperCAmelCase_:		Optional[Any]							      =     1.0

						# setable values
						UpperCAmelCase_:		Any							      =     None
						UpperCAmelCase_:		List[Any]							      =     torch.from_numpy(np.arange(0,   SCREAMING_SNAKE_CASE_					)[::-1].copy()					)

						UpperCAmelCase_:		Union[str, Any]							      =     variance_type

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_ = None					)       ->   torch.FloatTensor:
						return sample

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_ = None					)       ->   List[Any]:
						UpperCAmelCase_:		Optional[Any]							      =     num_inference_steps
						UpperCAmelCase_:		List[str]							      =     (self.config.num_train_timesteps - 1) / (self.num_inference_steps - 1)
						UpperCAmelCase_:		str							      =     (np.arange(0,   SCREAMING_SNAKE_CASE_					) * step_ratio).round()[::-1].copy().astype(np.intaa					)
						UpperCAmelCase_:		Union[str, Any]							      =     torch.from_numpy(SCREAMING_SNAKE_CASE_					).to(SCREAMING_SNAKE_CASE_					)

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_=None,   SCREAMING_SNAKE_CASE_=None,   SCREAMING_SNAKE_CASE_=None					)       ->   List[Any]:
						if prev_timestep is None:
									UpperCAmelCase_:		List[Any]							      =     t - 1

						UpperCAmelCase_:		List[str]							      =     self.alphas_cumprod[t]
						UpperCAmelCase_:		int							      =     self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one
						UpperCAmelCase_:		Optional[Any]							      =     1 - alpha_prod_t
						UpperCAmelCase_:		Dict							      =     1 - alpha_prod_t_prev

						if prev_timestep == t - 1:
									UpperCAmelCase_:		Tuple							      =     self.betas[t]
						else:
									UpperCAmelCase_:		List[str]							      =     1 - alpha_prod_t / alpha_prod_t_prev

						# For t > 0, compute predicted variance βt (see formula (6) and (7) from https://arxiv.org/pdf/2006.11239.pdf)
						# and sample from it to get previous sample
						# x_{t-1} ~ N(pred_prev_sample, variance) == add variance to pred_sample
						UpperCAmelCase_:		List[str]							      =     beta_prod_t_prev / beta_prod_t * beta

						if variance_type is None:
									UpperCAmelCase_:		str							      =     self.config.variance_type

						# hacks - were probably added for training stability
						if variance_type == "fixed_small_log":
									UpperCAmelCase_:		List[Any]							      =     torch.log(torch.clamp(SCREAMING_SNAKE_CASE_,   min=1E-20					)					)
									UpperCAmelCase_:		List[str]							      =     torch.exp(0.5 * variance					)
						elif variance_type == "learned_range":
									# NOTE difference with DDPM scheduler
									UpperCAmelCase_:		int							      =     variance.log()
									UpperCAmelCase_:		List[str]							      =     beta.log()

									UpperCAmelCase_:		str							      =     (predicted_variance + 1) / 2
									UpperCAmelCase_:		Optional[int]							      =     frac * max_log + (1 - frac) * min_log

						return variance

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_ = None,   SCREAMING_SNAKE_CASE_=None,   SCREAMING_SNAKE_CASE_ = True,   )       ->   Union[UnCLIPSchedulerOutput, Tuple]:
						UpperCAmelCase_:		List[Any]							      =     timestep

						if model_output.shape[1] == sample.shape[1] * 2 and self.variance_type == "learned_range":
									UpperCAmelCase_      ,							UpperCAmelCase_:		List[str]							      =     torch.split(SCREAMING_SNAKE_CASE_,   sample.shape[1],   dim=1					)
						else:
									UpperCAmelCase_:		Optional[Any]							      =     None

						# 1. compute alphas, betas
						if prev_timestep is None:
									UpperCAmelCase_:		Optional[int]							      =     t - 1

						UpperCAmelCase_:		List[str]							      =     self.alphas_cumprod[t]
						UpperCAmelCase_:		List[Any]							      =     self.alphas_cumprod[prev_timestep] if prev_timestep >= 0 else self.one
						UpperCAmelCase_:		List[str]							      =     1 - alpha_prod_t
						UpperCAmelCase_:		List[Any]							      =     1 - alpha_prod_t_prev

						if prev_timestep == t - 1:
									UpperCAmelCase_:		List[str]							      =     self.betas[t]
									UpperCAmelCase_:		int							      =     self.alphas[t]
						else:
									UpperCAmelCase_:		Optional[Any]							      =     1 - alpha_prod_t / alpha_prod_t_prev
									UpperCAmelCase_:		Optional[int]							      =     1 - beta

						# 2. compute predicted original sample from predicted noise also called
						# "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf
						if self.config.prediction_type == "epsilon":
									UpperCAmelCase_:		int							      =     (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5
						elif self.config.prediction_type == "sample":
									UpperCAmelCase_:		Any							      =     model_output
						else:
									raise ValueError(
									    f'prediction_type given as {self.config.prediction_type} must be one of `epsilon` or `sample`'
									    """ for the UnCLIPScheduler."""					)

						# 3. Clip "predicted x_0"
						if self.config.clip_sample:
									UpperCAmelCase_:		int							      =     torch.clamp(
									    SCREAMING_SNAKE_CASE_,   -self.config.clip_sample_range,   self.config.clip_sample_range					)

						# 4. Compute coefficients for pred_original_sample x_0 and current sample x_t
						# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
						UpperCAmelCase_:		List[Any]							      =     (alpha_prod_t_prev ** 0.5 * beta) / beta_prod_t
						UpperCAmelCase_:		Optional[int]							      =     alpha ** 0.5 * beta_prod_t_prev / beta_prod_t

						# 5. Compute predicted previous sample µ_t
						# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
						UpperCAmelCase_:		Dict							      =     pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample

						# 6. Add noise
						UpperCAmelCase_:		Any							      =     0
						if t > 0:
									UpperCAmelCase_:		int							      =     randn_tensor(
									    model_output.shape,   dtype=model_output.dtype,   generator=SCREAMING_SNAKE_CASE_,   device=model_output.device					)

									UpperCAmelCase_:		Tuple							      =     self._get_variance(
									    SCREAMING_SNAKE_CASE_,   predicted_variance=SCREAMING_SNAKE_CASE_,   prev_timestep=SCREAMING_SNAKE_CASE_,   )

									if self.variance_type == "fixed_small_log":
												UpperCAmelCase_:		Any							      =     variance
									elif self.variance_type == "learned_range":
												UpperCAmelCase_:		Optional[Any]							      =     (0.5 * variance).exp()
									else:
												raise ValueError(
												    f'variance_type given as {self.variance_type} must be one of `fixed_small_log` or `learned_range`'
												    """ for the UnCLIPScheduler."""					)

									UpperCAmelCase_:		Tuple							      =     variance * variance_noise

						UpperCAmelCase_:		Tuple							      =     pred_prev_sample + variance

						if not return_dict:
									return (pred_prev_sample,)

						return UnCLIPSchedulerOutput(prev_sample=SCREAMING_SNAKE_CASE_,   pred_original_sample=SCREAMING_SNAKE_CASE_					)

			def      __snake_case       (self,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_,   )       ->   torch.FloatTensor:
						# Make sure alphas_cumprod and timestep have same device and dtype as original_samples
						UpperCAmelCase_:		List[Any]							      =     self.alphas_cumprod.to(device=original_samples.device,   dtype=original_samples.dtype					)
						UpperCAmelCase_:		Optional[Any]							      =     timesteps.to(original_samples.device					)

						UpperCAmelCase_:		List[Any]							      =     alphas_cumprod[timesteps] ** 0.5
						UpperCAmelCase_:		Union[str, Any]							      =     sqrt_alpha_prod.flatten()
						while len(sqrt_alpha_prod.shape					) < len(original_samples.shape					):
									UpperCAmelCase_:		Optional[int]							      =     sqrt_alpha_prod.unsqueeze(-1					)

						UpperCAmelCase_:		List[str]							      =     (1 - alphas_cumprod[timesteps]) ** 0.5
						UpperCAmelCase_:		Union[str, Any]							      =     sqrt_one_minus_alpha_prod.flatten()
						while len(sqrt_one_minus_alpha_prod.shape					) < len(original_samples.shape					):
									UpperCAmelCase_:		Any							      =     sqrt_one_minus_alpha_prod.unsqueeze(-1					)

						UpperCAmelCase_:		Optional[int]							      =     sqrt_alpha_prod * original_samples + sqrt_one_minus_alpha_prod * noise
						return noisy_samples











from __future__ import annotations







def  lowerCAmelCase_	(lowerCAmelCase__:		int | str  ):





			"""simple docstring"""


			UpperCAmelCase_:		Optional[int]							      =     str(lowerCAmelCase__  )
			return n == n[::-1]







def  lowerCAmelCase_	(lowerCAmelCase__:		int = 1_0_0_0_0_0_0  ):





			"""simple docstring"""


			UpperCAmelCase_:		int							      =     0

			for i in range(1		,						lowerCAmelCase__  ):
						if is_palindrome(lowerCAmelCase__  ) and is_palindrome(bin(lowerCAmelCase__  ).split("""b"""  )[1]  ):
									total += i
			return total


if __name__ == "__main__":
	print(solution(int(str(input().strip()))))






