結果

問題 No.1097 Remainder Operation
ユーザー 👑 p-adicp-adic
提出日時 2023-06-04 18:42:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 20,630 bytes
コンパイル時間 3,624 ms
コンパイル使用メモリ 226,076 KB
実行使用メモリ 9,712 KB
最終ジャッジ日時 2023-08-28 08:19:42
合計ジャッジ時間 6,360 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,704 KB
testcase_01 AC 2 ms
4,528 KB
testcase_02 AC 2 ms
4,788 KB
testcase_03 AC 2 ms
4,560 KB
testcase_04 AC 2 ms
4,528 KB
testcase_05 AC 2 ms
4,576 KB
testcase_06 AC 2 ms
4,568 KB
testcase_07 AC 5 ms
4,620 KB
testcase_08 AC 5 ms
4,628 KB
testcase_09 AC 5 ms
4,584 KB
testcase_10 AC 6 ms
4,600 KB
testcase_11 AC 6 ms
4,844 KB
testcase_12 AC 36 ms
4,944 KB
testcase_13 AC 36 ms
5,000 KB
testcase_14 AC 36 ms
4,940 KB
testcase_15 AC 36 ms
4,964 KB
testcase_16 AC 37 ms
4,932 KB
testcase_17 AC 38 ms
4,912 KB
testcase_18 AC 71 ms
9,712 KB
testcase_19 AC 72 ms
9,656 KB
testcase_20 AC 71 ms
9,652 KB
testcase_21 AC 71 ms
9,692 KB
testcase_22 AC 71 ms
9,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef DEBUG
  #define _GLIBCXX_DEBUG
  #define CERR( ANSWER ) cerr << ANSWER << "\n";
#else
  #pragma GCC optimize ( "O3" )
  #pragma GCC optimize( "unroll-loops" )
  #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
  #define CERR( ANSWER ) 
#endif
#include <bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) )
#define TYPE_OF( VAR ) decay_t<decltype( VAR )>
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr )
#define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE
#define CIN( LL , A ) LL A; cin >> A
#define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) )
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); ASSERT( A , MIN , MAX )
#define GETLINE( A ) string A; getline( cin , A )
#define GETLINE_SEPARATE( A , SEPARATOR ) string A; getline( cin , A , SEPARATOR )
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( TYPE_OF( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ )
#define FOREQ( VAR , INITIAL , FINAL ) for( TYPE_OF( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ )
#define FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR >= FINAL ; VAR -- )
#define FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ )
#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES )
#define QUIT return 0
#define COUT( ANSWER ) cout << ( ANSWER ) << "\n"
#define RETURN( ANSWER ) COUT( ANSWER ); QUIT
#define SET_PRECISION( PRECISION ) cout << fixed << setprecision( PRECISION )
#define DOUBLE( PRECISION , ANSWER ) SET_PRECISION << ( ANSWER ) << "\n"; QUIT

template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; }
template <typename T> inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : ( a % p ) + p; }

#define POWER( ANSWER , ARGUMENT , EXPONENT )				\
  static_assert( ! is_same<TYPE_OF( ARGUMENT ),int>::value && ! is_same<TYPE_OF( ARGUMENT ),uint>::value ); \
  TYPE_OF( ARGUMENT ) ANSWER{ 1 };					\
  {									\
    TYPE_OF( ARGUMENT ) ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT );	\
    TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT );	\
    while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){			\
      if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){			\
	ANSWER *= ARGUMENT_FOR_SQUARE_FOR_POWER;			\
      }									\
      ARGUMENT_FOR_SQUARE_FOR_POWER *= ARGUMENT_FOR_SQUARE_FOR_POWER;	\
      EXPONENT_FOR_SQUARE_FOR_POWER /= 2;				\
    }									\
  }									\

