結果
問題 | No.3105 Міжнародний підрядок саміт |
ユーザー | 👑 p-adic |
提出日時 | 2023-04-02 11:38:55 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,769 bytes |
コンパイル時間 | 3,787 ms |
コンパイル使用メモリ | 237,016 KB |
実行使用メモリ | 45,412 KB |
最終ジャッジ日時 | 2024-10-12 02:10:07 |
合計ジャッジ時間 | 12,297 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 182 ms
45,324 KB |
testcase_01 | TLE | - |
testcase_02 | RE | - |
testcase_03 | TLE | - |
testcase_04 | AC | 192 ms
45,336 KB |
ソースコード
#pragma GCC optimize ( "O3" ) #pragma GCC optimize( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #include <bits/stdc++.h> using namespace std; using ll = long long; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type #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( S ) string S; getline( cin , S ) #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 , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define COUT( ANSWER ) cout << ( ANSWER ) << "\n" #define RETURN( ANSWER ) COUT( ANSWER ); QUIT #define GETLINE_COUNT( S , VARIABLE_NUMBER ) GETLINE( S ); int VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S = 0; int VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S = S.size(); { int size = S.size(); int count = 0; for( int i = 0 ; i < size ; i++ ){ if( S.substr( i , 1 ) == " " ){ count++; } } assert( count + 1 == VARIABLE_NUMBER ); } // |N| <= BOUNDを満たすNをSから構築 #define STOI( S , N , BOUND ) TYPE_OF( BOUND ) N = 0; { bool VARIABLE_FOR_POSITIVITY_FOR_GETLINE = true; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); if( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) == "-" ){ VARIABLE_FOR_POSITIVITY_FOR_GETLINE = false; VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; assert( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ); } assert( S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) != " " ); string VARIABLE_FOR_LETTER_FOR_GETLINE{}; int VARIABLE_FOR_DIGIT_FOR_GETLINE{}; while( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ? ( VARIABLE_FOR_LETTER_FOR_GETLINE = S.substr( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S , 1 ) ) != " " : false ){ VARIABLE_FOR_DIGIT_FOR_GETLINE = stoi( VARIABLE_FOR_LETTER_FOR_GETLINE ); assert( N < BOUND / 10 ? true : N == BOUND / 10 && VARIABLE_FOR_DIGIT_FOR_GETLINE <= BOUND % 10 ); N = N * 10 + VARIABLE_FOR_DIGIT_FOR_GETLINE; VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } if( ! VARIABLE_FOR_POSITIVITY_FOR_GETLINE ){ N *= -1; } if( VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S < VARIABLE_FOR_SIZE_FOR_GETLINE_FOR_ ## S ){ VARIABLE_FOR_INDEX_FOR_GETLINE_FOR_ ## S ++; } } template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; } inline CEXPR( int , bound_N , 13 ); struct CombSum { int m_val[bound_N+1]; constexpr CombSum() : m_val() { FOREQ( N , 1 , bound_N ){ if( ( N & 1 ) == 1 ){ m_val[N] = 1 << ( N - 1 ); } else { int& m_val_N = m_val[N]; int comb = 1; FOREQ( p , 1 , N ){ ( comb *= ( N - p + 1 ) ) /= p; if( ( p & 1 ) == 1 ){ m_val_N += comb; } } } } } }; int main() { UNTIE; CEXPR( int , bound_T , 6000 ); GETLINE_COUNT( T_str , 1 ); STOI( T_str , T , bound_T ); CEXPR( int , bound_P , 1000000000 ); CEXPR( ll , bound_Ai , 1000000000 ); CEXPR( ll , bound_evenness , ll( 1 ) << 62 ); CEXPR( int , lim_B , 1 << bound_N ); static set<int> x_set[lim_B][bound_N+1] = {}; int Card[lim_B]; CEXPR( int , lim_x , ( ( bound_N * ( bound_N - 1 ) ) >> 1 ) + 1 ); FOR( B , 1 , lim_B ){ set<int> ( &xB_set )[bound_N+1] = x_set[B]; int B_copy = B; int A[bound_N] = {}; int B_card = 0; FOR( d , 0 , bound_N ){ if( ( B_copy & 1 ) == 1 ){ A[B_card++] = d; } B_copy >>= 1; } Card[B] = B_card; FOREQ( p , 0 , B_card ){ set<int>& xBp_set = xB_set[p]; xBp_set.insert( lim_x ); xBp_set.insert( -lim_x ); } int power = 1 << B_card; FOREQ( B_p , 0 , power ){ B_copy = B_p; int x = 0; int p = 0; FOR( d , 0 , B_card ){ ( B_copy & 1 ) == 1 ? ( p++ , x += A[d] ) : x -= A[d]; B_copy >>= 1; } xB_set[p].insert( x ); } } constexpr CombSum comb_sum{}; REPEAT( T ){ GETLINE_COUNT( NP_str , 2 ); STOI( NP_str , N , bound_N ); STOI( NP_str , P , bound_P ); GETLINE_COUNT( A_str , N ); STOI( A_str , A0 , bound_Ai ); STOI( A_str , A1 , bound_Ai ); ll d = A1 - A0; ll answer; if( d == 0 ){ answer = comb_sum.m_val[N] * A0; } else { answer = 0; if( d < 0 ){ d *= -1; A0 -= d * ( N - 1 ); } int power_N = 1 << N; FOR( B , 1 , power_N ){ set<int> ( &xB_set )[bound_N+1] = x_set[B]; int& B_card = Card[B]; ll evenness = bound_evenness; FOREQ( p , 0 , B_card ){ set<int>& xBp_set = xB_set[p]; ll A0_factor = ( B_card - ( p << 1 ) ) * A0; ll x = A0_factor / d; if( x >= lim_x ){ x = lim_x - 1; } else if( x <= -lim_x ){ x = -lim_x + 1; } auto itr = xBp_set.lower_bound( x ); if( *itr != lim_x ){ ll evenness_curr = -A0_factor + *itr * d; evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr; evenness > evenness_curr ? evenness = evenness_curr : evenness; } if( *itr != -lim_x ){ itr--; if( *itr != -lim_x ){ ll evenness_curr = -A0_factor + *itr * d; evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr; evenness > evenness_curr ? evenness = evenness_curr : evenness; } } } answer += evenness; } } COUT( answer % P ); } QUIT; }