// 誤解法(積の順序を無視したセグ木解O(N + Q log N))チェック #ifdef DEBUG #define _GLIBCXX_DEBUG #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ); signal( SIGABRT , &AlertAbort ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define CERR( MESSAGE ) cerr << MESSAGE << endl; #define COUT( ANSWER ) cout << ANSWER << endl #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " << ( MIN ) << ( ( MIN ) <= A ? "<=" : ">" ) << A << ( A <= ( MAX ) ? "<=" : ">" ) << ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define CERR( MESSAGE ) #define COUT( ANSWER ) cout << ANSWER << "\n" #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #endif #include using namespace std; using ll = long long; #define MAIN main #define TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define CIN( LL , A ) LL A; cin >> A #define CIN_ASSERT( A , MIN , MAX ) TYPE_OF( MAX ) A; SET_ASSERT( A , MIN , MAX ) #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 REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } #endif template inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } // 2^16 = 65536 // 2^17 = 131072 // 2^18 = 262144 template class PowerCalculation { public: int m_val; inline constexpr PowerCalculation(); }; template inline constexpr PowerCalculation::PowerCalculation() : m_val( 1 ) { while( m_val < N ){ m_val <<= 1; } } #define TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE typename T , T m_T(const T&,const T&) , const T& e_T() , int N // (可換とは限らない)モノイド(T,m_T:T^2->T,e_T:1->T)と非負整数Nをパラメータとする。 // 単位元による初期化O(N) // 配列による初期化O(N) // 一点取得O(1) // 区間積取得O(log_2 N) // 一点更新O((log_2 N)^2) template class SegmentTree { private: static constexpr const int g_power = PowerCalculation{}.m_val; static constexpr const int g_power2 = g_power << 1; T m_a[g_power2]; public: static const T& g_e; inline SegmentTree(); inline SegmentTree( const T ( &a )[N] ); inline const T& operator[]( const int& i ) const; inline const T& Get( const int& i ) const; T IntervalProduct( const int& i_start , const int& i_final ); void Set( const int& i , const T& n ); }; template inline const T& SegmentTree::g_e = e_T(); template inline SegmentTree::SegmentTree() : m_a() { if( m_a[0] != g_e ){ for( int j = 1 ; j < g_power2 ; j++ ){ m_a[j] = g_e; } } } template inline SegmentTree::SegmentTree( const T ( &a )[N] ) : m_a() { int j_ulim = g_power + N; if( m_a[0] != g_e ){ for( int j = g_power2 - 1 ; j >= j_ulim ; j-- ){ m_a[j] = g_e; } } for( int i = 0 ; i < N ; i++ ){ m_a[i | g_power] = a[i]; } for( int j = g_power - 1 ; j >= 1 ; j-- ){ int j2 = j << 1; m_a[j] = m_T( m_a[j2] , m_a[j2+1] ); } } template inline const T& SegmentTree::operator[]( const int& i ) const { return m_a[g_power + i]; } template inline const T& SegmentTree::Get( const int& i ) const { return m_a[g_power + i]; } template T SegmentTree::IntervalProduct( const int& i_start , const int& i_final ) { int j_min = i_start < 0 ? g_power : g_power + i_start; int j_ulim = i_final < N ? g_power + i_final + 1 : g_power + N; T answer0 = g_e; T answer1 = g_e; while( j_min < j_ulim ){ ( j_min & 1 ) == 1 ? answer0 = m_T( answer0 , m_a[j_min++] ) : answer0; ( j_ulim & 1 ) == 1 ? answer1 = m_T( m_a[--j_ulim] , answer1 ) : answer1; j_min >>= 1; j_ulim >>= 1; } return m_T( answer0 , answer1 ); } template void SegmentTree::Set( const int& i , const T& n ) { int j = g_power + i; m_a[j] = n; while( ( j <<= 1 ) >= 1 ){ int j2 = j << 1; m_a[j] = m_T( m_a[j2] , m_a[j2+1] ); } return; } #define OO first.first #define OI first.second #define IO second.first #define II second.second ll B; using Matrix = pair,pair >; inline Matrix m( const Matrix& M , const Matrix& N ) { return { { ( M.OO * N.OO + M.OI * N.IO ) % B , ( M.OO * N.OI + M.OI * N.II ) % B } , { ( M.IO * N.OO + M.II * N.IO ) % B , ( M.IO * N.OI + M.II * N.II ) % B } }; } inline const Matrix& e() { static const Matrix one{ { 1 , 0 } , { 0 , 1 } }; return one; } int MAIN() { UNTIE; DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 CIN_ASSERT( N , 1 , bound_N ); CEXPR( ll , bound_ABx , 1000000000 ); // 0が9個 SET_ASSERT( B , 1 , bound_ABx ); DEXPR( int , bound_Q , 100000 , 100 ); // 0が5個 CIN_ASSERT( Q , 1 , bound_Q ); Matrix temp{ { 1 , 0 } , { 0 , 1 } }; Matrix A[bound_N + 1] = { temp }; FOREQ( n , 1 , N ){ CIN_ASSERT( AOO , -bound_ABx , bound_ABx ); CIN_ASSERT( AOI , -bound_ABx , bound_ABx ); CIN_ASSERT( AIO , -bound_ABx , bound_ABx ); CIN_ASSERT( AII , -bound_ABx , bound_ABx ); assert( AOO * AII - AOI * AIO == 1 ); A[n] = { { AOO , AOI } , { AIO , AII } }; } SegmentTree st{ A }; REPEAT( Q ){ CIN_ASSERT( Lq , 0 , N ); CIN_ASSERT( Rq , Lq , N ); CIN_ASSERT( x , -bound_ABx , bound_ABx ); CIN_ASSERT( y , -bound_ABx , bound_ABx ); Matrix prod = st.IntervalProduct( Lq + 1 , Rq ); ll z = Residue( prod.OO * x + prod.OI * y , B ); ll w = Residue( prod.IO * x + prod.II * y , B ); COUT( z << " " << w ); } QUIT; }