結果
問題 | No.2443 特殊線形群の標準表現 |
ユーザー | 👑 p-adic |
提出日時 | 2023-08-06 10:29:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 212 ms / 3,000 ms |
コード長 | 6,829 bytes |
コンパイル時間 | 3,263 ms |
コンパイル使用メモリ | 223,056 KB |
実行使用メモリ | 14,864 KB |
最終ジャッジ日時 | 2024-11-06 13:32:21 |
合計ジャッジ時間 | 6,684 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 12 ms
14,628 KB |
testcase_01 | AC | 13 ms
14,688 KB |
testcase_02 | AC | 11 ms
14,644 KB |
testcase_03 | AC | 12 ms
14,640 KB |
testcase_04 | AC | 11 ms
14,672 KB |
testcase_05 | AC | 12 ms
14,636 KB |
testcase_06 | AC | 11 ms
14,640 KB |
testcase_07 | AC | 12 ms
14,692 KB |
testcase_08 | AC | 11 ms
14,632 KB |
testcase_09 | AC | 11 ms
14,628 KB |
testcase_10 | AC | 12 ms
14,624 KB |
testcase_11 | AC | 12 ms
14,508 KB |
testcase_12 | AC | 11 ms
14,652 KB |
testcase_13 | AC | 12 ms
14,576 KB |
testcase_14 | AC | 29 ms
14,632 KB |
testcase_15 | AC | 207 ms
14,692 KB |
testcase_16 | AC | 205 ms
14,708 KB |
testcase_17 | AC | 212 ms
14,712 KB |
testcase_18 | AC | 210 ms
14,704 KB |
testcase_19 | AC | 210 ms
14,668 KB |
testcase_20 | AC | 212 ms
14,864 KB |
ソースコード
// セグ木解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 <bits/stdc++.h> using namespace std; using ll = long long; #define MAIN main #define TYPE_OF( VAR ) decay_t<decltype( VAR )> #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 <typename T> 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 <int N> class PowerCalculation { public: int m_val; inline constexpr PowerCalculation(); }; template <int N> inline constexpr PowerCalculation<N>::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 <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> class SegmentTree { private: static constexpr const int g_power = PowerCalculation<N>{}.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 <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> inline const T& SegmentTree<T,m_T,e_T,N>::g_e = e_T(); template <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> inline SegmentTree<T,m_T,e_T,N>::SegmentTree() : m_a() { if( m_a[0] != g_e ){ for( int j = 1 ; j < g_power2 ; j++ ){ m_a[j] = g_e; } } } template <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> inline SegmentTree<T,m_T,e_T,N>::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 <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> inline const T& SegmentTree<T,m_T,e_T,N>::operator[]( const int& i ) const { return m_a[g_power + i]; } template <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> inline const T& SegmentTree<T,m_T,e_T,N>::Get( const int& i ) const { return m_a[g_power + i]; } template <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> T SegmentTree<T,m_T,e_T,N>::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 <TEMPLATE_ARGUMENTS_FOR_SEGMENT_TREE> void SegmentTree<T,m_T,e_T,N>::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<ll,ll>,pair<ll,ll> >; inline Matrix operator*( 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 Matrix m( const Matrix& M , const Matrix& N ) { return N * M; } 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<Matrix,m,e,bound_N+1> 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; }