



from __future__ import annotations

def       A_   (						__a  :					int		):

				"""simple docstring"""
				a__					   =		{1}
				a__					   =		[1]
				for _ in range(2			,		depth + 1		):
								a__					   =		[*previous_coefficients, 0]
								a__					   =		[0, *previous_coefficients]
								a__					   =		[]
								for x, y in zip(__a			,		__a		):
												coefficients.add(x + y		)
												previous_coefficients.append(x + y		)
				return coefficients

def       A_   (						__a  :					set[int]		):

				"""simple docstring"""
				a__					   =		set()
				for number in unique_coefficients:
								a__					   =		2
								a__					   =		number
								while divisor**2 <= copy_number:
												a__					   =		0
												while copy_number % divisor == 0:
																copy_number //= divisor
																multiplicity += 1
												if multiplicity >= 2:
																non_squarefrees.add(__a		)
																break
												divisor += 1

				return unique_coefficients.difference(__a		)

def       A_   (						__a  :					int = 51		):

				"""simple docstring"""
				a__					   =		get_pascal_triangle_unique_coefficients(__a		)
				a__					   =		get_squarefrees(__a		)
				return sum(__a		)


if __name__ == "__main__":
	print(f"""{solution() = }""")








import collections
import types

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_tensorflow_probability_available,
    is_tf_available,
    is_torch_available,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException


if is_torch_available():
      import torch

      from ..models.auto.modeling_auto import (
          MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
          MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
      )

if is_tf_available() and is_tensorflow_probability_available():
      import tensorflow as tf
      import tensorflow_probability as tfp

      from ..models.auto.modeling_tf_auto import (
          TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
          TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
      )

class 	__snake_case					(      _a ):






    '''simple docstring'''





    def __call__( self    :		Optional[Any]      ,    snake_case    :		Any=None      ,    snake_case    :		Optional[int]=None      ,    **snake_case    :		Any				):

      """simple docstring"""





      requires_backends(self      ,    '''pandas'''				)
      import pandas as pd

      if table is None:
        raise ValueError('''Keyword argument `table` cannot be None.'''				)
      elif query is None:
        if isinstance(snake_case      ,    snake_case				) and table.get('''query'''				) is not None and table.get('''table'''				) is not None:
          __lowerCAmelCase								=					[table]
        elif isinstance(snake_case      ,    snake_case				) and len(snake_case				) > 0:
          if not all(isinstance(snake_case      ,    snake_case				) for d in table				):
            raise ValueError(
                f'''Keyword argument `table` should be a list of dict, but is {(type(snake_case				) for d in table)}'''				)

          if table[0].get('''query'''				) is not None and table[0].get('''table'''				) is not None:
            __lowerCAmelCase								=					table
          else:
            raise ValueError(
                '''If keyword argument `table` is a list of dictionaries, each dictionary should have a `table`'''
                f''' and `query` key, but only dictionary has keys {table[0].keys()} `table` and `query` keys.'''				)
        elif Dataset is not None and isinstance(snake_case      ,    snake_case				) or isinstance(snake_case      ,    types.GeneratorType				):
          return table
        else:
          raise ValueError(
              '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
              f'''is {type(snake_case				)})'''				)
      else:
        __lowerCAmelCase								=					[{'''table''': table, '''query''': query}]

      for tqa_pipeline_input in tqa_pipeline_inputs:
        if not isinstance(tqa_pipeline_input['''table''']      ,    pd.DataFrame				):
          if tqa_pipeline_input["table"] is None:
            raise ValueError('''Table cannot be None.'''				)

          __lowerCAmelCase								=					pd.DataFrame(tqa_pipeline_input['''table''']				)

      return tqa_pipeline_inputs