#define POWER_MOD( ANSWER , ARGUMENT , EXPONENT , MODULO )		\
  ll ANSWER{ 1 };							\
  {									\
    ll ARGUMENT_FOR_SQUARE_FOR_POWER = ( MODULO + ( ( ARGUMENT ) % MODULO ) ) % MODULO; \
    TYPE_OF( EXPONENT ) EXPONENT_FOR_SQUARE_FOR_POWER = ( EXPONENT );	\
    while( EXPONENT_FOR_SQUARE_FOR_POWER != 0 ){			\
      if( EXPONENT_FOR_SQUARE_FOR_POWER % 2 == 1 ){			\
	ANSWER = ( ANSWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO;	\
      }									\
      ARGUMENT_FOR_SQUARE_FOR_POWER = ( ARGUMENT_FOR_SQUARE_FOR_POWER * ARGUMENT_FOR_SQUARE_FOR_POWER ) % MODULO; \
      EXPONENT_FOR_SQUARE_FOR_POWER /= 2;				\
    }									\
  }									\

#define FACTORIAL_MOD( ANSWER , ANSWER_INV , INVERSE , MAX_I , LENGTH , MODULO ) \
  static ll ANSWER[LENGTH];						\
  static ll ANSWER_INV[LENGTH];						\
  static ll INVERSE[LENGTH];						\
  {									\
    ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1;				\
    ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL;			\
    FOREQ( i , 1 , MAX_I ){						\
      ANSWER[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= i ) %= MODULO; \
    }									\
    ANSWER_INV[0] = ANSWER_INV[1] = INVERSE[1] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \
    FOREQ( i , 2 , MAX_I ){						\
      ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = MODULO - ( ( ( MODULO / i ) * INVERSE[MODULO % i] ) % MODULO ) ) %= MODULO; \
    }									\
  }									\

// 通常の二分探索その1
// EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION >= TARGETを満たす最小の整数を返す。
// 広義単調増加関数を扱いたい時は等号成立の処理を消して続く>に等号を付ける。
#define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \
  ll ANSWER;								\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM;				\
    ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \
    while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \
      CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER + 1;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
    CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << ">=0" ); \
  }									\

// 通常の二分探索その2
// EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION <= TARGETを満たす最大の整数を返す。
// 広義単調増加関数を扱いたい時は等号成立の処理を消して続く<に等号を付ける。
#define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \
  ll ANSWER;								\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM;				\
    ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \
    while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \
      CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER - 1;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
    CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "<=0" ); \
  }									\

// 通常の二分探索その3
// EXPRESSIONがANSWERの狭義単調減少関数の時、EXPRESSION >= TARGETを満たす最大の整数を返す。
// 広義単調増加関数を扱いたい時は等号成立の処理を消して続く>に等号を付ける。
#define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \
  ll ANSWER;								\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM;				\
    ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \
    while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \
      CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER - 1;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
    CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << ">=0" ); \
  }									\

// 通常の二分探索その4
// EXPRESSIONがANSWERの狭義単調減少関数の時、EXPRESSION <= TARGETを満たす最小の整数を返す。
// 広義単調増加関数を扱いたい時は等号成立の処理を消して続く<に等号を付ける。
#define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  static_assert( ! is_same<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::value ); \
  ll ANSWER;								\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = MAXIMUM;				\
    ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH; \
    while( VARIABLE_FOR_BINARY_SEARCH_L != VARIABLE_FOR_BINARY_SEARCH_U ){ \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \
      CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "=" << VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER + 1;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
    CERR( VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << "-" << TARGET << "<=0" ); \
  }									\



// 二進法の二分探索
// EXPRESSIONがANSWERの狭義単調増加関数の時、EXPRESSION <= TARGETを満たす最大の整数を返す。
#define BBS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  ll ANSWER = MINIMUM;							\
  {									\
    ll VARIABLE_FOR_POWER_FOR_BINARY_SEARCH = 1;			\
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( MAXIMUM ) - ANSWER; \
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH <= VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ){ \
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH *= 2;			\
    }									\
    VARIABLE_FOR_POWER_FOR_BINARY_SEARCH /= 2;				\
    ll VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER;			\
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH != 0 ){			\
      ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH + VARIABLE_FOR_POWER_FOR_BINARY_SEARCH; \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( EXPRESSION ) - ( TARGET ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER;			\
	break;								\
      } else if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH < 0 ){	\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH = ANSWER;			\
      }									\
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH /= 2;			\
    }									\
    ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH;			\
  }									\

// 圧縮用
#define TE template
#define TY typename
#define US using
#define ST static
#define IN inline
#define CL class
#define PU public
#define OP operator
#define CE constexpr
#define CO const
#define NE noexcept
#define RE return 
#define WH while
#define VO void
#define VE vector
#define LI list
#define BE begin
#define EN end
#define SZ size
#define MO move
#define TH this
#define CRI CO int&
#define CRUI CO uint&
#define CRL CO ll&

