#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( MaximisationProbability ); 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 \ class BREADTH ## FirstSearch_Body \ { \ \ protected: \ int m_V; \ int m_init; \ list m_next; \ bool m_found[V_max]; \ int m_prev[V_max]; \ \ public: \ inline BREADTH ## FirstSearch_Body( const int& V ); \ inline BREADTH ## FirstSearch_Body( const int& V , const int& init ); \ \ inline void Reset( const int& init ); \ inline void Shift( const int& init ); \ \ inline const int& size() const; \ inline const int& init() const; \ inline bool& found( const int& i ); \ inline const int& prev( const int& i ) const; \ \ int Next(); \ \ private: \ virtual list e( const int& t ) = 0; \ \ }; \ \ template E(const int&)> \ class BREADTH ## FirstSearch : \ public BREADTH ## FirstSearch_Body \ { \ \ public: \ \ template inline BREADTH ## FirstSearch( const Args&... args ); \ \ private: \ inline list e( const int& t ); \ \ }; \ \ template E(const int&)> void BREADTH ## FirstConnectedComponentSearch( const int& V , int ( &vertex )[V_max] , int& count ); \ #define DEFINITION_OF_FIRST_SEARCH( BREADTH , PUSH ) \ template inline BREADTH ## FirstSearch_Body::BREADTH ## FirstSearch_Body( const int& 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; } } \ template inline BREADTH ## FirstSearch_Body::BREADTH ## FirstSearch_Body( const int& V , const int& init ) : BREADTH ## FirstSearch_Body( V ) { m_init = init; m_next.push_back( m_init ); m_found[m_init] = true; } \ template E(const int&)> template inline BREADTH ## FirstSearch::BREADTH ## FirstSearch( const Args&... args ) : BREADTH ## FirstSearch_Body( args... ) {} \ \ template inline void BREADTH ## FirstSearch_Body::Reset( const int& 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; } } \ template inline void BREADTH ## FirstSearch_Body::Shift( const int& 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; } } \ \ template inline const int& BREADTH ## FirstSearch_Body::size() const { return m_V; } \ template inline const int& BREADTH ## FirstSearch_Body::init() const { return m_init; } \ template inline bool& BREADTH ## FirstSearch_Body::found( const int& i ) { assert( i < m_V ); return m_found[i]; } \ template inline const int& BREADTH ## FirstSearch_Body::prev( const int& i ) const { assert( i < m_V ); return m_prev[i]; } \ \ template \ int BREADTH ## FirstSearch_Body::Next() \ { \ \ if( m_next.empty() ){ \ \ return -1; \ \ } \ \ const int i_curr = m_next.front(); \ m_next.pop_front(); \ list edge = e( i_curr ); \ \ while( ! edge.empty() ){ \ \ const int& 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(); \ \ } \ \ return i_curr; \ \ } \ \ template E(const int&)> inline list BREADTH ## FirstSearch::e( const int& t ) { return E( t ); } \ \ template E(const int&)> void BREADTH ## FirstConnectedComponentSearch( const int& 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(); \ \ while( j != -1 ? vertex[j] == -1 : false ){ \ \ vertex[j] = count; \ j = bfs.Next(); \ \ } \ \ count++; \ \ } \ \ } \ \ return; \ \ } \ DECLARATION_OF_FIRST_SEARCH( Breadth ); // (V,E)におけるinitからの深さを格納(O((initの連結成分)+(initの連結成分におけるEのサイズの合計))) // 到達できない点は深さの代わりに-1を格納。 template E(const int&)> void SetDepth( const int& V , const int& init , int ( &depth )[V_max] ); // 探索畤にbfsを初期化して流用する。 template E(const int&)> void SetDepth( BreadthFirstSearch& bfs , const int& V , const int& init , int ( &depth )[V_max] ); DEFINITION_OF_FIRST_SEARCH( Breadth , push_back ); template E(const int&)> void SetDepth_Body( BreadthFirstSearch& bfs , const int& V , const int& init , int ( &depth )[V_max] ) { for( int i = 1 ; i < V ; i++ ){ depth[i] = -1; } int i = bfs.Next(); depth[i] = 0; while( ( i = bfs.Next() ) != -1 ){ depth[i] = depth[bfs.prev( i )] + 1; } return; } template E(const int&)> void SetDepth( const int& V , const int& init , int ( &depth )[V_max] ) { BreadthFirstSearch bfs{ V , init }; SetDepth_Body( bfs , V , init , depth ); return; } template E(const int&)> void SetDepth( BreadthFirstSearch& bfs , const int& V , const int& init , int ( &depth )[V_max] ) { bfs.Reset( init ); SetDepth_Body( bfs , V , init , depth ); return; } string Direction( const int& i , const int& j , const int& k , const int& h ) { if( i < k ){ return "D"; } if( i > k ){ return "U"; } if( j < h ){ return "R"; } if( j > h ){ return "L"; } assert( false ); return "BUG"; } string Reverse( const string& D ) { if( D == "R" ){ return "L"; } if( D == "L" ){ return "R"; } if( D == "U" ){ return "D"; } if( D == "D" ){ return "U"; } assert( false ); return "BUG"; } // // inline CEXPR( int , bound_N , 10 ); // inline DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 // // inline CEXPR( int , bound_N , 1000000000 ); // 0が9個 // // inline CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 // int N; // // inline CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // // inline CEXPR( int , bound_M , 10 ); // inline DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // // inline CEXPR( int , bound_M , 1000000000 ); // 0が9個 // // inline CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // int M; inline DEXPR( int , bound_H , 500 , 10 ); // inline DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // inline CEXPR( int , bound_H , 1000000000 ); // 0が9個 inline CEXPR( int , bound_W , bound_H ); #if bound_H < ( 1 << 16 ) inline CEXPR( int , bound_HW , bound_H * bound_W ); #else inline CEXPR( ll , bound_HW , ll( bound_H ) * bound_W ); #endif // CEXPR( int , bound_HW , 100000 ); // 0が5個 // CEXPR( int , bound_HW , 1000000000 ); // 0が5個 int H , W; inline int EnumHW( const int& h , const int& w ) { return h * W + w; } inline pair EnumHW_inv( const int& v ) { return { v / W , v % W }; } // list e[bound_N]; static list e[bound_HW]; list E( const int& i ) { list answer = e[i]; // 入力によらない処理 return answer; } // template inline T add( const T& t0 , const T& t1 ) { return t0 + t1; } // template inline const T& zero() { static const T z = 0; return z; } // template inline T multiply( const T& t0 , const T& t1 ) { return t0 * t1; } // template inline const T& one() { static const T o = 1; return o; } // 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 ){ // } // SET_ASSERT( N , 1 , bound_N ); // SET_ASSERT( M , 1 , bound_M ); SET_ASSERT( H , 3 , bound_H ); SET_ASSERT( W , 3 , bound_W ); TYPE_OF( bound_HW ) HW = TYPE_OF( bound_HW )( H ) * W; // assert( HW <= bound_HW ); DEXPR( int , bound_K , 500000 , 10 ); CIN_ASSERT( K , 4 , bound_K ); CIN_ASSERT( L , 1 , K ); CIN_ASSERT( R , L , K ); string S[bound_H]; FOR( i , 0 , H ){ CIN( string , Si ); assert( int( Si.size() ) == W ); FOR( j , 0 , W ){ if( Si[j] == '.' ){ int v = EnumHW(i,j); if( i > 0 ){ e[EnumHW(i-1,j)].push_back( v ); } if( i + 1 < H ){ e[EnumHW(i+1,j)].push_back( v ); } if( j > 0 ){ e[EnumHW(i,j-1)].push_back( v ); } if( j + 1 < W ){ e[EnumHW(i,j+1)].push_back( v ); } } } S[i] = Si; } if( ( R - L ) % 2 == 0 ){ RETURN( "No" ); } int depth[2][bound_HW]; BreadthFirstSearch bfs0{ HW }; BreadthFirstSearch bfs1{ HW }; SetDepth( bfs0 , HW , EnumHW( 0 , 0 ) , depth[0] ); SetDepth( bfs1 , HW , EnumHW( H - 1 , W - 1 ) , depth[1] ); int R_reverse = K - R + 1; FOR( v , 0 , HW ){ int& depth0v = depth[0][v]; if( depth0v == -1 || depth0v >= L || ( depth0v - L ) % 2 == 0 ){ continue; } int& depth1v = depth[1][v]; if( depth1v == -1 || depth1v >= R_reverse || ( depth1v - R_reverse ) % 2 == 0 ){ continue; } auto [i,j] = EnumHW_inv( v ); string answer_LR = ""; if( 0 < i && i + 1 < H ){ if( S[i-1][j] == '.' && S[i+1][j] == '.' ){ answer_LR = "U"; } } if( 0 < j && j + 1 < W ){ if( S[i][j-1] == '.' && S[i][j+1] == '.' ){ answer_LR = "L"; } } if( answer_LR != "" ){ string answer[bound_K + 1]; int w = v; int k = i; int h = j; FOREQINV( n , depth0v , 1 ){ int u = bfs0.prev( w ); auto [k_prev,h_prev] = EnumHW_inv( u ); answer[n] = Direction( k_prev , h_prev , k , h ); w = u; k = k_prev; h = h_prev; } string& answer_L_front = answer[depth0v]; string answer_L_front_reverse = Reverse( answer_L_front ); FOR( n , depth0v + 1 , L ){ answer[n] = ( n - depth0v ) % 2 == 1 ? answer_L_front_reverse : answer_L_front; } string answer_LR_reverse = Reverse( answer_LR ); FOREQ( n , L , R ){ answer[n] = ( n - L ) % 2 == 0 ? answer_LR_reverse : answer_LR; } int depth1v_reverse = K - depth1v + 1; w = v; k = i; h = j; FOREQ( n , depth1v_reverse , K ){ int u = bfs1.prev( w ); auto [k_next,h_next] = EnumHW_inv( u ); answer[n] = Direction( k , h , k_next , h_next ); w = u; k = k_next; h = h_next; } string& answer_R_back = answer[depth1v_reverse]; string answer_R_back_reverse = Reverse( answer_R_back ); FOR( n , R + 1 , depth1v_reverse ){ answer[n] = ( n - R ) % 2 == 1 ? answer_R_back : answer_R_back_reverse; } COUT( "Yes" ); FOREQ( n , 1 , K ){ cout << answer[n]; } RETURN( "" ); } } RETURN( "No" ); FINISH_MAIN; QUIT; }