

'''simple docstring'''

import colorsys
import io

import cva
import matplotlib as mpl
import matplotlib.colors as mplc
import matplotlib.figure as mplfigure
import numpy as np
import torch
from matplotlib.backends.backend_agg import FigureCanvasAgg

from utils import img_tensorize


A_      =							10_00





class 							_snake_case	:
   def __init__(	self     : Optional[Any]       ,SCREAMING_SNAKE_CASE__     : Tuple       ,SCREAMING_SNAKE_CASE__     : List[Any]=1.2       ,SCREAMING_SNAKE_CASE__     : Optional[int]="g"       ,SCREAMING_SNAKE_CASE__     : str=0.5       ,SCREAMING_SNAKE_CASE__     : Optional[Any]="-"       ,SCREAMING_SNAKE_CASE__     : Tuple="test_out.jpg"       ,SCREAMING_SNAKE_CASE__     : Optional[int]=True       ,SCREAMING_SNAKE_CASE__     : Dict=False       ,SCREAMING_SNAKE_CASE__     : List[str]=None       ,SCREAMING_SNAKE_CASE__     : List[Any]=None       ,SCREAMING_SNAKE_CASE__     : Union[str, Any]=0.7       ,):
     if isinstance(SCREAMING_SNAKE_CASE__       ,torch.Tensor							):
       SCREAMING_SNAKE_CASE:Tuple   =   img.numpy().astype("np.uint8"							)
     if isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							):
       SCREAMING_SNAKE_CASE:Dict   =   img_tensorize(SCREAMING_SNAKE_CASE__							)
     assert isinstance(SCREAMING_SNAKE_CASE__       ,np.ndarray							)

     SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:List[Any]   =   img.shape[1], img.shape[0]
     SCREAMING_SNAKE_CASE:Any   =   mplfigure.Figure(frameon=SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   fig.get_dpi()
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   (width * scale + 1e-2) / dpi
     SCREAMING_SNAKE_CASE:Optional[int]   =   (height * scale + 1e-2) / dpi
     fig.set_size_inches(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Any   =   fig.add_axes([0.0, 0.0, 1.0, 1.0]							)
     ax.axis("off"							)
     ax.set_xlim(0.0       ,SCREAMING_SNAKE_CASE__							)
     ax.set_ylim(SCREAMING_SNAKE_CASE__							)

     SCREAMING_SNAKE_CASE:str   =   saveas
     SCREAMING_SNAKE_CASE:List[Any]   =   rgb
     SCREAMING_SNAKE_CASE:Optional[Any]   =   pynb
     SCREAMING_SNAKE_CASE:Optional[Any]   =   img
     SCREAMING_SNAKE_CASE:List[Any]   =   edgecolor
     SCREAMING_SNAKE_CASE:List[Any]   =   0.5
     SCREAMING_SNAKE_CASE:int   =   linestyle
     SCREAMING_SNAKE_CASE:List[str]   =   int(np.sqrt(min(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)							) * scale // 3							)
     SCREAMING_SNAKE_CASE:Tuple   =   width
     SCREAMING_SNAKE_CASE:List[str]   =   height
     SCREAMING_SNAKE_CASE:Any   =   scale
     SCREAMING_SNAKE_CASE:str   =   fig
     SCREAMING_SNAKE_CASE:Optional[Any]   =   ax
     SCREAMING_SNAKE_CASE:Optional[int]   =   pad
     SCREAMING_SNAKE_CASE:int   =   idaobj
     SCREAMING_SNAKE_CASE:List[Any]   =   idaattr
     SCREAMING_SNAKE_CASE:Dict   =   FigureCanvasAgg(SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Optional[int]       ,SCREAMING_SNAKE_CASE__     : Tuple=None							):
     if color is None:
       SCREAMING_SNAKE_CASE:Optional[int]   =   self.edgecolor
     ((SCREAMING_SNAKE_CASE) ,      (SCREAMING_SNAKE_CASE) ,      (SCREAMING_SNAKE_CASE) ,      (SCREAMING_SNAKE_CASE)):Optional[Any]   =   box
     SCREAMING_SNAKE_CASE:List[Any]   =   xa - xa
     SCREAMING_SNAKE_CASE:int   =   ya - ya
     self.ax.add_patch(
         mpl.patches.Rectangle(
             (xa, ya)       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,fill=SCREAMING_SNAKE_CASE__       ,edgecolor=SCREAMING_SNAKE_CASE__       ,linewidth=self.font_size // 3       ,alpha=self.alpha       ,linestyle=self.linestyle       ,)							)
   def       __UpperCamelCase  (	self     : Optional[Any]       ,SCREAMING_SNAKE_CASE__     : int       ,SCREAMING_SNAKE_CASE__     : List[str]=None       ,SCREAMING_SNAKE_CASE__     : Tuple=None       ,SCREAMING_SNAKE_CASE__     : str=None       ,SCREAMING_SNAKE_CASE__     : Union[str, Any]=None							):
     if len(boxes.shape							) > 2:
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   boxes[0]
     if len(obj_ids.shape							) > 1:
       SCREAMING_SNAKE_CASE:str   =   obj_ids[0]
     if len(obj_scores.shape							) > 1:
       SCREAMING_SNAKE_CASE:int   =   obj_scores[0]
     if len(attr_ids.shape							) > 1:
       SCREAMING_SNAKE_CASE:int   =   attr_ids[0]
     if len(attr_scores.shape							) > 1:
       SCREAMING_SNAKE_CASE:int   =   attr_scores[0]
     if isinstance(SCREAMING_SNAKE_CASE__       ,torch.Tensor							):
       SCREAMING_SNAKE_CASE:Optional[Any]   =   boxes.numpy()
     if isinstance(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							):
       SCREAMING_SNAKE_CASE:str   =   np.array(SCREAMING_SNAKE_CASE__							)
     assert isinstance(SCREAMING_SNAKE_CASE__       ,np.ndarray							)
     SCREAMING_SNAKE_CASE:int   =   np.prod(boxes[:, 2:] - boxes[:, :2]       ,axis=1							)
     SCREAMING_SNAKE_CASE:List[str]   =   np.argsort(-areas							).tolist()
     SCREAMING_SNAKE_CASE:Any   =   boxes[sorted_idxs] if boxes is not None else None
     SCREAMING_SNAKE_CASE:Dict   =   obj_ids[sorted_idxs] if obj_ids is not None else None
     SCREAMING_SNAKE_CASE:Optional[int]   =   obj_scores[sorted_idxs] if obj_scores is not None else None
     SCREAMING_SNAKE_CASE:Dict   =   attr_ids[sorted_idxs] if attr_ids is not None else None
     SCREAMING_SNAKE_CASE:int   =   attr_scores[sorted_idxs] if attr_scores is not None else None

     SCREAMING_SNAKE_CASE:List[Any]   =   [self._random_color(maximum=1							) for _ in range(len(SCREAMING_SNAKE_CASE__							)							)]
     SCREAMING_SNAKE_CASE:Optional[int]   =   [assigned_colors[idx] for idx in sorted_idxs]
     if obj_ids is not None:
       SCREAMING_SNAKE_CASE:str   =   self._create_text_labels_attr(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
       for i in range(len(SCREAMING_SNAKE_CASE__							)							):
         SCREAMING_SNAKE_CASE:int   =   assigned_colors[i]
         self.add_box(boxes[i]       ,SCREAMING_SNAKE_CASE__							)
         self.draw_labels(labels[i]       ,boxes[i]       ,SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Any       ,SCREAMING_SNAKE_CASE__     : Tuple       ,SCREAMING_SNAKE_CASE__     : Any       ,SCREAMING_SNAKE_CASE__     : str							):
     SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Optional[int]   =   box
     SCREAMING_SNAKE_CASE:str   =   (xa, ya)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   (ya - ya) * (xa - xa)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   _SMALL_OBJ * self.scale
     if instance_area < small or ya - ya < 40 * self.scale:
       if ya >= self.height - 5:
         SCREAMING_SNAKE_CASE:Optional[Any]   =   (xa, ya)
       else:
         SCREAMING_SNAKE_CASE:Dict   =   (xa, ya)

     SCREAMING_SNAKE_CASE:Optional[int]   =   (ya - ya) / np.sqrt(self.height * self.width							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   self._change_color_brightness(SCREAMING_SNAKE_CASE__       ,brightness_factor=0.7							)
     SCREAMING_SNAKE_CASE:Optional[int]   =   np.clip((height_ratio - 0.02) / 0.08 + 1       ,1.2       ,2							)
     font_size *= 0.75 * self.font_size

     self.draw_text(
         text=SCREAMING_SNAKE_CASE__       ,position=SCREAMING_SNAKE_CASE__       ,color=SCREAMING_SNAKE_CASE__       ,)
   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Any       ,SCREAMING_SNAKE_CASE__     : List[Any]       ,SCREAMING_SNAKE_CASE__     : Optional[Any]="g"       ,SCREAMING_SNAKE_CASE__     : Tuple="left"       ,):
     SCREAMING_SNAKE_CASE:Tuple   =   0
     SCREAMING_SNAKE_CASE:List[str]   =   self.font_size
     SCREAMING_SNAKE_CASE:Dict   =   np.maximum(list(mplc.to_rgb(SCREAMING_SNAKE_CASE__							)							)       ,0.2							)
     SCREAMING_SNAKE_CASE:Optional[Any]   =   max(0.8       ,np.max(SCREAMING_SNAKE_CASE__							)							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   {
         "facecolor": "black",
         "alpha": self.alpha,
         "pad": self.pad,
         "edgecolor": "none",
     }
     SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Any   =   position
     self.ax.text(
         SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,size=font_size * self.scale       ,family="sans-serif"       ,bbox=SCREAMING_SNAKE_CASE__       ,verticalalignment="top"       ,horizontalalignment=SCREAMING_SNAKE_CASE__       ,color=SCREAMING_SNAKE_CASE__       ,zorder=10       ,rotation=SCREAMING_SNAKE_CASE__       ,)
   def       __UpperCamelCase  (	self     : List[Any]       ,SCREAMING_SNAKE_CASE__     : str=None							):
     if saveas is None:
       SCREAMING_SNAKE_CASE:Optional[Any]   =   self.saveas
     if saveas.lower().endswith(".jpg"							) or saveas.lower().endswith(".png"							):
       cva.imwrite(
           SCREAMING_SNAKE_CASE__       ,self._get_buffer()[:, :, ::-1]       ,)
     else:
       self.fig.savefig(SCREAMING_SNAKE_CASE__							)
   def       __UpperCamelCase  (	self     : Optional[int]       ,SCREAMING_SNAKE_CASE__     : str       ,SCREAMING_SNAKE_CASE__     : Dict       ,SCREAMING_SNAKE_CASE__     : Optional[int]       ,SCREAMING_SNAKE_CASE__     : Dict							):
     SCREAMING_SNAKE_CASE:str   =   [self.idaobj[i] for i in classes]
     SCREAMING_SNAKE_CASE:Dict   =   [self.idaattr[i] for i in attr_classes]
     SCREAMING_SNAKE_CASE:int   =   [
         F'''{label} {score:.2f} {attr} {attr_score:.2f}'''
         for label, score, attr, attr_score in zip(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)
     ]
     return labels
   def       __UpperCamelCase  (	self     : List[str]       ,SCREAMING_SNAKE_CASE__     : Optional[int]       ,SCREAMING_SNAKE_CASE__     : List[str]							):
     SCREAMING_SNAKE_CASE:Any   =   [self.idaobj[i] for i in classes]
     if scores is not None:
       if labels is None:
         SCREAMING_SNAKE_CASE:List[Any]   =   ["{:.0f}%".format(s * 100							) for s in scores]
       else:
         SCREAMING_SNAKE_CASE:Any   =   ["{} {:.0f}%".format(SCREAMING_SNAKE_CASE__       ,s * 100							) for li, s in zip(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__							)]
     return labels
   def       __UpperCamelCase  (	self     : List[Any]       ,SCREAMING_SNAKE_CASE__     : Tuple=255							):
     SCREAMING_SNAKE_CASE:str   =   np.random.randint(0       ,len(_COLORS							)							)
     SCREAMING_SNAKE_CASE:Any   =   _COLORS[idx] * maximum
     if not self.rgb:
       SCREAMING_SNAKE_CASE:int   =   ret[::-1]
     return ret
   def       __UpperCamelCase  (	self     : Optional[int]							):
     if not self.pynb:
       SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Optional[int]   =   self.canvas.print_to_buffer()
       if (width, height) != (self.width, self.height):
         SCREAMING_SNAKE_CASE:str   =   cva.resize(self.img       ,(width, height)							)
       else:
         SCREAMING_SNAKE_CASE:Any   =   self.img
     else:
       SCREAMING_SNAKE_CASE:int   =   io.BytesIO()  # works for cairo backend
       self.canvas.print_rgba(SCREAMING_SNAKE_CASE__							)
       SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Optional[Any]   =   self.width, self.height
       SCREAMING_SNAKE_CASE:Union[str, Any]   =   buf.getvalue()
       SCREAMING_SNAKE_CASE:str   =   self.img

     SCREAMING_SNAKE_CASE:List[Any]   =   np.frombuffer(SCREAMING_SNAKE_CASE__       ,dtype="uint8"							)
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   buffer.reshape(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,4							)
     SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE:Tuple   =   np.split(SCREAMING_SNAKE_CASE__       ,[3]       ,axis=2							)

     try:
       import numexpr as ne  # fuse them with numexpr

       SCREAMING_SNAKE_CASE:Tuple   =   ne.evaluate("img * (1 - alpha / 255.0) + rgb * (alpha / 255.0)"							)
     except ImportError:
       SCREAMING_SNAKE_CASE:int   =   alpha.astype("float32"							) / 255.0
       SCREAMING_SNAKE_CASE:Optional[int]   =   img * (1 - alpha) + rgb * alpha

     return visualized_image.astype("uint8"							)







   def       __UpperCamelCase  (	self     : int       ,SCREAMING_SNAKE_CASE__     : Dict       ,SCREAMING_SNAKE_CASE__     : Optional[int]							):
     assert brightness_factor >= -1.0 and brightness_factor <= 1.0
     SCREAMING_SNAKE_CASE:str   =   mplc.to_rgb(SCREAMING_SNAKE_CASE__							)
     SCREAMING_SNAKE_CASE:Tuple   =   colorsys.rgb_to_hls(*mplc.to_rgb(SCREAMING_SNAKE_CASE__							)							)
     SCREAMING_SNAKE_CASE:List[Any]   =   polygon_color[1] + (brightness_factor * polygon_color[1])
     SCREAMING_SNAKE_CASE:Union[str, Any]   =   0.0 if modified_lightness < 0.0 else modified_lightness
     SCREAMING_SNAKE_CASE:int   =   1.0 if modified_lightness > 1.0 else modified_lightness
     SCREAMING_SNAKE_CASE:List[str]   =   colorsys.hls_to_rgb(polygon_color[0]       ,SCREAMING_SNAKE_CASE__       ,polygon_color[2]							)
     return modified_color



A_      =							(
    np.array(
        [
            0.000,
            0.447,
            0.741,
            0.850,
            0.325,
            0.098,
            0.929,
            0.694,
            0.125,
            0.494,
            0.184,
            0.556,
            0.466,
            0.674,
            0.188,
            0.301,
            0.745,
            0.933,
            0.635,
            0.078,
            0.184,
            0.300,
            0.300,
            0.300,
            0.600,
            0.600,
            0.600,
            1.000,
            0.000,
            0.000,
            1.000,
            0.500,
            0.000,
            0.749,
            0.749,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.333,
            0.333,
            0.000,
            0.333,
            0.667,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            0.333,
            0.000,
            0.667,
            0.667,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.667,
            0.000,
            1.000,
            1.000,
            0.000,
            0.000,
            0.333,
            0.500,
            0.000,
            0.667,
            0.500,
            0.000,
            1.000,
            0.500,
            0.333,
            0.000,
            0.500,
            0.333,
            0.333,
            0.500,
            0.333,
            0.667,
            0.500,
            0.333,
            1.000,
            0.500,
            0.667,
            0.000,
            0.500,
            0.667,
            0.333,
            0.500,
            0.667,
            0.667,
            0.500,
            0.667,
            1.000,
            0.500,
            1.000,
            0.000,
            0.500,
            1.000,
            0.333,
            0.500,
            1.000,
            0.667,
            0.500,
            1.000,
            1.000,
            0.500,
            0.000,
            0.333,
            1.000,
            0.000,
            0.667,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            0.000,
            1.000,
            0.333,
            0.333,
            1.000,
            0.333,
            0.667,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            0.000,
            1.000,
            0.667,
            0.333,
            1.000,
            0.667,
            0.667,
            1.000,
            0.667,
            1.000,
            1.000,
            1.000,
            0.000,
            1.000,
            1.000,
            0.333,
            1.000,
            1.000,
            0.667,
            1.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.167,
            0.000,
            0.000,
            0.333,
            0.000,
            0.000,
            0.500,
            0.000,
            0.000,
            0.667,
            0.000,
            0.000,
            0.833,
            0.000,
            0.000,
            1.000,
            0.000,
            0.000,
            0.000,
            0.143,
            0.143,
            0.143,
            0.857,
            0.857,
            0.857,
            1.000,
            1.000,
            1.000,
        ]
    )
    .astype(np.floataa)
    .reshape(-1, 3)
)









'''simple docstring'''

from __future__ import annotations

from collections import Counter





def 				A_					(	snake_case	):
  SCREAMING_SNAKE_CASE:int   =   [True] * n
  SCREAMING_SNAKE_CASE:List[str]   =   False
  SCREAMING_SNAKE_CASE:Tuple   =   False
  SCREAMING_SNAKE_CASE:int   =   True

  for i in range(3 ,    int(n**0.5 + 1	) ,    2	):
    SCREAMING_SNAKE_CASE:str   =   i * 2
    while index < n:
      SCREAMING_SNAKE_CASE:Any   =   False
      SCREAMING_SNAKE_CASE:List[Any]   =   index + i

  SCREAMING_SNAKE_CASE:Union[str, Any]   =   [2]

  for i in range(3 ,    snake_case ,    2	):
    if is_prime[i]:
      primes.append(snake_case	)

  return primes





def 				A_					(	snake_case	):
  SCREAMING_SNAKE_CASE:List[str]   =   str(snake_case	)
  SCREAMING_SNAKE_CASE:Union[str, Any]   =   []
  SCREAMING_SNAKE_CASE:List[str]   =   ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

  for duplicate in Counter(snake_case	) - Counter(set(snake_case	)	):
    SCREAMING_SNAKE_CASE:List[str]   =   [int(number_str.replace(snake_case ,    snake_case	)	) for digit in digits]
    replacements.append(snake_case	)

  return replacements





def 				A_					(	snake_case = 8	):
  SCREAMING_SNAKE_CASE:str   =   set()

  # Filter primes with less than 3 replaceable digits
  SCREAMING_SNAKE_CASE:Tuple   =   {
      x for x in set(prime_sieve(1000000	)	) if len(str(snake_case	)	) - len(set(str(snake_case	)	)	) >= 3
  }

  for prime in primes:
    if prime in numbers_checked:
      continue

    SCREAMING_SNAKE_CASE:List[Any]   =   digit_replacements(snake_case	)

    for family in replacements:
      numbers_checked.update(snake_case	)
      SCREAMING_SNAKE_CASE:Optional[Any]   =   primes.intersection(snake_case	)

      if len(snake_case	) != family_length:
        continue

      return min(snake_case	)

  return -1


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






