import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
      import jax

      from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
      from PIL import Image

      from transformers import BeitImageProcessor





class 					A							(    unittest.TestCase					):


   def __init__(self					:						Optional[Any]	,  __UpperCAmelCase					:						Optional[Any]	,  __UpperCAmelCase					:						Tuple=1_0_0	,  __UpperCAmelCase					:						Dict=1_3	,  __UpperCAmelCase					:						Union[str, Any]=3_0	,  __UpperCAmelCase					:						str=2	,  __UpperCAmelCase					:						Optional[Any]=3	,  __UpperCAmelCase					:						Optional[int]=True	,  __UpperCAmelCase					:						Tuple=True	,  __UpperCAmelCase					:						Tuple=3_2	,  __UpperCAmelCase					:						Dict=5	,  __UpperCAmelCase					:						Optional[Any]=4	,  __UpperCAmelCase					:						Any=3_7	,  __UpperCAmelCase					:						List[str]="gelu"	,  __UpperCAmelCase					:						Any=0.1	,  __UpperCAmelCase					:						Tuple=0.1	,  __UpperCAmelCase					:						Optional[int]=1_0	,  __UpperCAmelCase					:						Tuple=0.02	,  __UpperCAmelCase					:						str=3	,  )						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  vocab_size
         UpperCAmelCase__								=  batch_size
         UpperCAmelCase__								=  image_size
         UpperCAmelCase__								=  patch_size
         UpperCAmelCase__								=  num_channels
         UpperCAmelCase__								=  is_training
         UpperCAmelCase__								=  use_labels
         UpperCAmelCase__								=  hidden_size
         UpperCAmelCase__								=  num_hidden_layers
         UpperCAmelCase__								=  num_attention_heads
         UpperCAmelCase__								=  intermediate_size
         UpperCAmelCase__								=  hidden_act
         UpperCAmelCase__								=  hidden_dropout_prob
         UpperCAmelCase__								=  attention_probs_dropout_prob
         UpperCAmelCase__								=  type_sequence_label_size
         UpperCAmelCase__								=  initializer_range

         # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
         UpperCAmelCase__								=  (image_size // patch_size) ** 2
         UpperCAmelCase__								=  num_patches + 1


   def       lowercase_				(self					:						Any		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         UpperCAmelCase__								=  None
         if self.use_labels:
               UpperCAmelCase__								=  ids_tensor([self.batch_size]	,  self.type_sequence_label_size		)

         UpperCAmelCase__								=  BeitConfig(
             vocab_size=self.vocab_size	,  image_size=self.image_size	,  patch_size=self.patch_size	,  num_channels=self.num_channels	,  hidden_size=self.hidden_size	,  num_hidden_layers=self.num_hidden_layers	,  num_attention_heads=self.num_attention_heads	,  intermediate_size=self.intermediate_size	,  hidden_act=self.hidden_act	,  hidden_dropout_prob=self.hidden_dropout_prob	,  attention_probs_dropout_prob=self.attention_probs_dropout_prob	,  is_decoder=__UpperCAmelCase	,  initializer_range=self.initializer_range	,  )

         return config, pixel_values, labels


   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						Optional[Any]	,  __UpperCAmelCase					:						List[str]	,  __UpperCAmelCase					:						Any		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitModel(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)		)


   def       lowercase_				(self					:						List[str]	,  __UpperCAmelCase					:						Optional[Any]	,  __UpperCAmelCase					:						Optional[int]	,  __UpperCAmelCase					:						Tuple		)						->    List[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitForMaskedImageModeling(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length - 1, self.vocab_size)		)


   def       lowercase_				(self					:						Union[str, Any]	,  __UpperCAmelCase					:						Optional[int]	,  __UpperCAmelCase					:						str	,  __UpperCAmelCase					:						Optional[int]		)						->    str:
         """simple docstring"""



         UpperCAmelCase__								=  self.type_sequence_label_size
         UpperCAmelCase__								=  FlaxBeitForImageClassification(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)		)

         # test greyscale images
         UpperCAmelCase__								=  1
         UpperCAmelCase__								=  FlaxBeitForImageClassification(__UpperCAmelCase		)

         UpperCAmelCase__								=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)





   def       lowercase_				(self					:						int		)						->    List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase__
         )		,				(
             UpperCAmelCase__
         )		,				(
             UpperCAmelCase__
         )		,				
         )								=  config_and_inputs
         UpperCAmelCase__								=  {"pixel_values": pixel_values}
         return config, inputs_dict