#define SFINAE_FOR_LOOP_DETECTION_BODY( DEFAULT ) enable_if_t<is_convertible_v<U,T> >* DEFAULT

template <typename T , typename U , U f(const T&) , int length_max>
class LoopDetectionBody
{

private:
  T m_init;
  map<T,int> m_memory;
  vector<T> m_memory_inv;
  
protected:
  int m_value[length_max];
  int m_length;
  int m_loop_start;
  int m_loop_length;

public:
  template <SFINAE_FOR_LOOP_DETECTION_BODY( = nullptr )> inline LoopDetectionBody( const T& init );
  template <typename INT> T IteratedComposition( const INT& n );

  inline const int& GetLength() const noexcept;
  inline const int& GetLoopStart() const noexcept;
  inline const int& GetLoopLength() const noexcept;
  void SearchLoopStart();
  
private:
  inline void SetInit();
  virtual T e( const int& i );
  virtual int e_inv( const T& t );
  virtual void SetValue( const int& i );

};

template <int f(const int&) , int length_max>
class LoopDetection :
  public LoopDetectionBody<int,int,f,length_max>
{

private:
  int m_value_inv[length_max];
  
public:
  inline LoopDetection( const int& init );

private:
  inline int e( const int& i );
  inline int e_inv( const int& t );
  void SetValue( const int& i );

};

template <typename T , typename U , U f(const T&) , int length_max>
class MemorisationLoopDetection :
  public LoopDetectionBody<T,U,f,length_max>
{

public:
  inline MemorisationLoopDetection( const T& init );

};

template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)>
class EnumerationLoopDetection :
  public LoopDetectionBody<T,U,f,length_max>
{

private:
  int m_value_inv[length_max];
  
public:
  inline EnumerationLoopDetection( const T& init );
  
private:
  inline T e( const int& i );
  inline int e_inv( const T& t );
  void SetValue( const int& i );

};

template <typename T , typename U , U f(const T&) , int length_max> template <SFINAE_FOR_LOOP_DETECTION_BODY()> inline LoopDetectionBody<T,U,f,length_max>::LoopDetectionBody( const T& init ) : m_init( init ) , m_memory() , m_memory_inv() , m_value() , m_length() , m_loop_start( -1 ) , m_loop_length( -1 ) {}
template <int f(const int&) , int length_max> inline LoopDetection<f,length_max>::LoopDetection( const int& init ) : LoopDetectionBody<int,int,f,length_max>( init ) , m_value_inv() { for( int i = 0 ; i < length_max ; i++ ){ m_value_inv[i] = -1; } }
template <typename T , typename U , U f(const T&) , int length_max> inline MemorisationLoopDetection<T,U,f,length_max>::MemorisationLoopDetection( const T& init ) : LoopDetectionBody<T,U,f,length_max>( init ) {}
template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::EnumerationLoopDetection( const T& init ) : LoopDetectionBody<T,U,f,length_max>( init ) , m_value_inv() { for( int i = 0 ; i < length_max ; i++ ){ m_value_inv[i] = -1; } }

template <typename T , typename U , U f(const T&) , int length_max> template <typename INT>
T LoopDetectionBody<T,U,f,length_max>::IteratedComposition( const INT& n )
{

  if( m_length == 0 ){

    SetInit();

  }

  if( n < m_length ){

    return e( m_value[n] );
    
  }

  if( m_loop_start != -1 ){

    return e( m_value[ m_loop_start + ( n - m_loop_start ) % m_loop_length ] );

  }
  
  SetValue( e_inv( f( e( m_value[m_length - 1] ) ) ) );
  return IteratedComposition( n );

}

template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLength() const noexcept { return m_length; }
template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLoopStart() const noexcept { return m_loop_start; }
template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLoopLength() const noexcept { return m_loop_length; }

template <typename T , typename U , U f(const T&) , int length_max>
void LoopDetectionBody<T,U,f,length_max>::SearchLoopStart()
{

  assert( m_loop_start == -1 );
  int n = 0;
  
  while( m_loop_start == -1 ){

    IteratedComposition( n++ );
    
  }

  return;

}

template <typename T , typename U , U f(const T&) , int length_max> inline void LoopDetectionBody<T,U,f,length_max>::SetInit() { assert( m_length == 0 ); SetValue( e_inv( m_init ) ); }

