#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( CountingSubArrayImage ); SLS( CountingPartitionOfTree ); SLS( CountingString ); 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( 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( CountingSubArrayImage ) { CERR( "入力で与えられる配列をAと置きます。" ); CERR( "配列として等しいAの部分列のうち辞書式順序最小のものを数え上げる" ); CERR( "部分列DPを検討しましょう。具体的には" ); CERR( "「末尾が第i成分由来で辞書順最小なAの部分列の個数dp[i]」" ); CERR( "「s=A[j]を満たすj e[bound_N]; // list E( const int& i ) // { // list answer = e[i]; // // 入力によらない処理 // return answer; // } // inline CEXPR( ll , P , 998244353 ); // inline CEXPR( ll , P , 1000000007 ); int main() { UNTIE; LIBRARY_SEARCH; START_MAIN; // DEXPR( int , bound_T , 100000 , 100 ); // CIN_ASSERT( T , 1 , bound_T ); // REPEAT( T ){ // } CIN_ASSERT( P , 1 , 1000000000 ); CIN_ASSERT( N , 1 , bound_N ); CIN_ASSERT( M , 1 , bound_M ); // CIN_ASSERT( H , 1 , bound_H ); // CIN_ASSERT( W , 1 , bound_W ); // TYPE_OF( bound_HW ) HW = TYPE_OF( bound_HW )( H ) * W; // assert( HW <= bound_HW ); CEXPR( int , bound_L , bound_N * 2 + bound_M ); int A[bound_N][bound_M]; ll B[bound_N][bound_L]; FOR( i , 0 , N ){ FOR( j , 0 , M ){ CIN_ASSERT( Ai , 0 , P - 1 ); A[i][j] = B[i][j] = Ai; } FOR( j , 0 , N ){ B[i][M+j] = ( i == j ? 1 : 0 ); } FOR( j , 0 , N ){ B[i][M+N+j] = ( i == j ? 1 : 0 ); } } int L = N + M; int i_min = 0; int i_curr; int j_curr = 0; while( i_min < N && j_curr < L ){ i_curr = i_min; while( B[i_curr][j_curr] == 0 && i_curr < N ){ i_curr++; } if( i_curr < N ){ swap( B[i_min] , B[i_curr] ); const ll ( &B_i_min )[bound_L] = B[i_min++]; while( ++i_curr < N ){ ll ( &B_i_curr )[bound_L] = B[i_curr]; const ll& B_i_min_j_curr = B_i_min[j_curr]; POWER_MOD( inv , B_i_min_j_curr , P - 2 , P ); ll c = B_i_curr[j_curr] * inv % P; FOR( j , j_curr , L ){ ( B_i_curr[j] -= c * B_i_min[j] ) %= P; } } } j_curr++; } int rank = i_min; FOR( i , 0 , N ){ bool zero = true; FOR( j , 0 , M ){ if( B[i][j] != 0 ){ zero = false; break; } } if( zero ){ rank = i; break; } } rank = max( rank , 1 ); if( rank * ( N + M ) > N * M ){ COUT( 1 ); COUT( N << " " << M ); FOR( i , 0 , N ){ FOR( j , 0 , M ){ cout << ( A[i][j] + P ) % P << " \n"[j==M-1]; } } } else { L = N * 2 + M; i_min = 0; j_curr = M; while( i_min < N && j_curr < L ){ i_curr = i_min; while( B[i_curr][j_curr] == 0 && i_curr < N ){ i_curr++; } if( i_curr < N ){ swap( B[i_min] , B[i_curr] ); const ll ( &B_i_min )[bound_L] = B[i_min++]; while( ++i_curr < N ){ ll ( &B_i_curr )[bound_L] = B[i_curr]; const ll& B_i_min_j_curr = B_i_min[j_curr]; POWER_MOD( inv , B_i_min_j_curr , P - 2 , P ); ll c = B_i_curr[j_curr] * inv % P; FOR( j , j_curr , L ){ ( B_i_curr[j] -= c * B_i_min[j] ) %= P; } } } j_curr++; } COUT( 2 ); COUT( N << " " << rank ); FOR( i , 0 , N ){ FOR( j , 0 , rank ){ cout << ( B[i][M+N+j] + P ) % P << " \n"[j==rank-1]; } } COUT( rank << " " << M ); FOR( i , 0 , rank ){ FOR( j , 0 , M ){ cout << ( B[i][j] + P ) % P << " \n"[j==M-1]; } } } FINISH_MAIN; QUIT; }