#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 ) ) #define LIBRARY_SEARCH bool searched_library = false; LibrarySearch( searched_library ); if( searched_library ){ QUIT; }; #define START_WATCH( PROCESS_NAME ) StartWatch( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) StopWatch( HOW_MANY_TIMES ) #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 ) ) #define LIBRARY_SEARCH #define START_WATCH( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) #endif // #define RANDOM_TEST #include 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 #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 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 AUTO_ITR( ARRAY ) auto itr_ ## ARRAY = ARRAY .begin() , end_ ## ARRAY = ARRAY .end() #define FOR_ITR( ARRAY ) for( AUTO_ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT_ ## HOW_MANY_TIMES , 0 , HOW_MANY_TIMES ) #define QUIT return 0 #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS_ ) #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void StartWatch( const string& process_name = "nothing" ); void StopWatch( const int& how_many_times = 1 ); #endif #if defined( DEBUG ) && defined( RANDOM_TEST ) inline CEXPR( int , bound_random_test_num , 1000 ); #define START_MAIN FOR( random_test_num , 0 , bound_random_test_num ){ CERR( "(" << random_test_num << ")" ); ll GetRand( const ll& Rand_min , const ll& Rand_max ); #define SET_ASSERT( A , MIN , MAX ) CERR( #A << " = " << ( A = GetRand( MIN , MAX ) ) ) #define RETURN( ANSWER ) if( ( ANSWER ) == guchoku ){ CERR( ( ANSWER ) << " == " << guchoku ); continue; } else { CERR( ( ANSWER ) << " != " << guchoku ); QUIT; } #define FINISH_MAIN CERR( "" ); } #else #define START_MAIN #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #define FINISH_MAIN #endif template inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same::value && ! is_same::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_INDEX , CONSTEXPR_LENGTH , MODULO ) \ static ll ANSWER[CONSTEXPR_LENGTH]; \ static ll ANSWER_INV[CONSTEXPR_LENGTH]; \ static ll INVERSE[CONSTEXPR_LENGTH]; \ { \ ll VARIABLE_FOR_PRODUCT_FOR_FACTORIAL = 1; \ ANSWER[0] = VARIABLE_FOR_PRODUCT_FOR_FACTORIAL; \ FOREQ( i , 1 , MAX_INDEX ){ \ 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_INDEX ){ \ ANSWER_INV[i] = ( VARIABLE_FOR_PRODUCT_FOR_FACTORIAL *= INVERSE[i] = ( MODULO ) - ( ( ( ( MODULO ) / i ) * INVERSE[ ( MODULO ) % i ] ) % ( MODULO ) ) ) %= ( MODULO ); \ } \ } \ // 二分探索テンプレート // EXPRESSIONがANSWERの広義単調関数の時、EXPRESSION >= TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ static_assert( ! is_same::value && ! is_same::value ); \ ll ANSWER = MINIMUM; \ if( MINIMUM <= MAXIMUM ){ \ 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 INEQUALITY_FOR_CHECK 0 ){ \ VARIABLE_FOR_BINARY_SEARCH_U = UPDATE_U; \ } else { \ VARIABLE_FOR_BINARY_SEARCH_L = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ CERR( "二分探索終了: " << VARIABLE_FOR_BINARY_SEARCH_L << "<=" << ANSWER << "<=" << VARIABLE_FOR_BINARY_SEARCH_U << ":" << EXPRESSION << ( EXPRESSION > TARGET ? ">" : EXPRESSION < TARGET ? "<" : "=" ) << TARGET ); \ CERR( ( EXPRESSION DESIRED_INEQUALITY TARGET ? "二分探索成功" : "二分探索失敗" ) ); \ assert( EXPRESSION DESIRED_INEQUALITY TARGET ); \ } else { \ CERR( "二分探索失敗: " << MINIMUM << ">" << MAXIMUM ); \ assert( MINIMUM <= MAXIMUM ); \ } \ // 単調増加の時にEXPRESSION >= TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , >= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調増加の時にEXPRESSION <= TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , > , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION >= TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , TARGET , < , ANSWER - 1 , ANSWER , ( VARIABLE_FOR_BINARY_SEARCH_L + 1 + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 単調減少の時にEXPRESSION <= TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , TARGET , <= , ANSWER , ANSWER + 1 , ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2 ) \ // 圧縮用 #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 ASK_NUMBER( ... ) \ CERR( "" ); \ CERR( "問題の区分は以下の中で何番に該当しますか?" ); \ problems = { __VA_ARGS__ }; \ problems_size = problems.size(); \ FOR( i , 0 , problems_size ){ \ CERR( i << ": " << problems[i] ); \ } \ cin >> num; \ CERR( "" ); \ if( num < 0 || num >= problems_size ){ \ CERR( "返答は" << problems_size - 1 << "以下の非負整数にしてください。" ); \ CERR( "終了します。" ); \ CERR( "" ); \ return; \ } \ num_temp = 0; \ #define ASK_YES_NO( QUESTION ) \ CERR( "" ); \ CERR( QUESTION << "[y/n]" ); \ cin >> reply; \ if( reply != "y" && reply != "n" ){ \ CERR( "y/nのいずれかで答えてください。" ); \ CERR( "終了します。" ); \ CERR( "" ); \ return; \ } \ CERR( "" ); \ #define SLS( CLASS ) void CLASS ## LibrarySearch( int& num , int& num_temp , string& reply , vector& problems , int& problems_size ) #define CALL_SLS( CLASS ) CLASS ## LibrarySearch( num , num_temp , reply , problems , problems_size ) SLS( ExplicitExpression ); SLS( ExplicitExpressionUnary ); SLS( ExplicitExpressionMultiary ); SLS( ExplicitExpressionIteration ); SLS( ExplicitExpressionArraySum ); SLS( ExplicitExpressionFunctionOnPermutation ); SLS( ExplicitExpressionFunctionOnTree ); SLS( FunctionOnTree ); SLS( ExplicitExpressionFunctionOnNonTreeGraph ); SLS( ExplicitExpressionOrder ); SLS( ExplicitExpressionProbability ); SLS( ExplicitExpressionOther ); SLS( Maximisation ); SLS( MaximisationExplicitExpression ); SLS( MaximisationFunctionOnArray ); SLS( MaximisationSubArraySum ); SLS( MaximisationArrayFunction ); SLS( MaximisationArrayLength ); SLS( MaximisationFunctionOnTree ); SLS( MinimisationMovingCost ); SLS( MaximisationStringMatching ); SLS( MaximisationBipartiteMatching ); SLS( Counting ); SLS( CountingExplicitExpression ); SLS( CountingArray ); SLS( CountingSubArray ); SLS( CountingSumFixedSubArray ); SLS( CountingRestrctedSubArray ); SLS( CountingRestrctedContinuousSubArray ); SLS( CountingRestrctedDiscontinuousSubArray ); SLS( CountingRestrctedSubPermutation ); SLS( CountingArbitraryArray ); SLS( CountingPartitionOfTree ); SLS( CountingPalindrome ); SLS( Solving ); SLS( Query ); SLS( QueryArray ); SLS( QueryGraph ); SLS( Decision ); SLS( DecisionConnectedness ); SLS( DecisionHigherConnectedness ); SLS( DecisionGame ); SLS( DecisionAccessibility ); SLS( DecisionSatisfiability ); SLS( Construction ); void LibrarySearch( bool& searched_library ) { int num = 0; vector problems{}; int problems_size = 13; int num_temp = 0; string reply{}; ASK_YES_NO( "ライブラリーを探索しますか?" ); if( reply != "y" ){ CERR( "ライブラリーを探索せずに続行します。" ); CERR( "" ); return; } searched_library = true; ASK_NUMBER( "明示式の計算問題" , "最大/最小化問題" , "数え上げ問題" , "求解問題" , "クエリ処理問題" , "真偽判定問題" , "構築問題" ); if( num == num_temp++ ){ CALL_SLS( ExplicitExpression ); } else if( num == num_temp++ ){ CALL_SLS( Maximisation ); } else if( num == num_temp++ ){ CALL_SLS( Counting ); } else if( num == num_temp++ ){ CALL_SLS( Solving ); } else if( num == num_temp++ ){ CALL_SLS( Query ); } else if( num == num_temp++ ){ CALL_SLS( Decision ); } else if( num == num_temp++ ){ CALL_SLS( Construction ); } ASK_YES_NO( "マルチテストケースですか?" ); if( reply == "y" ){ CERR( "テストケースを跨ぐ前計算が可能か否かを優先的に考察しましょう。" ); CERR( "" ); CERR( "テストケース全体でのNの総和に直接上限が与えられている問題では、" ); CERR( "ライブラリーの使用時は配列の初期化が各テストケースに必要となる場合に" ); CERR( "TLEとなる可能性が高いです。" ); CERR( "- 動的配列への置き換え" ); CERR( "- 座標圧縮" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\CoordinateCompress" ); CERR( "を検討しましょう。" ); CERR( "" ); CERR( "配列を手元の環境でデバッグしやすくするためにstaticをつけている場合は" ); CERR( "テストケースを跨いで値が残ってしまわないように注意しましょう。" ); CERR( "" ); } CERR( "ライブラリー探索は以上です。終了します。" ); CERR( "" ); } SLS( ExplicitExpression ) { ASK_YES_NO( "入力は1つの数か、1つの数と法を表す数ですか?" ); if( reply == "y" ){ CALL_SLS( ExplicitExpressionUnary ); } else { CALL_SLS( ExplicitExpressionMultiary ); } } SLS( ExplicitExpressionUnary ) { CERR( "まずは小さい入力の場合を愚直に計算し、OEISで検索しましょう。" ); CERR( "https://oeis.org/?language=japanese" ); CERR( "" ); CERR( "次に出力の定義と等価な式を考察しましょう。" ); CERR( "- 単調ならば、冪乗や階乗" ); CERR( "- 定義にp進法が使われていれば、各種探索アルゴリズム" ); CERR( "- 入力が素数に近い場合に規則性があれば、p進付値、p進法、" ); CERR( " オイラー関数、約数の個数など" ); CERR( "を検討しましょう。" ); CERR( "" ); CERR( "前計算の候補としては" ); CERR( "- 素数列挙" ); CERR( "- 1つまたは複数の整数の約数列挙" ); CERR( "- オイラー関数の値の列挙" ); CERR( "- サブゴールとなる関係式を満たす解の列挙" ); CERR( "を検討しましょう。" ); } SLS( ExplicitExpressionMultiary ) { ASK_YES_NO( "関数の反復合成の計算問題ですか?" ); if( reply == "y" ){ CALL_SLS( ExplicitExpressionIteration ); } else { ASK_NUMBER( "配列上の関数の総和の計算問題" , "順列上の関数の計算問題" , "木上の関数の総和の計算問題" , "木以外のグラフ上の関数の総和の計算問題" , "序数の計算問題" , "確率/期待値の計算問題" , "その他の明示式の計算問題" ); if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionArraySum ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionFunctionOnPermutation ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionFunctionOnTree ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionFunctionOnNonTreeGraph ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionOrder ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionProbability ); } else if( num == num_temp++ ){ CALL_SLS( ExplicitExpressionOther ); } } } SLS( ExplicitExpressionIteration ) { CERR( "定義域の要素数N、テストケース数T、反復回数の上限Kとします。" ); CERR( "- O((N + T)log_2 K)が通りそうならばダブリング" ); CERR( " \\Mathematics\\Function\\Iteration\\Doubling" ); CERR( "- O(TN)が通りそうならばループ検出" ); CERR( " \\Mathematics\\Function\\Iteration\\LoopDetection" ); CERR( "- O(N)すら通らなさそうならば関数の規則性を見付けるための実験" ); CERR( "を検討しましょう。" ); } SLS( ExplicitExpressionArraySum ) { ASK_NUMBER( "成分を受け取る関数の総和の計算問題" , "部分列を受け取る関数の総和の計算問題" ); if( num == num_temp++ ){ CERR( "成分を受け取る関数fが与えられているとします。" ); CERR( "fが一次式の場合、実質内積と定数の和となります。" ); CERR( "内積は片方の添え字を反転させることで畳み込みに帰着させることができます。" ); CERR( "配列への操作がシフトである場合は繰り返し内積を求めることになるので、" ); CERR( "適当な法での高速フーリエ変換" ); CERR( "\\Mathematics\\Arithmetic\\Mod" ); CERR( "\\Mathematics\\Polynoial" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ ASK_NUMBER( "連続部分列への分割に関する関数の総和の計算問題" , "連続とは限らない部分列への分割に関する関数の総和の計算問題" ); if( num == num_temp++ ){ CERR( "配列の添字集合は全順序集合なので、木の分割の問題に一般化されます。" ); CALL_SLS( ExplicitExpressionFunctionOnTree ); CERR( "" ); CERR( "更にfが部分列の長さに関する再帰的な構造を持つ場合、全ての連続部分列に" ); CERR( "対しfの値を前計算することを検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "配列の並び換えによって答えが変わらないので、適切にソートしてから" ); CERR( "計算することを検討しましょう。" ); } } CERR( "" ); CERR( "入力が大きい場合と小さい場合で解法を変える考察を忘れないようにしましょう。" ); } SLS( ExplicitExpressionFunctionOnPermutation ) { CERR( "- 符号そのものの計算問題は" ); CERR( " - O(N log_2 N)やO(N^2)が間に合いそうなら転倒数の計算" ); CERR( " - O(N log_2 N)が間に合わなさそうなら互換表示(O(N))" ); CERR( "- 符号と何かの積の和は行列式に帰着させ、" ); CERR( " - 行列式そのものなら行基本変形(O(N^3))" ); CERR( " - 余因子展開の途中の値が必要ならメモ化再帰(O(N 2^N))" ); CERR( "を検討しましょう。" ); CERR( "" ); CERR( "1つの順列の転倒数は、" ); CERR( "- O(N^2)が通りそうならば愚直な二重ループ" ); CERR( "- O(N log_2 N)が通りそうならば可換群BIT" ); CERR( " \\Mathematics\\Combinatorial\\Permutation" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\BIT" ); CERR( "で計算しましょう。" ); CERR( "" ); CERR( "条件を満たす順列全体をわたる転倒数の総和/期待値は、" ); CERR( "各i=3かつ(n,L)=(2,2)で" ); CERR( "- Q(i)=「i=3」" ); CERR( "- R_0(b_0,b_1)=「b_0b_1」" ); CERR( "と表されます。" ); CERR( "" ); if( num == num_temp++ ){ CALL_SLS( CountingRestrctedContinuousSubArray ); } else if( num == num_temp++ ){ CALL_SLS( CountingRestrctedDiscontinuousSubArray ); } else if( num == num_temp++ ){ CALL_SLS( CountingRestrctedSubPermutation ); } CERR( "を検討しましょう。" ); CERR( "" ); CERR( "特にR_l(B)たちがgcdやmaxなどの羃等演算に関する等式で与えられる場合は、" ); CERR( "不等式の方が扱いやすいのでゼータ変換/メビウス変換" ); CERR( "\\Mathematics\\Combinatorial\\ZetaTransform" ); CERR( "を検討しましょう。" ); } SLS( CountingRestrctedContinuousSubArray ) { CERR( "P(B)を満たすAの連続部分列Bの数え上げは、" ); CERR( "- R_lたちがlに依存しないならば尺取り法O(N)" ); CERR( "- R_lたちがlに依存する場合、" ); CERR( " - O(N^2)が通りそうなら左端を固定した愚直探索" ); CERR( " - O(N^2)が通らなさそうならR_lたちの読み替え" ); CERR( "を検討しましょう。" ); } SLS( CountingRestrctedDiscontinuousSubArray ) { CERR( "P(B)を満たすAの連続とは限らない部分列Bの数え上げは、" ); CERR( "- n-1<=i<=max{j<=N|Q(j)}を満たす各i" ); CERR( "- (0,1,...,N-1)の長さn-1の各部分列s" ); CERR( "に対する" ); CERR( "「長さiで、任意の0<=l<=i-nに対しR_l(B)を満たし、" ); CERR( " 末尾n-1項がsに対応するAの部分列Bの個数dp[i][s]」" ); CERR( "を管理するi,sに関する動的計画法" ); CERR( "\\Mathematics\\Combinatorial\\Counting\\IncreasingSubsequence" ); CERR( "\\Mathematics\\Combinatorial\\Counting\\IncreasingSubsequence\\Subwalk" ); CERR( "を検討しましょう。" ); } SLS( CountingPartitionOfTree ) { CALL_SLS( FunctionOnTree ); CERR( "F(P)が固定された時のPの数え上げ問題は" ); CERR( "「第i成分までで切った時のF(P)=vを満たすPの個数dp[i][v]」" ); CERR( "を管理するi,vに関する動的計画法(O(N^2 v_max×fの計算量))" ); CERR( "を検討しましょう。" ); } SLS( CountingRestrctedSubPermutation ) { CERR( "P(B)を満たすAの部分順列Bの数え上げは、" ); CERR( "- n-1<=|S|<=max{j<=N|Q(j)}を満たす(0,1,...,N-1)の部分集合S" ); CERR( "- Sの長さn-1の各部分順列s" ); CERR( "に対する" ); CERR( "「任意の0<=l<=|S|-nに対しR_l(B)を満たし、末尾n-1項がsに対応し、」" ); CERR( " 全体がSに対応するAの部分順列Bの個数dp[S][s]」" ); CERR( "を管理するS,sに関する動的計画法を検討しましょう。" ); CERR( "" ); CERR( "sの網羅は[0,N)^{n-1}の全探策でもS内の順列探索と定数倍しか変わらないので" ); CERR( "実装の速さを優先しましょう。" ); CERR( "" ); CERR( "Nが小さい場合は概算値" ); CERR( "7! ≒ 5×10^3" ); CERR( "8! ≒ 4×10^4" ); CERR( "9! ≒ 4×10^5" ); CERR( "10! ≒ 4×10^6" ); CERR( "11! ≒ 4×10^7" ); CERR( "12! ≒ 5×10^8" ); CERR( "を参考に順列の全列挙" ); CERR( "\\Mathematics\\Combinatorial\\Permutation" ); CERR( "を検討しましょう。" ); } SLS( CountingArbitraryArray ) { ASK_NUMBER( "配列を受け取る関数の値の数え上げ問題" , "隣接成分間関係式を満たす配列の数え上げ問題" ); if( num == num_temp++ ){ CERR( "- 配列の種類が少ない場合は、全ての配列に対する関数の値の前計算" ); CERR( "- 取り得る値が少なく関数が長さに関して再帰的構造を持つ場合は、" ); CERR( " 「長さiの時に値vである配列の総数dp[i][v]」" ); CERR( " を管理するi,vに関する動的計画法" ); } else if( num == num_temp++ ){ CERR( "- いくつかの条件の重ね合わせの時は包除原理" ); CERR( "- 全順序の場合は数の分割方法などへの翻訳" ); CERR( "- 疎な半順序の場合はグラフの前計算" ); } CERR( "を検討しましょう。" ); } SLS( CountingPalindrome ) { CERR( "回文判定は長さに関して再帰的に計算できます。" ); CERR( "- O(N^2)が通る場合、愚直な再帰により前計算で全ての部分列の回文判定" ); CERR( "- O(N^2)が通らない場合、Manacherのアルゴリズムやローリングハッシュで前計算" ); CERR( " https://snuke.hatenablog.com/entry/2014/12/02/235837" ); CERR( "を検討しましょう。" ); } SLS( Solving ) { CERR( "- 単調関数は二分探索" ); CERR( "- 可微分関数はニュートン法" ); CERR( "- 一次関数は掃き出し法" ); CERR( "を検討しましょう。" ); } SLS( Query ) { ASK_NUMBER( "配列の問題" , "グラフの問題" ); if( num == num_temp++ ){ CALL_SLS( QueryArray ); } else if( num == num_temp++ ){ CALL_SLS( QueryGraph ); } } SLS( QueryArray ) { ASK_NUMBER( "可換群構造+を使う問題" , "可換羃等モノイド構造∨を使う問題" , "モノイド構造*を使う問題" , "非結合的マグマ構造*を使う問題" , "集合へのマグマ作用(*,\\cdot)を使う問題" , "モノイドへのマグマ作用(+,\\cdot)を使う問題" , "定数とのmaxを取った値の区間和取得を使う問題" ); if( num == num_temp++ ){ CERR( "- 区間加算/区間取得が必要ならば可換群BIT" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\BIT\\Template" ); CERR( "- 一点代入/一点加算/区間取得が必要ならば可換群平方分割" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\SqrtDecomposition\\Template" ); CERR( "- 区間以外の領域で加算/全更新後の一点取得が必要ならば階差数列" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\Tree\\DifferenceSeqeuence" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "- 一点代入/区間加算/一点取得/区間取得が必要ならば可換羃等モノイドBIT" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\BIT\\IntervalMax\\Template" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "- 一点代入/区間取得が必要ならばモノイドBIT" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\BIT\\Template\\Monoid" ); CERR( "- 一点加算/区間加算/一点取得/区間取得が必要ならばモノイド平方分割" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\SqrtDecomposition\\Template\\Monoid" ); CERR( "- 一点代入/一点取得/区間取得が必要ならばモノイドセグメント木" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\SegmentTree" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "- 写像のコード化" ); CERR( " \\Mathematics\\Function\\Encoder" ); CERR( "によりモノイドに帰着させることを検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "- 一点作用/区間作用/一点取得が必要ならば双対平方分割" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\SqrtDecomposition\\Template\\Dual" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "- 区間代入/区間作用/区間加算/一点取得/区間取得が必要な場合は遅延評価平方分割" ); CERR( " \\Mathematics\\SetTheory\\DirectProduct\\AffineSpace\\SqrtDecomposition\\Template\\LazyEvaluation" ); CERR( "を検討しましょう。" ); } else if( num == num_temp++ ){ CERR( "maxで全体更新でなく区間更新をする場合の汎用的な解法は分かりません。" ); CERR( "例えば区間が包含について単調でmaxを取る値も単調であれば全体更新と" ); CERR( "同様の処理ができます。状況に応じた考察をしましょう。" ); CERR( "" ); CERR( "maxで全体更新をする場合、maxを取る値は単調である場合に帰着できます。" ); CERR( "maxで全体更新をしない場合、つまりただmaxの区間和を処理するだけの場合、" ); CERR( "クエリの順番を入れ替えることができるので、単調な全体更新に帰着できます。" ); CERR( "従って以下では単調な全体更新の問題を考えます。" ); CERR( "" ); CERR( "maxを取る定数を変数化し、元の値との大小を表す{0,1}値の係数を考えます。" ); CERR( "すると区間作用前後の値は統一的にその係数と変数を使って表せます。" ); CERR( "配列の各成分の係数の値が変化するイベントとクエリをソートして管理し、" ); CERR( "クエリがイベントを跨ぐたびに係数を更新することを検討しましょう。" ); CERR( "" ); CERR( "例えばクエリB_qに対するmax(A_i,B_q)の区間和は、" ); CERR( "- 優先度つきキューA'={(A_i,i)|i}(構築O(N log N))" ); CERR( "- (B_q,q)_qをソートしたB'(構築O(Q log Q))" ); CERR( "- 長さNの数列C=(0,...,0)(構築O(N))" ); CERR( "を用意し、B'を前から探索して順に各クエリ(B_q,q)を処理します。" ); CERR( "具体的にはA'を前から探索して順にA_i CL BREADTH ## FirstSearch_Body{PU:int m_V;int m_init;LI m_next;bool m_found[V_max];int m_prev[V_max];IN BREADTH ## FirstSearch_Body(CRI V);IN BREADTH ## FirstSearch_Body(CRI V,CRI init);IN VO Reset(CRI init);IN VO Shift(CRI init);IN CRI SZ()CO;IN CRI init()CO;IN bool& found(CRI i);IN CRI prev(CRI i)CO;int Next();virtual LI e(CRI t)= 0;};TE E(CRI)> CL BREADTH ## FirstSearch:PU BREADTH ## FirstSearch_Body{PU:TE IN BREADTH ## FirstSearch(CO Args&... args);IN LI e(CRI t);};TE E(CRI)> VO BREADTH ## FirstConnectedComponent(CRI V,int(&vertex)[V_max],int& count); #define DF_OF_FIRST_SEARCH(BREADTH,PUSH)TE IN BREADTH ## FirstSearch_Body::BREADTH ## FirstSearch_Body(CRI V):m_V(V),m_init(),m_next(),m_found(),m_prev(){assert(m_V <= V_max);for(int i = 0;i < m_V;i++){m_prev[i] = -1;}}TE IN BREADTH ## FirstSearch_Body::BREADTH ## FirstSearch_Body(CRI V,CRI init):BREADTH ## FirstSearch_Body(V){m_init = init;m_next.push_back(m_init);m_found[m_init] = true;}TE E(CRI)> TE IN BREADTH ## FirstSearch::BREADTH ## FirstSearch(CO Args&... args):BREADTH ## FirstSearch_Body(args...){}TE IN VO BREADTH ## FirstSearch_Body::Reset(CRI init){m_init = init;assert(m_init < m_V);m_next.clear();m_next.push_back(m_init);for(int i = 0;i < m_V;i++){m_found[i] = i == m_init;m_prev[i] = -1;}}TE IN VO BREADTH ## FirstSearch_Body::Shift(CRI init){m_init = init;assert(m_init < m_V);m_next.clear();if(! m_found[m_init]){m_next.push_back(m_init);m_found[m_init] = true;}}TE IN CRI BREADTH ## FirstSearch_Body::SZ()CO{RE m_V;}TE IN CRI BREADTH ## FirstSearch_Body::init()CO{RE m_init;}TE IN bool& BREADTH ## FirstSearch_Body::found(CRI i){assert(i < m_V);RE m_found[i];}TE IN CRI BREADTH ## FirstSearch_Body::prev(CRI i)CO{assert(i < m_V);RE m_prev[i];}TE int BREADTH ## FirstSearch_Body::Next(){if(m_next.empty()){RE -1;}CO int i_curr = m_next.front();m_next.pop_front();LI edge = e(i_curr);WH(! edge.empty()){CRI i = edge.front();bool& found_i = found(i);if(! found_i){m_next.PUSH(i);m_prev[i] = i_curr;found_i = true;}edge.pop_front();}RE i_curr;}TE E(CRI)> IN LI BREADTH ## FirstSearch::e(CRI t){RE E(t);}TE E(CRI)> VO BREADTH ## FirstConnectedComponentSearch(CRI V,int(&vertex)[V_max],int& count){BREADTH ## FirstSearch bfs{V};count = 0;for(int i = 0;i < V;i++){vertex[i] = -1;}for(int i = 0;i < V;i++){if(vertex[i] == -1){bfs.Shift(i);int j = bfs.Next();WH(j != -1?vertex[j] == 0:false){vertex[j] = count;j = bfs.Next();}count++;}}RE;} DC_OF_FIRST_SEARCH(Depth);DF_OF_FIRST_SEARCH(Depth,push_front); TE E(CRI),int digit = 0>CL DepthFirstSearchOnTree:PU DepthFirstSearch{PU:int m_reversed[V_max];VE m_children[V_max];int m_children_num[V_max];bool m_set_children;int m_depth[V_max];int m_height[V_max];bool m_set_height;int m_weight[V_max];bool m_set_weight;int m_doubling[digit][V_max];bool m_set_doubling;IN DepthFirstSearchOnTree(CRI V,CRI root);IN VO Reset(CRI init)= delete;IN VO Shift(CRI init)= delete;IN CRI Root()CO;IN CRI Parent(CRI i)CO;IN CO VE& Children(CRI i);IN CRI Depth(CRI i)CO;IN CRI Height(CRI i);IN CRI Weight(CRI i);IN CRI NodeNumber(CRI i,CO bool& reversed = false)CO;IN CRI ChildrenNumber(CRI i);int Ancestor(int i,int n);int LCA(int i,int j);int LCA(int i,int j,int& i_prev,int& j_prev);TE T RootingDP(const T(&a)[V_max]);TE VO RerootingDP(T(&d)[V_max]);VO SetChildren();VO SetHeight();VO SetWeight();VO SetDoubling();}; TE E(CRI),int digit> IN DepthFirstSearchOnTree::DepthFirstSearchOnTree(CRI V,CRI root):DepthFirstSearch(V,root),m_reversed(),m_children(),m_set_children(),m_depth(),m_height(),m_set_height(),m_weight(),m_set_weight(),m_doubling(),m_set_doubling(){int n = DepthFirstSearch::SZ();WH(--n >= 0){CRI i = m_reversed[n] = DepthFirstSearch::Next();CRI j = Parent(i);if(j != -1){m_depth[i] = m_depth[j] + 1;}}}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::Root()CO{RE DepthFirstSearch::init();}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::Parent(CRI i)CO{RE DepthFirstSearch::prev(i);}TE E(CRI),int digit> IN CO VE& DepthFirstSearchOnTree::Children(CRI i){if(! m_set_children){SetChildren();}RE m_children[i];}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::Depth(CRI i)CO{RE m_depth[i];}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::Height(CRI i){if(! m_set_height){SetHeight();}RE m_height[i];}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::Weight(CRI i){if(! m_set_weight){SetWeight();}RE m_weight[i];}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::NodeNumber(CRI i,CO bool& reversed)CO{RE m_reversed[reversed?i:DepthFirstSearch::SZ()- 1 - i];}TE E(CRI),int digit> IN CRI DepthFirstSearchOnTree::ChildrenNumber(CRI i){if(! m_set_children){SetChildren();}RE m_children_num[i];}TE E(CRI),int digit>int DepthFirstSearchOnTree::Ancestor(int i,int n){if(! m_set_doubling){SetDoubling();}assert((n >> digit)== 0);int d = 0;WH(n != 0){if((n & 1)== 1){assert((i = m_doubling[d][i])!= -1);}d++;n >>= 1;}RE i;}TE E(CRI),int digit>int DepthFirstSearchOnTree::LCA(int i,int j){int diff = Depth(i)- Depth(j);if(diff < 0){swap(i,j);diff *= -1;}i = Ancestor(i,diff);if(i == j){RE i;}int d = digit;WH(--d >= 0){CO int(&doubling_d)[V_max] = m_doubling[d];CRI doubling_d_i = doubling_d[i];CRI doubling_d_j = doubling_d[j];if(doubling_d_i != doubling_d_j){i = doubling_d_i;j = doubling_d_j;assert(i != -1);assert(j != -1);}}RE Parent(i);}TE E(CRI),int digit>int DepthFirstSearchOnTree::LCA(int i,int j,int& i_prev,int& j_prev){if(i == j){i_prev = j_prev = -1;RE i;}int diff = Depth(i)- Depth(j);if(diff < 0){RE LCA(j,i,j_prev,i_prev);}if(diff > 0){i_prev = Ancestor(i,diff - 1);i = Parent(i_prev);assert(i != -1);if(i == j){j_prev = -1;RE i;}}else if(! m_set_doubling){SetDoubling();}int d = digit;WH(--d >= 0){CO int(&doubling_d)[V_max] = m_doubling[d];CRI doubling_d_i = doubling_d[i];CRI doubling_d_j = doubling_d[j];if(doubling_d_i != doubling_d_j){i = doubling_d_i;j = doubling_d_j;assert(i != -1);assert(j != -1);}}i_prev = i;j_prev = j;RE Parent(i_prev);}TE E(CRI),int digit>VO DepthFirstSearchOnTree::SetChildren(){assert(!m_set_children);m_set_children = true;CRI V = DepthFirstSearch::SZ();for(int i = 0;i < V;i++){CRI j = Parent(i);if(j == -1){m_children_num[i] = -1;}else{VE& m_children_j = m_children[j];m_children_num[i] = m_children_j.SZ();m_children_j.push_back(i);}}RE;}TE E(CRI),int digit>VO DepthFirstSearchOnTree::SetHeight(){assert(!m_set_height);m_set_height = true;CRI V = DepthFirstSearch::SZ();for(int i = 0;i < V;i++){CRI reversed_i = m_reversed[i];CRI parent_i = Parent(reversed_i);if(parent_i != -1){int& height_parent_i = m_height[parent_i];CRI height_i = m_height[reversed_i];height_parent_i > height_i?height_parent_i:height_parent_i = height_i + 1;}}RE;}TE E(CRI),int digit>VO DepthFirstSearchOnTree::SetWeight(){assert(!m_set_weight);m_set_weight = true;CRI V = DepthFirstSearch::SZ();for(int i = 0;i < V;i++){CRI reversed_i = m_reversed[i];CRI parent_i = Parent(reversed_i);if(parent_i != -1){m_weight[parent_i] += m_weight[reversed_i] + 1;}}RE;}TE E(CRI),int digit>VO DepthFirstSearchOnTree::SetDoubling(){assert(!m_set_doubling);m_set_doubling = true;CRI V = DepthFirstSearch::SZ();{int(&doubling_0)[V_max] = m_doubling[0];CRI r = Root();for(int i = 0;i < V;i++){doubling_0[i] = Parent(i);}}for(int d = 1;d < digit;d++){int(&doubling_d)[V_max] = m_doubling[d];int(&doubling_d_minus)[V_max] = m_doubling[d-1];for(int i = 0;i < V;i++){CRI doubling_d_minus_i = doubling_d_minus[i];doubling_d[i] = doubling_d_minus_i == -1?-1:doubling_d_minus[doubling_d_minus_i];}}RE;}TE E(CRI),int digit> TE T DepthFirstSearchOnTree::RootingDP(const T(&a)[V_max]){if(! m_set_children){SetChildren();}CRI V = DepthFirstSearch::SZ();LI children_value[V_max] = {};T temp;for(int n = 0;n < V;n++){CRI i = NodeNumber(n,true);LI& children_value_i = children_value[i];temp = a[i];WH(!children_value_i.empty()){temp = m_T(temp,children_value_i.front());children_value_i.pop_front();}CRI j = Parent(i);if(j != -1){children_value[j].push_back(temp);}}RE temp;}TE E(CRI),int digit> TE VO DepthFirstSearchOnTree::RerootingDP(T(&d)[V_max]){if(! m_set_children){SetChildren();}CRI V = DepthFirstSearch::SZ();CO T& e = e_T();VE children_value[V_max] ={};VE left_sum[V_max] ={};VE right_sum[V_max] ={};for(int i = 0;i < V;i++){children_value[i].resize(m_children[i].SZ());}for(int n = 0;n < V;n++){CRI i = NodeNumber(n,true);CO VE& children_value_i = children_value[i];CO int SZ_i = children_value_i.SZ();T temp = e;VE& left_sum_i = left_sum[i];left_sum_i.reserve(SZ_i + 1);left_sum_i.push_back(temp);for(int m = 0;m < SZ_i;m++){left_sum_i.push_back(temp = m_T(temp,children_value_i[m]));}CRI j = Parent(i);if(j != -1){children_value[j][m_children_num[i]] = f(temp,i);}temp = e;VE& right_sum_i = right_sum[i];right_sum_i.resize(SZ_i);for(int m = 1;m <= SZ_i;m++){right_sum_i[ SZ_i - m ] = temp;temp = m_T(children_value_i[SZ_i - m],temp);}}for(int n = 1;n < V;n++){CRI i = NodeNumber(n);CRI j = Parent(i);CRI k = ChildrenNumber(i);VE& left_sum_i = left_sum[i];VE& right_sum_i = right_sum[i];CO int SZ_i = right_sum_i.SZ();CO T rest_i = f(m_T(left_sum[j][k],right_sum[j][k]),j);for(int m = 0;m <= SZ_i;m++){T& left_sum_im = left_sum_i[m];left_sum_im = m_T(rest_i,left_sum_im);}}for(int i = 0;i < V;i++){d[i] = f(left_sum[i].back(),i);}RE;} inline DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 list e[bound_N] = {}; list E( const int& i ) { return e[i]; } using T = vector >; T A[bound_N] = {}; inline CEXPR( ll , P , 998244353 ); T m_T( const T& s , const T& t ) { if( s.empty() ){ return t; } if( t.empty() ){ return s; } T answer = T( 30 , { 0 , 0 } ); FOR( d , 0 , 30 ){ FOR( is , 0 , 2 ){ ( answer[d][is] += s[d][is] * t[d][1] ) %= P; ( answer[d][is ^ 0] += s[d][is] * t[d][0] ) %= P; ( answer[d][is ^ 1] += s[d][is] * t[d][1] ) %= P; } } return answer; } int main() { UNTIE; LIBRARY_SEARCH; START_MAIN; // DEXPR( int , bound_T , 100000 , 100 ); // CIN_ASSERT( T , 1 , bound_T ); // REPEAT( T ){ // } // CEXPR( int , bound_N , 10 ); // CEXPR( int , bound_N , 1000000000 ); // 0が9個 // CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 CIN_ASSERT( N , 1 , bound_N ); FOR( n , 1 , N ){ CIN_ASSERT( u , 1 , N ); CIN_ASSERT( v , 1 , N ); u--; v--; e[u].push_back( v ); e[v].push_back( u ); } CEXPR( int , bound_Ai , ( 1 << 30 ) - 1 ); FOR( i , 0 , N ){ CIN_ASSERT( Ai , 0 , bound_Ai ); T& A_i = A[i] = T( 30 , { 0 , 0 } ); FOR( d , 0 , 30 ){ A_i[d][ ( Ai >> d ) & 1 ] = 1; } } DepthFirstSearchOnTree dfst{ N , 0 }; T val = dfst.RootingDP( A ); // // CEXPR( int , bound_M , 10 ); // DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // // CEXPR( int , bound_M , 1000000000 ); // 0が9個 // // CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // CIN_ASSERT( M , 1 , bound_M ); // DEXPR( int , bound_Q , 100000 , 100 ); // CIN_ASSERT( Q , 1 , bound_Q ); // REPEAT( Q ){ // COUT( N ); // } // ll guchoku = Guchoku(); // ll answer = 0; // if( answer == guchoku ){ // CERR( answer << " == " << guchoku ); // } else { // CERR( answer << " != " << guchoku ); // QUIT; // } ll answer = 0; ll power = 1; FOR( d , 0 , 30 ){ ( answer += val[d][1] * power ) %= P; ( power <<= 1 ) < P ? power : power -= P; } COUT( ( answer ) ); FINISH_MAIN; QUIT; }