#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 using namespace std; using ll = long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) decay_t #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 #define SFINAE_FOR_LOOP_DETECTION_BODY( DEFAULT ) enable_if_t >* DEFAULT template class LoopDetectionBody { private: T m_init; map m_memory; vector m_memory_inv; protected: int m_value[length_max]; int m_length; int m_loop_start; int m_loop_length; public: template inline LoopDetectionBody( const T& init ); template 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 class LoopDetection : public LoopDetectionBody { 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 template inline LoopDetectionBody::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 inline LoopDetection::LoopDetection( const int& init ) : LoopDetectionBody( init ) , m_value_inv() { for( int i = 0 ; i < length_max ; i++ ){ m_value_inv[i] = -1; } } template template T LoopDetectionBody::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 inline const int& LoopDetectionBody::GetLength() const noexcept { return m_length; } template inline const int& LoopDetectionBody::GetLoopStart() const noexcept { return m_loop_start; } template inline const int& LoopDetectionBody::GetLoopLength() const noexcept { return m_loop_length; } template void LoopDetectionBody::SearchLoopStart() { assert( m_loop_start == -1 ); int n = 0; while( m_loop_start == -1 ){ IteratedComposition( n++ ); } return; } template inline void LoopDetectionBody::SetInit() { assert( m_length == 0 ); SetValue( e_inv( m_init ) ); } template T LoopDetectionBody::e( const int& i ) { assert( i < m_length ); return m_memory_inv[i]; } template inline int LoopDetection::e( const int& i ) { return i; } template int LoopDetectionBody::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 inline int LoopDetection::e_inv( const int& t ) { return t; } template void LoopDetectionBody::SetValue( const int& i ) {} template void LoopDetection::SetValue( const int& i ) { using base = LoopDetectionBody; 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; } 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 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; }