@require_flax
class 					A							(    UpperCAmelCase_		,    unittest.TestCase					):
   __UpperCAmelCase			:			Optional[int]              =      (
       (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
   )


   def       lowercase_				(self					:						int		)						->    None:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self	,  config_class=__UpperCAmelCase	,  has_text_modality=__UpperCAmelCase	,  hidden_size=3_7		)


   def       lowercase_				(self					:						List[Any]		)						->    Any:
         """simple docstring"""



         self.config_tester.run_common_tests()


   def       lowercase_				(self					:						Optional[int]		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               UpperCAmelCase__								=  model_class(__UpperCAmelCase		)
               UpperCAmelCase__								=  inspect.signature(model.__call__		)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase__								=  [*signature.parameters.keys()]

               UpperCAmelCase__								=  ["pixel_values"]
               self.assertListEqual(arg_names[:1]	,  __UpperCAmelCase		)


   def       lowercase_				(self					:						List[str]		)						->    Dict:
         """simple docstring"""



         UpperCAmelCase__		,				UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__		):
                     UpperCAmelCase__								=  self._prepare_for_class(__UpperCAmelCase	,  __UpperCAmelCase		)
                     UpperCAmelCase__								=  model_class(__UpperCAmelCase		)

                     @jax.jit
                     def model_jitted(__UpperCAmelCase					:						Dict	,  **__UpperCAmelCase					:						Union[str, Any]		):
                           return model(pixel_values=__UpperCAmelCase	,  **__UpperCAmelCase		)

                     with self.subTest("JIT Enabled"		):
                           UpperCAmelCase__								=  model_jitted(**__UpperCAmelCase		).to_tuple()

                     with self.subTest("JIT Disabled"		):
                           with jax.disable_jit():
                                 UpperCAmelCase__								=  model_jitted(**__UpperCAmelCase		).to_tuple()

                     self.assertEqual(len(__UpperCAmelCase		)	,  len(__UpperCAmelCase		)		)
                     for jitted_output, output in zip(__UpperCAmelCase	,  __UpperCAmelCase		):
                           self.assertEqual(jitted_output.shape	,  output.shape		)


   def       lowercase_				(self					:						Dict		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


   def       lowercase_				(self					:						Dict		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase		)


   def       lowercase_				(self					:						Dict		)						->    Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase		)





   @slow
   def       lowercase_				(self					:						int		)						->    Dict:
         """simple docstring"""



         for model_class_name in self.all_model_classes:
               UpperCAmelCase__								=  model_class_name.from_pretrained("microsoft/beit-base-patch16-224"		)
               UpperCAmelCase__								=  model(np.ones((1, 3, 2_2_4, 2_2_4)		)		)
               self.assertIsNotNone(__UpperCAmelCase		)




def        lowerCAmelCase_			(					)					->    int:







      '''simple docstring'''


      UpperCAmelCase__								=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
      return image







@require_vision
@require_flax
class 					A							(    unittest.TestCase					):


   @cached_property
   def       lowercase_				(self					:						str		)						->    Optional[int]:
         """simple docstring"""



         return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"		) if is_vision_available() else None


   @slow
   def       lowercase_				(self					:						str		)						->    Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"		)

         UpperCAmelCase__								=  self.default_image_processor
         UpperCAmelCase__								=  prepare_img()
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase	,  return_tensors="np"		).pixel_values

         # prepare bool_masked_pos
         UpperCAmelCase__								=  np.ones((1, 1_9_6)	,  dtype=__UpperCAmelCase		)

         # forward pass
         UpperCAmelCase__								=  model(pixel_values=__UpperCAmelCase	,  bool_masked_pos=__UpperCAmelCase		)
         UpperCAmelCase__								=  outputs.logits

         # verify the logits
         UpperCAmelCase__								=  (1, 1_9_6, 8_1_9_2)
         self.assertEqual(logits.shape	,  __UpperCAmelCase		)

         UpperCAmelCase__								=  np.array(
             [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]		)

         self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]	,  __UpperCAmelCase	,  atol=1E-2		)		)


   @slow
   def       lowercase_				(self					:						Tuple		)						->    Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"		)

         UpperCAmelCase__								=  self.default_image_processor
         UpperCAmelCase__								=  prepare_img()
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase	,  return_tensors="np"		)

         # forward pass
         UpperCAmelCase__								=  model(**__UpperCAmelCase		)
         UpperCAmelCase__								=  outputs.logits

         # verify the logits
         UpperCAmelCase__								=  (1, 1_0_0_0)
         self.assertEqual(logits.shape	,  __UpperCAmelCase		)

         UpperCAmelCase__								=  np.array([-1.2385, -1.0987, -1.0108]		)

         self.assertTrue(np.allclose(logits[0, :3]	,  __UpperCAmelCase	,  atol=1E-4		)		)

         UpperCAmelCase__								=  2_8_1
         self.assertEqual(logits.argmax(-1		).item()	,  __UpperCAmelCase		)





   @slow
   def       lowercase_				(self					:						List[str]		)						->    List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  FlaxBeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"		)

         UpperCAmelCase__								=  self.default_image_processor
         UpperCAmelCase__								=  prepare_img()
         UpperCAmelCase__								=  image_processor(images=__UpperCAmelCase	,  return_tensors="np"		)

         # forward pass
         UpperCAmelCase__								=  model(**__UpperCAmelCase		)
         UpperCAmelCase__								=  outputs.logits

         # verify the logits
         UpperCAmelCase__								=  (1, 2_1_8_4_1)
         self.assertEqual(logits.shape	,  __UpperCAmelCase		)

         UpperCAmelCase__								=  np.array([1.6881, -0.2787, 0.5901]		)

         self.assertTrue(np.allclose(logits[0, :3]	,  __UpperCAmelCase	,  atol=1E-4		)		)

         UpperCAmelCase__								=  2_3_9_6
         self.assertEqual(logits.argmax(-1		).item()	,  __UpperCAmelCase		)




from __future__ import annotations




def        lowerCAmelCase_			(					__A							)					->    bool:







      '''simple docstring'''


      UpperCAmelCase__								=  str(__A							)
      return n == n[::-1]




def        lowerCAmelCase_			(					__A = 1_000_000							)					->    Optional[int]:







      '''simple docstring'''


      UpperCAmelCase__								=  0

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


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