template <typename T , typename U , U f(const T&) , int length_max>
T LoopDetectionBody<T,U,f,length_max>::e( const int& i )
{
  
  assert( i < m_length );
  return m_memory_inv[i];

}

template <int f(const int&) , int length_max> inline int LoopDetection<f,length_max>::e( const int& i ) { return i; }
template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline T EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::e( const int& i ) { return enum_T( i ); }

template <typename T , typename U , U f(const T&) , int length_max>
int LoopDetectionBody<T,U,f,length_max>::e_inv( const T& t )
{

  if( m_memory.count( t ) == 0 ){

    assert( m_length < length_max );
    m_value[m_length] = m_length;
    m_memory_inv.push_back( t );
    return m_memory[t] = m_length++;

  }

  m_loop_length = m_length - ( m_loop_start = m_memory[t] );
  return m_loop_start;
  
}

template <int f(const int&) , int length_max> inline int LoopDetection<f,length_max>::e_inv( const int& t ) { return t; }
template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline int EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::e_inv( const T& t ) { return enum_T_inv( t ); }

template <typename T , typename U , U f(const T&) , int length_max>
void LoopDetectionBody<T,U,f,length_max>::SetValue( const int& i ) {}

template <int f(const int&) , int length_max>
void LoopDetection<f,length_max>::SetValue( const int& i )
{

  using base = LoopDetectionBody<int,int,f,length_max>;
  int& m_value_inv_i = m_value_inv[i];

  if( m_value_inv_i != -1 ){

    base::m_loop_length = base::m_length - ( base::m_loop_start = m_value_inv_i );

  } else {
  
    base::m_value[base::m_length] = i;
    m_value_inv_i = base::m_length++;

  }

  return;

}

template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)>
void EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::SetValue( const int& i )
{

  using base = LoopDetectionBody<T,U,f,length_max>;
  int& m_value_inv_i = m_value_inv[i];

  if( m_value_inv_i != -1 ){

    base::m_loop_length = base::m_length - ( base::m_loop_start = m_value_inv_i );

  } else {
  
    base::m_value[base::m_length] = i;
    m_value_inv_i = base::m_length++;

  }

  return;

}


inline CEXPR( int , bound_NQ , 100000 );
int A[bound_NQ];
int N;
inline int f( const int& i ) { return ( i + A[i] ) % N; }

inline int enum_int( const int& i ) { return ( i + 1 ) % bound_NQ; }
inline int enum_int_inv( const int& i ) { return ( i + bound_NQ - 1 ) % bound_NQ; }

int main()
{
  UNTIE;
  CIN_ASSERT( N_prep , 1 , bound_NQ );
  N = N_prep;
  CEXPR( int , bound_Ai , 1000000 );
  FOR( i , 0 , N ){
    CIN_ASSERT( Ai , 1 , bound_Ai );
    A[i] = Ai;
  }
  CIN_ASSERT( Q , 1 , bound_NQ );
  CEXPR( ll , bound_Ki , 1000000000000 );
  // LoopDetection<f,bound_NQ> ld{ 0 };
  MemorisationLoopDetection<int,int,f,bound_NQ> ld{ 0 };
  // EnumerationLoopDetection<int,int,f,bound_NQ,enum_int,enum_int_inv> ld{ 0 };
  ld.SearchLoopStart();
  const int& length = ld.GetLength();
  const int& loop_start = ld.GetLoopStart();
  const int& loop_length = ld.GetLoopLength();
  ll X[bound_NQ + 1] = {};
  CERR( "デバッグ用出力:");
  CERR( "VVV");
  CERR( "X[0] = 0");
  ll X_curr = 0;
  FOREQ( i , 1 , length ){
    X_curr = X[i] = X_curr + A[X_curr % N];
    CERR( "X[" << i << "] = " << X_curr );
  }
  CERR( "AAA");
  ll& X_loop_start = X[loop_start];
  ll X_loop = X_curr - X_loop_start;
  REPEAT( Q ){
    CIN_ASSERT( Ki , 1 , bound_Ki );
    if( Ki <= loop_start ){
      COUT( X[Ki] );
    } else {
      ll diff = Ki - loop_start - 1;
      COUT( X[loop_start + 1 + diff % loop_length] + X_loop * ( diff / loop_length ) );
    }
  }
  QUIT;
}
0