
"""simple docstring"""

from typing import Union

import torch
from PIL import Image
from torchvision import transforms as tfms
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)

class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def __init__(      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	):

      '''simple docstring'''




      super().__init__()

      self.register_modules(vae=__UpperCAmelCase      ,	text_encoder=__UpperCAmelCase      ,	tokenizer=__UpperCAmelCase      ,	unet=__UpperCAmelCase      ,	scheduler=__UpperCAmelCase )





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase ):

      '''simple docstring'''




      with torch.no_grad():
       __UpperCamelCase							    =			self.vae.encode(tfms.ToTensor()(__UpperCAmelCase ).unsqueeze(0 ).to(self.device ) * 2 - 1 )
       __UpperCamelCase							    =			0.1_8_2_1_5 * latent.latent_dist.sample()
      return latent





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			(1 / 0.1_8_2_1_5) * latent
      with torch.no_grad():
       __UpperCamelCase							    =			self.vae.decode(__UpperCAmelCase ).sample
      __UpperCamelCase							    =			(img / 2 + 0.5).clamp(0      ,	1 )
      __UpperCamelCase							    =			img.detach().cpu().permute(0      ,	2      ,	3      ,	1 ).numpy()
      __UpperCamelCase							    =			(img * 255).round().astype('uint8' )
      return Image.fromarray(img[0] )





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.tokenizer(
          __UpperCAmelCase      ,	padding='max_length'      ,	max_length=self.tokenizer.model_max_length      ,	truncation=__UpperCAmelCase      ,	return_tensors='pt'      ,	)

      __UpperCamelCase							    =			self.text_encoder(text_input.input_ids.to(self.device ) )[0]

      __UpperCamelCase							    =			self.tokenizer(
          ''      ,	padding='max_length'      ,	max_length=self.tokenizer.model_max_length      ,	truncation=__UpperCAmelCase      ,	return_tensors='pt'      ,	)

      __UpperCamelCase							    =			self.text_encoder(uncond_input.input_ids.to(self.device ) )[0]

      return torch.cat([uncond_embedding, text_embedding] )







     def __call__(      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase = 0.3      ,	__UpperCAmelCase = 0.6      ,	__UpperCAmelCase = 0.5      ,	__UpperCAmelCase = 42      ,	__UpperCAmelCase = 50      ,	__UpperCAmelCase = 7.5      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			steps - int(kmin * steps )
      __UpperCamelCase							    =			steps - int(kmax * steps )

      __UpperCamelCase							    =			self.prep_text(__UpperCAmelCase )

      self.scheduler.set_timesteps(__UpperCAmelCase )

      __UpperCamelCase    ,      __UpperCamelCase							    =			img.size
      __UpperCamelCase							    =			self.encode(__UpperCAmelCase )

      torch.manual_seed(__UpperCAmelCase )
      __UpperCamelCase							    =			torch.randn(
          (1, self.unet.config.in_channels, height // 8, width // 8)      ,	).to(self.device )

      __UpperCamelCase							    =			self.scheduler.add_noise(
          __UpperCAmelCase      ,	__UpperCAmelCase      ,	timesteps=self.scheduler.timesteps[tmax]      ,	)

      __UpperCamelCase							    =			torch.cat([latents] * 2 )

      __UpperCamelCase							    =			self.scheduler.scale_model_input(__UpperCAmelCase      ,	self.scheduler.timesteps[tmax] )

      with torch.no_grad():
       __UpperCamelCase							    =			self.unet(
           __UpperCAmelCase      ,	self.scheduler.timesteps[tmax]      ,	encoder_hidden_states=__UpperCAmelCase      ,	).sample

      __UpperCamelCase    ,      __UpperCamelCase							    =			pred.chunk(2 )
      __UpperCamelCase							    =			pred_uncond + guidance_scale * (pred_text - pred_uncond)

      __UpperCamelCase							    =			self.scheduler.step(__UpperCAmelCase      ,	self.scheduler.timesteps[tmax]      ,	__UpperCAmelCase ).prev_sample

      for i, t in enumerate(tqdm(self.scheduler.timesteps ) ):
       if i > tmax:
        if i < tmin:  # layout generation phase
         __UpperCamelCase							    =			self.scheduler.add_noise(
             __UpperCAmelCase      ,	__UpperCAmelCase      ,	timesteps=__UpperCAmelCase      ,	)

         __UpperCamelCase							    =			(mix_factor * latents) + (
             1 - mix_factor
         ) * orig_latents  # interpolating between layout noise and conditionally generated noise to preserve layout sematics
         __UpperCamelCase							    =			torch.cat([input] * 2 )

        else:  # content generation phase
         __UpperCamelCase							    =			torch.cat([latents] * 2 )

        __UpperCamelCase							    =			self.scheduler.scale_model_input(__UpperCAmelCase      ,	__UpperCAmelCase )

        with torch.no_grad():
         __UpperCamelCase							    =			self.unet(
             __UpperCAmelCase      ,	__UpperCAmelCase      ,	encoder_hidden_states=__UpperCAmelCase      ,	).sample

        __UpperCamelCase    ,      __UpperCamelCase							    =			pred.chunk(2 )
        __UpperCamelCase							    =			pred_uncond + guidance_scale * (pred_text - pred_uncond)

        __UpperCamelCase							    =			self.scheduler.step(__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase ).prev_sample

      return self.decode(__UpperCAmelCase )





"""simple docstring"""

import math
from itertools import permutations






def 				A		(							snake_case :int						) ->      bool:

 if 1 < number < 4:
  # 2 and 3 are primes
  return True
 elif number < 2 or number % 2 == 0 or number % 3 == 0:
  # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
  return False

 # All primes number are in format of 6k +/- 1
 for i in range(5							,   int(math.sqrt(snake_case						) + 1						)							,   6						):
  if number % i == 0 or number % (i + 2) == 0:
   return False
 return True






def 				A		(							snake_case :int							,   snake_case :list						) ->      bool:
 __UpperCamelCase    ,      __UpperCamelCase							    =			0, len(snake_case						) - 1
 while left <= right:
  __UpperCamelCase							    =			(left + right) // 2
  if prime_list[middle] == target:
   return True
  elif prime_list[middle] < target:
   __UpperCamelCase							    =			middle + 1
  else:
   __UpperCamelCase							    =			middle - 1

 return False






def 				A		(							) ->      int:
 __UpperCamelCase							    =			[n for n in range(1_0_0_1							,   1_0_0_0_0							,   2						) if is_prime(snake_case						)]
 __UpperCamelCase							    =			[]

 for number in prime_list:
  __UpperCamelCase							    =			[]

  for prime_member in permutations(list(str(snake_case						)						)						):
   __UpperCamelCase							    =			int(''.join(snake_case						)						)

   if prime % 2 == 0:
    continue

   if search(snake_case							,   snake_case						):
    tmp_numbers.append(snake_case						)

  tmp_numbers.sort()
  if len(snake_case						) >= 3:
   candidates.append(snake_case						)

 __UpperCamelCase							    =			[]
 for candidate in candidates:
  __UpperCamelCase							    =			len(snake_case						)
  __UpperCamelCase							    =			False

  for i in range(snake_case						):
   for j in range(i + 1							,   snake_case						):
    for k in range(j + 1							,   snake_case						):
     if (
         abs(candidate[i] - candidate[j]						)
         == abs(candidate[j] - candidate[k]						)
         and len({candidate[i], candidate[j], candidate[k]}						) == 3
     ):
      passed.append(
          sorted([candidate[i], candidate[j], candidate[k]]						)						)
      __UpperCamelCase							    =			True

     if found:
      break
    if found:
     break
   if found:
    break

 __UpperCamelCase							    =			set()
 for seq in passed:
  answer.add(''.join([str(snake_case						) for i in seq]						)						)

 return max(int(snake_case						) for x in answer						)


if __name__ == "__main__":
      print(solution())