@add_end_docstrings(_a )
class 	__snake_case					(      _a ):






    '''simple docstring'''
    _lowerCAmelCase  =						"""table,query"""





    def __init__( self    :		Optional[Any]      ,    snake_case    :		Dict=TableQuestionAnsweringArgumentHandler()      ,    *snake_case    :		List[Any]      ,    **snake_case    :		Any				):

      """simple docstring"""





      super().__init__(*snake_case      ,    **snake_case				)
      __lowerCAmelCase								=					args_parser

      self.check_model_type(
          dict(
              TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items()
              + TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()				)
          if self.framework == '''tf'''
          else dict(
              MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items() + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()				)				)

      __lowerCAmelCase								=					bool(getattr(self.model.config      ,    '''aggregation_labels'''      ,    snake_case				)				) and bool(
          getattr(self.model.config      ,    '''num_aggregation_labels'''      ,    snake_case				)				)
      __lowerCAmelCase								=					'''tapas''' if hasattr(self.model.config      ,    '''aggregation_labels'''				) else None





    def 						A		( self    :		Tuple      ,    **snake_case    :		Dict				):

      """simple docstring"""





      return self.model(**snake_case				)





    def 						A		( self    :		List[str]      ,    **snake_case    :		Dict				):

      """simple docstring"""





      if self.framework == "pt":
        __lowerCAmelCase								=					[]
        __lowerCAmelCase								=					[]
        __lowerCAmelCase								=					None
        __lowerCAmelCase								=					inputs['''input_ids'''].shape[0]

        __lowerCAmelCase								=					inputs['''input_ids'''].to(self.device				)
        __lowerCAmelCase								=					inputs['''attention_mask'''].to(self.device				)
        __lowerCAmelCase								=					inputs['''token_type_ids'''].to(self.device				)
        __lowerCAmelCase								=					None

        for index in range(snake_case				):
          # If sequences have already been processed, the token type IDs will be created according to the previous
          # answer.
          if prev_answers is not None:
            __lowerCAmelCase								=					token_type_ids_example[:, 3]  # shape (seq_len,)
            __lowerCAmelCase								=					np.zeros_like(prev_labels_example.cpu().numpy()				)  # shape (seq_len,)

            __lowerCAmelCase								=					token_type_ids[index]  # shape (seq_len, 7)
            for i in range(model_labels.shape[0]				):
              __lowerCAmelCase								=					token_type_ids_example[:, 0].tolist()[i]
              __lowerCAmelCase								=					token_type_ids_example[:, 1].tolist()[i] - 1
              __lowerCAmelCase								=					token_type_ids_example[:, 2].tolist()[i] - 1

              if row_id >= 0 and col_id >= 0 and segment_id == 1:
                __lowerCAmelCase								=					int(prev_answers[(col_id, row_id)]				)

            __lowerCAmelCase								=					torch.from_numpy(snake_case				).type(torch.long				).to(self.device				)

          __lowerCAmelCase								=					input_ids[index]
          __lowerCAmelCase								=					attention_mask[index]  # shape (seq_len,)
          __lowerCAmelCase								=					token_type_ids[index]  # shape (seq_len, 7)
          __lowerCAmelCase								=					self.model(
              input_ids=input_ids_example.unsqueeze(0				)      ,    attention_mask=attention_mask_example.unsqueeze(0				)      ,    token_type_ids=token_type_ids_example.unsqueeze(0				)      ,    )
          __lowerCAmelCase								=					outputs.logits

          if self.aggregate:
            all_aggregations.append(outputs.logits_aggregation				)

          all_logits.append(snake_case				)

          __lowerCAmelCase								=					torch.distributions.Bernoulli(logits=snake_case				)
          __lowerCAmelCase								=					dist_per_token.probs * attention_mask_example.type(torch.floataa				).to(
              dist_per_token.probs.device				)

          __lowerCAmelCase								=					collections.defaultdict(snake_case				)
          for i, p in enumerate(probabilities.squeeze().tolist()				):
            __lowerCAmelCase								=					token_type_ids_example[:, 0].tolist()[i]
            __lowerCAmelCase								=					token_type_ids_example[:, 1].tolist()[i] - 1
            __lowerCAmelCase								=					token_type_ids_example[:, 2].tolist()[i] - 1
            if col >= 0 and row >= 0 and segment_id == 1:
              coords_to_probs[(col, row)].append(snake_case				)

          __lowerCAmelCase								=					{key: np.array(coords_to_probs[key]				).mean() > 0.5 for key in coords_to_probs}

        __lowerCAmelCase								=					torch.cat(tuple(snake_case				)      ,    0				)

        return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(snake_case				)      ,    0				))
      else:
        __lowerCAmelCase								=					[]
        __lowerCAmelCase								=					[]
        __lowerCAmelCase								=					None
        __lowerCAmelCase								=					inputs['''input_ids'''].shape[0]

        __lowerCAmelCase								=					inputs['''input_ids''']
        __lowerCAmelCase								=					inputs['''attention_mask''']
        __lowerCAmelCase								=					inputs['''token_type_ids'''].numpy()
        __lowerCAmelCase								=					None

        for index in range(snake_case				):
          # If sequences have already been processed, the token type IDs will be created according to the previous
          # answer.
          if prev_answers is not None:
            __lowerCAmelCase								=					token_type_ids_example[:, 3]  # shape (seq_len,)
            __lowerCAmelCase								=					np.zeros_like(snake_case      ,    dtype=np.intaa				)  # shape (seq_len,)

            __lowerCAmelCase								=					token_type_ids[index]  # shape (seq_len, 7)
            for i in range(model_labels.shape[0]				):
              __lowerCAmelCase								=					token_type_ids_example[:, 0].tolist()[i]
              __lowerCAmelCase								=					token_type_ids_example[:, 1].tolist()[i] - 1
              __lowerCAmelCase								=					token_type_ids_example[:, 2].tolist()[i] - 1

              if row_id >= 0 and col_id >= 0 and segment_id == 1:
                __lowerCAmelCase								=					int(prev_answers[(col_id, row_id)]				)

            __lowerCAmelCase								=					model_labels

          __lowerCAmelCase								=					input_ids[index]
          __lowerCAmelCase								=					attention_mask[index]  # shape (seq_len,)
          __lowerCAmelCase								=					token_type_ids[index]  # shape (seq_len, 7)
          __lowerCAmelCase								=					self.model(
              input_ids=np.expand_dims(snake_case      ,    axis=0				)      ,    attention_mask=np.expand_dims(snake_case      ,    axis=0				)      ,    token_type_ids=np.expand_dims(snake_case      ,    axis=0				)      ,    )
          __lowerCAmelCase								=					outputs.logits

          if self.aggregate:
            all_aggregations.append(outputs.logits_aggregation				)

          all_logits.append(snake_case				)

          __lowerCAmelCase								=					tfp.distributions.Bernoulli(logits=snake_case				)
          __lowerCAmelCase								=					dist_per_token.probs_parameter() * tf.cast(snake_case      ,    tf.floataa				)

          __lowerCAmelCase								=					collections.defaultdict(snake_case				)
          __lowerCAmelCase								=					token_type_ids_example
          for i, p in enumerate(tf.squeeze(snake_case				).numpy().tolist()				):
            __lowerCAmelCase								=					token_type_ids_example[:, 0].tolist()[i]
            __lowerCAmelCase								=					token_type_ids_example[:, 1].tolist()[i] - 1
            __lowerCAmelCase								=					token_type_ids_example[:, 2].tolist()[i] - 1
            if col >= 0 and row >= 0 and segment_id == 1:
              coords_to_probs[(col, row)].append(snake_case				)

          __lowerCAmelCase								=					{key: np.array(coords_to_probs[key]				).mean() > 0.5 for key in coords_to_probs}

        __lowerCAmelCase								=					tf.concat(tuple(snake_case				)      ,    0				)

        return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(snake_case				)      ,    0				))





    def __call__( self    :		List[Any]      ,    *snake_case    :		Optional[int]      ,    **snake_case    :		Dict				):

      """simple docstring"""





      __lowerCAmelCase								=					self._args_parser(*snake_case      ,    **snake_case				)

      __lowerCAmelCase								=					super().__call__(snake_case      ,    **snake_case				)
      if len(snake_case				) == 1:
        return results[0]
      return results





    def 						A		( self    :		Tuple      ,    snake_case    :		Union[str, Any]=None      ,    snake_case    :		Any=None      ,    snake_case    :		Dict=None      ,    **snake_case    :		int				):

      """simple docstring"""





      __lowerCAmelCase								=					{}
      if padding is not None:
        __lowerCAmelCase								=					padding
      if truncation is not None:
        __lowerCAmelCase								=					truncation

      __lowerCAmelCase								=					{}
      if sequential is not None:
        __lowerCAmelCase								=					sequential
      return preprocess_params, forward_params, {}





    def 						A		( self    :		Any      ,    snake_case    :		Tuple      ,    snake_case    :		Dict=None      ,    snake_case    :		Any=True      ,    snake_case    :		Optional[int]=None				):

      """simple docstring"""





      if truncation is None:
        if self.type == "tapas":
          __lowerCAmelCase								=					'''drop_rows_to_fit'''
        else:
          __lowerCAmelCase								=					'''do_not_truncate'''

      __lowerCAmelCase						,    __lowerCAmelCase								=					pipeline_input['''table'''], pipeline_input['''query''']
      if table.empty:
        raise ValueError('''table is empty'''				)
      if query is None or query == "":
        raise ValueError('''query is empty'''				)
      __lowerCAmelCase								=					self.tokenizer(snake_case      ,    snake_case      ,    return_tensors=self.framework      ,    truncation=snake_case      ,    padding=snake_case				)
      __lowerCAmelCase								=					table
      return inputs





    def 						A		( self    :		Any      ,    snake_case    :		Dict      ,    snake_case    :		Union[str, Any]=False				):

      """simple docstring"""





      __lowerCAmelCase								=					model_inputs.pop('''table'''				)

      if self.type == "tapas":
        if sequential:
          __lowerCAmelCase								=					self.sequential_inference(**snake_case				)
        else:
          __lowerCAmelCase								=					self.batch_inference(**snake_case				)
      else:
        __lowerCAmelCase								=					self.model.generate(**snake_case				)
      __lowerCAmelCase								=					{'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
      return model_outputs





    def 						A		( self    :		Optional[Any]      ,    snake_case    :		List[Any]				):

      """simple docstring"""





      __lowerCAmelCase								=					model_outputs['''model_inputs''']
      __lowerCAmelCase								=					model_outputs['''table''']
      __lowerCAmelCase								=					model_outputs['''outputs''']
      if self.type == "tapas":
        if self.aggregate:
          __lowerCAmelCase						,    __lowerCAmelCase								=					outputs[:2]
          __lowerCAmelCase								=					self.tokenizer.convert_logits_to_predictions(snake_case      ,    snake_case      ,    snake_case				)
          __lowerCAmelCase						,    __lowerCAmelCase								=					predictions
          __lowerCAmelCase								=					{i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(snake_case				)}

          __lowerCAmelCase								=					self.model.config.no_aggregation_label_index
          __lowerCAmelCase								=					{
              i: aggregators[i] + ''' > ''' for i, pred in enumerate(snake_case				) if pred != no_agg_label_index
          }
        else:
          __lowerCAmelCase								=					outputs[0]
          __lowerCAmelCase								=					self.tokenizer.convert_logits_to_predictions(snake_case      ,    snake_case				)
          __lowerCAmelCase								=					predictions[0]
          __lowerCAmelCase								=					{}
          __lowerCAmelCase								=					{}
        __lowerCAmelCase								=					[]
        for index, coordinates in enumerate(snake_case				):
          __lowerCAmelCase								=					[table.iat[coordinate] for coordinate in coordinates]
          __lowerCAmelCase								=					aggregators.get(snake_case      ,    ''''''				)
          __lowerCAmelCase								=					aggregators_prefix.get(snake_case      ,    ''''''				)
          __lowerCAmelCase								=					{
              '''answer''': aggregator_prefix + ''', '''.join(snake_case				),
              '''coordinates''': coordinates,
              '''cells''': [table.iat[coordinate] for coordinate in coordinates],
          }
          if aggregator:
            __lowerCAmelCase								=					aggregator

          answers.append(snake_case				)
        if len(snake_case				) == 0:
          raise PipelineException('''Empty answer'''				)
      else:
        __lowerCAmelCase								=					[{'''answer''': answer} for answer in self.tokenizer.batch_decode(snake_case      ,    skip_special_tokens=snake_case				)]

      return answers if len(snake_case				) > 1 else answers[0]
