#ifdef INCLUDE_MAIN inline void Solve() { // // 数 DEXPR( ll , bound_N , 200000 , 100 ); // 0が5個 // // DEXPR( ll , bound_N , 1000000000 , 100 ); // 0が9個 // // DEXPR( ll , bound_N , 1000000000000000000 , 100 ); // 0が18個 // CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); CIN( ll , N ); static ll N_sum = 0; // // CIN_ASSERT( N , 1 , bound_N ); // ランダムテスト用。 // ll answer = 0; // // MP answer = 0; // RETURN( answer ); // // 配列 // CIN_A( ll , A , N ); CIN_A( ll , B , N ); // // CIN_A( MP , A , N ); CIN_A( MP , B , N ); // // vector A( N ) , B( N ); // // vector A( N ) , B( N ); // // ll A[bound_N] , ll B[bound_N]; // 関数の引数に使う。 // // MP A[bound_N] , MP B[bound_N]; // 関数の引数に使う。 // // FOR( i , 0 , N ){ cin >> A[i] >> B[i]; } // // FOR( i , 0 , N * 2 ){ cin >> ( i < N ? A[i] : B[i-N] ); } // ll answer = 0; // // MP answer = 0; // // COUT_A( A , N ); // RETURN( answer ); // // 文字列 // CIN( string , S , T ); // ll answer = 0; // // MP answer = 0; // RETURN( answer ); // 順列 // vector A( N ); ll answer = 0; list A{}; static BIT bit{}; FOR( i , 0 , N ){ CIN( ll , Ai ); Ai--; ll count = bit.IntervalSum( N_sum , N_sum + Ai ); if( count < i - count ){ answer += count; A.push_front( Ai ); } else if( count > i - count || A.empty() ){ answer += i - count; A.push_back( Ai ); } else { if( A.front() < Ai ){ answer += i - count; A.push_back( Ai ); } else { answer += count; A.push_front( Ai ); } } bit.Add( N_sum + Ai , 1 ); } // MP answer = 0; // COUT( answer ); N_sum += N; COUT( answer ); auto itr = A.begin(); FOR( i , 0 , N ){ cout << ++(*itr++) << " \n"[i == N - 1]; } // RETURN( answer ); // // グラフ // e.resize( N ); // // e.resize( N ); // FOR( j , 0 , M ){ // CIN_ASSERT( uj , 1 , N ); CIN_ASSERT( vj , 1 , N ); // uj--; vj--; // e[uj].push_back( vj ); e[vj].push_back( uj ); // // CIN( ll , wj ); // // e[uj].push_back( { vj , wj } ); e[vj].push_back( { uj , wj } ); // } // ll answer = 0; // // MP answer = 0; // RETURN( answer ); // // 座標圧縮や単一クエリタイプなどのための入力格納 // vector > data( M ); // FOR( j , 0 , M ){ CIN( ll , x , y , z ); data[j] = { x , y , z }; } // ll answer = 0; // // MP answer = 0; // RETURN( answer ); // // 一般のクエリ // CIN( int , Q ); // // DEXPR( int , bound_Q , 100000 , 100 ); // 基本不要。 // // CIN_ASSERT( Q , 1 , bound_Q ); // 基本不要。 // // vector > query( Q ); // // vector > query( Q ); // FOR( q , 0 , Q ){ // CIN( int , type ); // if( type == 1 ){ // CIN( ll , x , y ); // // query[q] = { type , x , y }; // } else if( type == 2 ){ // CIN( ll , x , y ); // // query[q] = { type , x , y }; // COUT( x + y ); // } // // CIN( ll , x , y ); // // // query[q] = { x , y }; // // COUT( x + y ); // } // // sort( query , query + Q ); // // FOR( q , 0 , Q ){ // // auto& [x,y] = query[q]; // // // auto& [type,x,y] = query[q]; // // } // ll answer = 0; // // MP answer = 0; // RETURN( answer ); // // グリッド // // DEXPR( int , bound_H , 2000 , 30 ); // // // DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // // // CEXPR( int , bound_H , 1000000000 ); // 0が9個 // // CEXPR( int , bound_W , bound_H ); // // static_assert( ll( bound_H ) * bound_W < ll( 1 ) << 31 ); // // CEXPR( int , bound_HW , bound_H * bound_W ); // cin >> H >> W; // // SET_ASSERT( H , 1 , bound_H ); SET_ASSERT( W , 1 , bound_W ); // ランダムテスト用。 // H_minus = H - 1; W_minus = W - 1; HW = H * W; // vector S( H ); // FOR( i , 0 , H ){ // cin >> S[i]; // // SetEdgeOnGrid( S[i] , i , e ); // // SetWallOnGrid( S[i] , i , non_wall ); // } // // {h,w}へデコード: EnumHW( v ) // // {h,w}をコード: EnumHW_inv( h , w ); // // (i,j)->(k,h)の方向番号を取得: DirectionNumberOnGrid( i , j , k , h ); // // v->wの方向番号を取得: DirectionNumberOnGrid( v , w ); // // 方向番号の反転U<->D、R<->L: ReverseDirectionNumberOnGrid( n ); // ll answer = 0; // // MP answer = 0; // RETURN( answer ); } REPEAT_MAIN(200000); #else // INCLUDE_MAIN #ifdef INCLUDE_SUB template list E( const int& i ) { // list answer{}; list answer = e[i]; // VVV 入力によらない処理は以下に挿入する。 // AAA 入力によらない処理は以上に挿入する。 return answer; } template inline T F( const T& t ){ return f[t]; } template inline T G( const int& i ){ return g[i]; } // COMPAREに使用。圧縮時は削除する。 ll Naive( int N , int M , int K ) { ll answer = N + M + K; return answer; } // COMPAREに使用。圧縮時は削除する。 ll Answer( ll N , ll M , ll K ) { // START_WATCH; ll answer = N + M + K; // // TLに準じる乱択や全探索。デフォルトの猶予は100.0[ms]。 // CEXPR( double , TL , 2000.0 ); // while( CHECK_WATCH( TL ) ){ // } return answer; } // 圧縮時は中身だけ削除する。 inline void Experiment() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COUT( N , M , K , ":" , Naive( N , M , K ) ); // } // } // // cout << Naive( N ) << ",\n"[N==bound]; // } } // 圧縮時は中身だけ削除する。 inline void SmallTest() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COMPARE( N , M , K ); // } // } // // COMPARE( N ); // } } #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_SUB #ifdef INCLUDE_LIBRARY /* C-x 3 C-x o C-x C-fによるファイル操作用 BFS: c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/BreadthFirstSearch/compress.txt CoordinateCompress: c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/CoordinateCompress/compress.txt DFSOnTree c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/DepthFirstSearch/Tree/a.hpp Divisor: c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Prime/Divisor/compress.txt Polynomial c:/Users/user/Documents/Programming/Mathematics/Polynomial/compress.txt UnionFind c:/Users/user/Documents/Programming/Utility/VLTree/UnionFindForest/compress.txt */ // VVV 常設でないライブラリは以下に挿入する。 // AAA 常設でないライブラリは以上に挿入する。 #define INCLUDE_SUB #include __FILE__ #else // INCLUDE_LIBRARY // #define REACTIVE // #define USE_GETLINE #ifdef DEBUG #define _GLIBCXX_DEBUG #define REPEAT_MAIN( BOUND ) START_MAIN; signal( SIGABRT , &AlertAbort ); AutoCheck( exec_mode , use_getline ); if( exec_mode == sample_debug_mode || exec_mode == submission_debug_mode || exec_mode == library_search_mode ){ return 0; } else if( exec_mode == experiment_mode ){ Experiment(); return 0; } else if( exec_mode == small_test_mode ){ SmallTest(); return 0; }; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if( exec_mode == solve_mode ){ if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } } else if( exec_mode == random_test_mode ){ CERR( "ランダムテストを行う回数を指定してください。" ); SET_LL( test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE ) #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) if( exec_mode == solve_mode ){ SET_LL( A ); ASSERT( A , MIN , MAX ); } else if( exec_mode == random_test_mode ){ CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ); } else { assert( false ); } #define SOLVE_ONLY static_assert( __FUNCTION__[0] == 'S' ) #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define COUT( ... ) VariadicCout( cout << "出力: " , __VA_ARGS__ ) << endl #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl #define COUT_A( A , N ) cout << "出力: "; OUTPUT_ARRAY( cout , A , N ) << endl #define CERR_ITR( A ) OUTPUT_ITR( cerr , A ) << endl #define COUT_ITR( A ) cout << "出力: "; OUTPUT_ITR( cout , A ) << endl #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX ) #define SOLVE_ONLY #define CERR( ... ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define CERR_A( A , N ) #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL #define CERR_ITR( A ) #define COUT_ITR( A ) OUTPUT_ITR( cout , A ) << ENDL #endif #ifdef REACTIVE #define ENDL endl #else #define ENDL "\n" #endif #ifdef USE_GETLINE #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A ## _str ); } #define GETLINE_SEPARATE( SEPARATOR , ... ) SOLVE_ONLY; string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) SOLVE_ONLY; GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #else #define SET_LL( A ) cin >> A #define CIN( LL , ... ) SOLVE_ONLY; LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define SET_A( A , N ) SOLVE_ONLY; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; } #define CIN_A( LL , A , N ) vector A( N ); SET_A( A , N ); #endif #include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; template using T2 = pair; template using T3 = tuple; template using T4 = tuple; using path = pair; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define FINISH_MAIN REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } } #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now() #define CURRENT_TIME static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 ) #define TYPE_OF( VAR ) decay_t #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #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 FOREQINV( VAR , INITIAL , FINAL ) for( TYPE_OF( INITIAL ) VAR = INITIAL ; VAR + 1 > 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 SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ) #define OUTPUT_ARRAY( OS , A , N ) FOR( VARIABLE_FOR_OUTPUT_ARRAY , 0 , N ){ OS << A[VARIABLE_FOR_OUTPUT_ARRAY] << (VARIABLE_FOR_OUTPUT_ARRAY==N-1?"":" "); } OS #define OUTPUT_ITR( OS , A ) { auto ITERATOR_FOR_OUTPUT_ITR = A.begin() , END_FOR_OUTPUT_ITR = A.end(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; while( VARIABLE_FOR_OUTPUT_ITR ){ OS << *ITERATOR_FOR_COUT_ITR; ( VARIABLE_FOR_OUTPUT_ITR = ++ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR ) ? OS : OS << " "; } } OS #define RETURN( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); return #define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ ); auto answer = Answer( __VA_ARGS__ ); bool match = naive == answer; COUT( "(" , #__VA_ARGS__ , ") == (" , __VA_ARGS__ , ") : Naive == " , naive , match ? "==" : "!=" , answer , "== Answer" ); if( !match ){ return; } // 入出力用 template inline basic_istream& VariadicCin( basic_istream& is ) { return is; } template inline basic_istream& VariadicCin( basic_istream& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator ) { return is; } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); } template inline basic_ostream& operator<<( basic_ostream& os , const vector& arg ) { auto begin = arg.begin() , end = arg.end(); auto itr = begin; while( itr != end ){ ( itr == begin ? os : os << " " ) << *itr; itr++; } return os; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg& arg ) { return os << arg; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg1& arg1 , const Arg2& arg2 , const ARGS&... args ) { return VariadicCout( os << arg1 << " " , arg2 , args... ); } // 算術用 template constexpr T PositiveBaseResidue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } template constexpr T Residue( const T& a , const T& p ){ return PositiveBaseResidue( a , p < 0 ? -p : p ); } template constexpr T PositiveBaseQuotient( const T& a , const T& p ){ return ( a - PositiveBaseResidue( a , p ) ) / p; } template constexpr T Quotient( const T& a , const T& p ){ return p < 0 ? PositiveBaseQuotient( -a , -p ) : PositiveBaseQuotient( a , 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 = ( ( ARGUMENT ) % ( MODULO ) ) % ( MODULO ); \ ARGUMENT_FOR_SQUARE_FOR_POWER < 0 ? ARGUMENT_FOR_SQUARE_FOR_POWER += ( MODULO ) : ARGUMENT_FOR_SQUARE_FOR_POWER; \ 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 ) \ ll ANSWER[CONSTEXPR_LENGTH]; \ ll ANSWER_INV[CONSTEXPR_LENGTH]; \ 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 >= CONST_TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , CONST_TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ static_assert( ! is_same::value && ! is_same::value ); \ ll ANSWER = MINIMUM; \ { \ ll L_BS = MINIMUM; \ ll U_BS = MAXIMUM; \ ANSWER = UPDATE_ANSWER; \ ll EXPRESSION_BS; \ const ll CONST_TARGET_BS = ( CONST_TARGET ); \ ll DIFFERENCE_BS; \ while( L_BS < U_BS ){ \ DIFFERENCE_BS = ( EXPRESSION_BS = ( EXPRESSION ) ) - CONST_TARGET_BS; \ CERR( "二分探索中:" , "L_BS =" , L_BS , "<=" , ANSWER , "<=" , U_BS , "= U_BS :" , #EXPRESSION , "-" , #CONST_TARGET , "=" , EXPRESSION_BS , "-" , CONST_TARGET_BS , "=" , DIFFERENCE_BS ); \ if( DIFFERENCE_BS INEQUALITY_FOR_CHECK 0 ){ \ U_BS = UPDATE_U; \ } else { \ L_BS = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ if( L_BS > U_BS ){ \ CERR( "二分探索失敗:" , "L_BS =" , L_BS , ">" , U_BS , "= U_BS :" , #ANSWER , ":=" , #MAXIMUM , "+ 1 =" , MAXIMUM + 1 ); \ CERR( "二分探索マクロにミスがある可能性があります。変更前の版に戻してください。" ); \ ANSWER = MAXIMUM + 1; \ } else { \ CERR( "二分探索終了:" , "L_BS =" , L_BS , "<=" , ANSWER , "<=" , U_BS , "= U_BS" ); \ CERR( "二分探索が成功したかを確認するために" , #EXPRESSION , "を計算します。" ); \ CERR( "成功判定が不要な場合はこの計算を削除しても構いません。" ); \ EXPRESSION_BS = ( EXPRESSION ); \ CERR( "二分探索結果:" , #EXPRESSION , "=" , EXPRESSION_BS , ( EXPRESSION_BS > CONST_TARGET_BS ? ">" : EXPRESSION_BS < CONST_TARGET_BS ? "<" : "=" ) , CONST_TARGET_BS ); \ if( EXPRESSION_BS DESIRED_INEQUALITY CONST_TARGET_BS ){ \ CERR( "二分探索成功:" , #ANSWER , ":=" , ANSWER ); \ } else { \ CERR( "二分探索失敗:" , #ANSWER , ":=" , #MAXIMUM , "+ 1 =" , MAXIMUM + 1 ); \ CERR( "単調でないか、単調増加性と単調減少性を逆にしてしまったか、探索範囲内に解が存在しません。" ); \ ANSWER = MAXIMUM + 1; \ } \ } \ } \ // 単調増加の時にEXPRESSION >= CONST_TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CONST_TARGET , >= , ANSWER , ANSWER + 1 , ( L_BS + U_BS ) / 2 ) \ // 単調増加の時にEXPRESSION <= CONST_TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CONST_TARGET , > , ANSWER - 1 , ANSWER , ( L_BS + 1 + U_BS ) / 2 ) \ // 単調減少の時にEXPRESSION >= CONST_TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CONST_TARGET , < , ANSWER - 1 , ANSWER , ( L_BS + 1 + U_BS ) / 2 ) \ // 単調減少の時にEXPRESSION <= CONST_TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) \ BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CONST_TARGET , <= , ANSWER , ANSWER + 1 , ( L_BS + U_BS ) / 2 ) \ // t以下の値が存在すればその最大値のiterator、存在しなければend()を返す。 template inline typename set::iterator MaximumLeq( set& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.upper_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t未満の値が存在すればその最大値のiterator、存在しなければend()を返す。 template inline typename set::iterator MaximumLt( set& S , const T& t ) { const auto end = S.end(); if( S.empty() ){ return end; } auto itr = S.lower_bound( t ); return itr == end ? S.find( *( S.rbegin() ) ) : itr == S.begin() ? end : --itr; } // t以上の値が存在すればその最小値のiterator、存在しなければend()を返す。 template inline typename set::iterator MinimumGeq( set& S , const T& t ) { return S.lower_bound( t ); } // tより大きい値が存在すればその最小値のiterator、存在しなければend()を返す。 template inline typename set::iterator MinimumGt( set& S , const T& t ) { return S.upper_bound( t ); } // データ構造用 template inline T Add( const T& t0 , const T& t1 ) { return t0 + t1; } template inline T XorAdd( const T& t0 , const T& t1 ){ return t0 ^ t1; } template inline T Multiply( const T& t0 , const T& t1 ) { return t0 * t1; } template inline const T& Zero() { static const T z = 0; return z; } template inline const T& One() { static const T o = 1; return o; }\ template inline T AddInv( const T& t ) { return -t; } template inline T Id( const T& v ) { return v; } template inline T Min( const T& a , const T& b ){ return a < b ? a : b; } template inline T Max( const T& a , const T& b ){ return a < b ? b : a; } // グリッド問題用 int H , W , H_minus , W_minus , HW; vector > non_wall; inline T2 EnumHW( const int& v ) { return { v / W , v % W }; } inline int EnumHW_inv( const int& h , const int& w ) { return h * W + w; } const string direction[4] = {"U","R","D","L"}; // (i,j)->(k,h)の方向番号を取得 inline int DirectionNumberOnGrid( const int& i , const int& j , const int& k , const int& h ){return ik?0:jh?3:(assert(false),-1);} // v->wの方向番号を取得 inline int DirectionNumberOnGrid( const int& v , const int& w ){auto [i,j]=EnumHW(v);auto [k,h]=EnumHW(w);return DirectionNumberOnGrid(i,j,k,h);} // 方向番号の反転U<->D、R<->L inline int ReverseDirectionNumberOnGrid( const int& n ){assert(0<=n&&n<4);return(n+2)%4;} inline void SetEdgeOnGrid( const string& Si , const int& i , list ( &e )[] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){int v = EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back(v);}if(i+10){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1 ( &e )[] , const char& walkable = '.' ){FOR(j,0,W){if(Si[j]==walkable){const int v=EnumHW_inv(i,j);if(i>0){e[EnumHW_inv(i-1,j)].push_back({v,1});}if(i+10){e[EnumHW_inv(i,j-1)].push_back({v,1});}if(j+1 >& non_wall , const char& walkable = '.' , const char& unwalkable = '#' ){non_wall.push_back(vector(W));auto& non_wall_i=non_wall[i];FOR(j,0,W){non_wall_i[j]=Si[j]==walkable?true:(assert(Si[j]==unwalkable),false);}} // グラフ用 template vector > e; template map f; template vector g; // デバッグ用 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( int& exec_mode , const bool& use_getline ); inline void Solve(); inline void Experiment(); inline void SmallTest(); inline void RandomTest(); ll GetRand( const ll& Rand_min , const ll& Rand_max ); int exec_mode; CEXPR( int , solve_mode , 0 ); CEXPR( int , sample_debug_mode , 1 ); CEXPR( int , submission_debug_mode , 2 ); CEXPR( int , library_search_mode , 3 ); CEXPR( int , experiment_mode , 4 ); CEXPR( int , small_test_mode , 5 ); CEXPR( int , random_test_mode , 6 ); #ifdef USE_GETLINE CEXPR( bool , use_getline , true ); #else CEXPR( bool , use_getline , false ); #endif #else ll GetRand( const ll& Rand_min , const ll& Rand_max ) { ll answer = time( NULL ); return answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; } #endif // 圧縮用 #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& // VVV 常設ライブラリは以下に挿入する。 // ConstexprModulo // c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Mod/ConstexprModulo/a.hpp CEXPR(uint,P,998244353);TE CE INT& RS(INT& n)NE{RE n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n %= M;}TE CE uint& RS(uint& n)NE{RE n %= M;}TE CE ull& RS(ull& n)NE{RE n %= M;}TE CE INT& RSP(INT& n)NE{CE CO uint trunc =(1 << 23)- 1;INT n_u = n >> 23;n &= trunc;INT n_uq =(n_u / 7)/ 17;n_u -= n_uq * 119;n += n_u << 23;RE n < n_uq?n += P - n_uq:n -= n_uq;}TE <> CE ull& RS(ull& n)NE{CE CO ull Pull = P;CE CO ull Pull2 =(Pull - 1)*(Pull - 1);RE RSP(n > Pull2?n -= Pull2:n);}TE CE INT RS(INT&& n)NE{RE MO(RS(n));}TE CE INT RS(CO INT& n)NE{RE RS(INT(n));} #define SFINAE_FOR_MOD(DEFAULT)TY T,enable_if_t >::value>* DEFAULT #define DC_OF_CM_FOR_MOD(FUNC)CE bool OP FUNC(CO Mod& n)CO NE #define DC_OF_AR_FOR_MOD(FUNC)CE Mod OP FUNC(CO Mod& n)CO NE;TE CE Mod OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MOD(FUNC)TE CE bool Mod::OP FUNC(CO Mod& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MOD(FUNC,FORMULA)TE CE Mod Mod::OP FUNC(CO Mod& n)CO NE{RE MO(Mod(*TH)FUNC ## = n);}TE TE CE Mod Mod::OP FUNC(T&& n)CO NE{RE FORMULA;}TE CE Mod OP FUNC(T&& n0,CO Mod& n1)NE{RE MO(Mod(forward(n0))FUNC ## = n1);} TE CL Mod{PU:uint m_n;CE Mod()NE;CE Mod(CO Mod& n)NE;CE Mod(Mod& n)NE;CE Mod(Mod&& n)NE;TE CE Mod(CO T& n)NE;TE CE Mod(T& n)NE;TE CE Mod(T&& n)NE;CE Mod& OP=(CO Mod& n)NE;CE Mod& OP=(Mod&& n)NE;CE Mod& OP+=(CO Mod& n)NE;CE Mod& OP-=(CO Mod& n)NE;CE Mod& OP*=(CO Mod& n)NE;IN Mod& OP/=(CO Mod& n);CE Mod& OP<<=(int n)NE;CE Mod& OP>>=(int n)NE;CE Mod& OP++()NE;CE Mod OP++(int)NE;CE Mod& OP--()NE;CE Mod OP--(int)NE;DC_OF_CM_FOR_MOD(==);DC_OF_CM_FOR_MOD(!=);DC_OF_CM_FOR_MOD(<);DC_OF_CM_FOR_MOD(<=);DC_OF_CM_FOR_MOD(>);DC_OF_CM_FOR_MOD(>=);DC_OF_AR_FOR_MOD(+);DC_OF_AR_FOR_MOD(-);DC_OF_AR_FOR_MOD(*);DC_OF_AR_FOR_MOD(/);CE Mod OP<<(int n)CO NE;CE Mod OP>>(int n)CO NE;CE Mod OP-()CO NE;CE Mod& SignInvert()NE;CE Mod& Double()NE;CE Mod& Halve()NE;IN Mod& Invert();TE CE Mod& PositivePW(T&& EX)NE;TE CE Mod& NonNegativePW(T&& EX)NE;TE CE Mod& PW(T&& EX);CE VO swap(Mod& n)NE;CE CRUI RP()CO NE;ST CE Mod DeRP(CRUI n)NE;ST CE uint& Normalise(uint& n)NE;ST IN CO Mod& Inverse(CRUI n)NE;ST IN CO Mod& Factorial(CRUI n)NE;ST IN CO Mod& FactorialInverse(CRUI n)NE;ST IN Mod Combination(CRUI n,CRUI i)NE;ST IN CO Mod& zero()NE;ST IN CO Mod& one()NE;TE CE Mod& Ref(T&& n)NE;}; #define SFINAE_FOR_MN(DEFAULT)TY T,enable_if_t,decay_t >::value>* DEFAULT #define DC_OF_AR_FOR_MN(FUNC)IN MN OP FUNC(CO MN& n)CO NE;TE IN MN OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MN(FUNC)TE IN bool MN::OP FUNC(CO MN& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MN(FUNC,FORMULA)TE IN MN MN::OP FUNC(CO MN& n)CO NE{RE MO(MN(*TH)FUNC ## = n);}TE TE IN MN MN::OP FUNC(T&& n)CO NE{RE FORMULA;}TE IN MN OP FUNC(T&& n0,CO MN& n1)NE{RE MO(MN(forward(n0))FUNC ## = n1);} TE CL MN:PU Mod{PU:CE MN()NE;CE MN(CO MN& n)NE;CE MN(MN& n)NE;CE MN(MN&& n)NE;TE CE MN(CO T& n)NE;TE CE MN(T&& n)NE;CE MN& OP=(CO MN& n)NE;CE MN& OP=(MN&& n)NE;CE MN& OP+=(CO MN& n)NE;CE MN& OP-=(CO MN& n)NE;CE MN& OP*=(CO MN& n)NE;IN MN& OP/=(CO MN& n);CE MN& OP<<=(int n)NE;CE MN& OP>>=(int n)NE;CE MN& OP++()NE;CE MN OP++(int)NE;CE MN& OP--()NE;CE MN OP--(int)NE;DC_OF_AR_FOR_MN(+);DC_OF_AR_FOR_MN(-);DC_OF_AR_FOR_MN(*);DC_OF_AR_FOR_MN(/);CE MN OP<<(int n)CO NE;CE MN OP>>(int n)CO NE;CE MN OP-()CO NE;CE MN& SignInvert()NE;CE MN& Double()NE;CE MN& Halve()NE;CE MN& Invert();TE CE MN& PositivePW(T&& EX)NE;TE CE MN& NonNegativePW(T&& EX)NE;TE CE MN& PW(T&& EX);CE uint RP()CO NE;CE Mod Reduce()CO NE;ST CE MN DeRP(CRUI n)NE;ST IN CO MN& Formise(CRUI n)NE;ST IN CO MN& Inverse(CRUI n)NE;ST IN CO MN& Factorial(CRUI n)NE;ST IN CO MN& FactorialInverse(CRUI n)NE;ST IN MN Combination(CRUI n,CRUI i)NE;ST IN CO MN& zero()NE;ST IN CO MN& one()NE;ST CE uint Form(CRUI n)NE;ST CE ull& Reduction(ull& n)NE;ST CE ull& ReducedMU(ull& n,CRUI m)NE;ST CE uint MU(CRUI n0,CRUI n1)NE;ST CE uint BaseSquareTruncation(uint& n)NE;TE CE MN& Ref(T&& n)NE;};TE CE MN Twice(CO MN& n)NE;TE CE MN Half(CO MN& n)NE;TE CE MN Inverse(CO MN& n);TE CE MN PW(MN n,T EX);TE CE MN<2> PW(CO MN<2>& n,CO T& p);TE CE T Square(CO T& t);TE <> CE MN<2> Square >(CO MN<2>& t);TE CE VO swap(MN& n0,MN& n1)NE;TE IN string to_string(CO MN& n)NE;TE IN basic_istream& OP>>(basic_istream& is,MN& n);TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n); TE CL COantsForMod{PU:COantsForMod()= delete;ST CE CO bool g_even =((M & 1)== 0);ST CE CO uint g_memory_bound = 1000000;ST CE CO uint g_memory_LE = M < g_memory_bound?M:g_memory_bound;ST CE ull MNBasePW(ull&& EX)NE;ST CE uint g_M_minus = M - 1;ST CE uint g_M_minus_2 = M - 2;ST CE uint g_M_minus_2_neg = 2 - M;ST CE CO int g_MN_digit = 32;ST CE CO ull g_MN_base = ull(1)<< g_MN_digit;ST CE CO uint g_MN_base_minus = uint(g_MN_base - 1);ST CE CO uint g_MN_digit_half =(g_MN_digit + 1)>> 1;ST CE CO uint g_MN_base_sqrt_minus =(1 << g_MN_digit_half)- 1;ST CE CO uint g_MN_M_neg_inverse = uint((g_MN_base - MNBasePW((ull(1)<<(g_MN_digit - 1))- 1))& g_MN_base_minus);ST CE CO uint g_MN_base_mod = uint(g_MN_base % M);ST CE CO uint g_MN_base_square_mod = uint(((g_MN_base % M)*(g_MN_base % M))% M);};TE CE ull COantsForMod::MNBasePW(ull&& EX)NE{ull prod = 1;ull PW = M;WH(EX != 0){(EX & 1)== 1?(prod *= PW)&= g_MN_base_minus:prod;EX >>= 1;(PW *= PW)&= g_MN_base_minus;}RE prod;} US MP = Mod

;US MNP = MN

;TE CE uint MN::Form(CRUI n)NE{ull n_copy = n;RE uint(MO(Reduction(n_copy *= COantsForMod::g_MN_base_square_mod)));}TE CE ull& MN::Reduction(ull& n)NE{ull n_sub = n & COantsForMod::g_MN_base_minus;RE((n +=((n_sub *= COantsForMod::g_MN_M_neg_inverse)&= COantsForMod::g_MN_base_minus)*= M)>>= COantsForMod::g_MN_digit)< M?n:n -= M;}TE CE ull& MN::ReducedMU(ull& n,CRUI m)NE{RE Reduction(n *= m);}TE CE uint MN::MU(CRUI n0,CRUI n1)NE{ull n0_copy = n0;RE uint(MO(ReducedMU(ReducedMU(n0_copy,n1),COantsForMod::g_MN_base_square_mod)));}TE CE uint MN::BaseSquareTruncation(uint& n)NE{CO uint n_u = n >> COantsForMod::g_MN_digit_half;n &= COantsForMod::g_MN_base_sqrt_minus;RE n_u;}TE CE MN::MN()NE:Mod(){static_assert(! COantsForMod::g_even);}TE CE MN::MN(CO MN& n)NE:Mod(n){}TE CE MN::MN(MN& n)NE:Mod(n){}TE CE MN::MN(MN&& n)NE:Mod(MO(n)){}TE TE CE MN::MN(CO T& n)NE:Mod(n){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE TE CE MN::MN(T&& n)NE:Mod(forward(n)){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE CE MN& MN::OP=(CO MN& n)NE{RE Ref(Mod::OP=(n));}TE CE MN& MN::OP=(MN&& n)NE{RE Ref(Mod::OP=(MO(n)));}TE CE MN& MN::OP+=(CO MN& n)NE{RE Ref(Mod::OP+=(n));}TE CE MN& MN::OP-=(CO MN& n)NE{RE Ref(Mod::OP-=(n));}TE CE MN& MN::OP*=(CO MN& n)NE{ull m_n_copy = Mod::m_n;RE Ref(Mod::m_n = MO(ReducedMU(m_n_copy,n.m_n)));}TE IN MN& MN::OP/=(CO MN& n){RE OP*=(MN(n).Invert());}TE CE MN& MN::OP<<=(int n)NE{RE Ref(Mod::OP<<=(n));}TE CE MN& MN::OP>>=(int n)NE{RE Ref(Mod::OP>>=(n));}TE CE MN& MN::OP++()NE{RE Ref(Mod::Normalise(Mod::m_n += COantsForMod::g_MN_base_mod));}TE CE MN MN::OP++(int)NE{MN n{*TH};OP++();RE n;}TE CE MN& MN::OP--()NE{RE Ref(Mod::m_n < COantsForMod::g_MN_base_mod?((Mod::m_n += M)-= COantsForMod::g_MN_base_mod):Mod::m_n -= COantsForMod::g_MN_base_mod);}TE CE MN MN::OP--(int)NE{MN n{*TH};OP--();RE n;}DF_OF_AR_FOR_MN(+,MN(forward(n))+= *TH);DF_OF_AR_FOR_MN(-,MN(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MN(*,MN(forward(n))*= *TH);DF_OF_AR_FOR_MN(/,MN(forward(n)).Invert()*= *TH);TE CE MN MN::OP<<(int n)CO NE{RE MO(MN(*TH)<<= n);}TE CE MN MN::OP>>(int n)CO NE{RE MO(MN(*TH)>>= n);}TE CE MN MN::OP-()CO NE{RE MO(MN(*TH).SignInvert());}TE CE MN& MN::SignInvert()NE{RE Ref(Mod::m_n > 0?Mod::m_n = M - Mod::m_n:Mod::m_n);}TE CE MN& MN::Double()NE{RE Ref(Mod::Double());}TE CE MN& MN::Halve()NE{RE Ref(Mod::Halve());}TE CE MN& MN::Invert(){assert(Mod::m_n > 0);RE PositivePW(uint(COantsForMod::g_M_minus_2));}TE TE CE MN& MN::PositivePW(T&& EX)NE{MN PW{*TH};(--EX)%= COantsForMod::g_M_minus_2;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE TE CE MN& MN::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(Mod::m_n = COantsForMod::g_MN_base_mod):PositivePW(forward(EX));}TE TE CE MN& MN::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod::m_n == 0));RE neg?PositivePW(forward(EX *= COantsForMod::g_M_minus_2_neg)):NonNegativePW(forward(EX));}TE CE uint MN::RP()CO NE{ull m_n_copy = Mod::m_n;RE MO(Reduction(m_n_copy));}TE CE Mod MN::Reduce()CO NE{ull m_n_copy = Mod::m_n;RE Mod::DeRP(MO(Reduction(m_n_copy)));}TE CE MN MN::DeRP(CRUI n)NE{RE MN(Mod::DeRP(n));}TE IN CO MN& MN::Formise(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = DeRP(LE_curr);LE_curr++;}RE memory[n];}TE IN CO MN& MN::Inverse(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN(Mod::Inverse(LE_curr));LE_curr++;}RE memory[n];}TE IN CO MN& MN::Factorial(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};ST MN val_last{one()};WH(LE_curr <= n){memory[LE_curr++] = val_curr *= ++val_last;}RE memory[n];}TE IN CO MN& MN::FactorialInverse(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};ST MN val_last{one()};WH(LE_curr <= n){memory[LE_curr] = val_curr *= Inverse(LE_curr);LE_curr++;}RE memory[n];}TE IN MN MN::Combination(CRUI n,CRUI i)NE{RE i <= n?Factorial(n)*FactorialInverse(i)*FactorialInverse(n - i):zero();}TE IN CO MN& MN::zero()NE{ST CE CO MN z{};RE z;}TE IN CO MN& MN::one()NE{ST CE CO MN o{DeRP(1)};RE o;}TE TE CE MN& MN::Ref(T&& n)NE{RE *TH;}TE CE MN Twice(CO MN& n)NE{RE MO(MN(n).Double());}TE CE MN Half(CO MN& n)NE{RE MO(MN(n).Halve());}TE CE MN Inverse(CO MN& n){RE MO(MN(n).Invert());}TE CE MN PW(MN n,T EX){RE MO(n.PW(EX));}TE CE VO swap(MN& n0,MN& n1)NE{n0.swap(n1);}TE IN string to_string(CO MN& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_istream& OP>>(basic_istream& is,MN& n){ll m;is >> m;n = m;RE is;}TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n){RE os << n.RP();} TE CE Mod::Mod()NE:m_n(){}TE CE Mod::Mod(CO Mod& n)NE:m_n(n.m_n){}TE CE Mod::Mod(Mod& n)NE:m_n(n.m_n){}TE CE Mod::Mod(Mod&& n)NE:m_n(MO(n.m_n)){}TE TE CE Mod::Mod(CO T& n)NE:m_n(RS(n)){}TE TE CE Mod::Mod(T& n)NE:m_n(RS(decay_t(n))){}TE TE CE Mod::Mod(T&& n)NE:m_n(RS(forward(n))){}TE CE Mod& Mod::OP=(CO Mod& n)NE{RE Ref(m_n = n.m_n);}TE CE Mod& Mod::OP=(Mod&& n)NE{RE Ref(m_n = MO(n.m_n));}TE CE Mod& Mod::OP+=(CO Mod& n)NE{RE Ref(Normalise(m_n += n.m_n));}TE CE Mod& Mod::OP-=(CO Mod& n)NE{RE Ref(m_n < n.m_n?(m_n += M)-= n.m_n:m_n -= n.m_n);}TE CE Mod& Mod::OP*=(CO Mod& n)NE{RE Ref(m_n = COantsForMod::g_even?RS(ull(m_n)* n.m_n):MN::MU(m_n,n.m_n));}TE <> CE MP& MP::OP*=(CO MP& n)NE{ull m_n_copy = m_n;RE Ref(m_n = MO((m_n_copy *= n.m_n)< P?m_n_copy:RSP(m_n_copy)));}TE IN Mod& Mod::OP/=(CO Mod& n){RE OP*=(Mod(n).Invert());}TE CE Mod& Mod::OP<<=(int n)NE{WH(n-- > 0){Normalise(m_n <<= 1);}RE *TH;}TE CE Mod& Mod::OP>>=(int n)NE{WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += M)>>= 1;}RE *TH;}TE CE Mod& Mod::OP++()NE{RE Ref(m_n < COantsForMod::g_M_minus?++m_n:m_n = 0);}TE CE Mod Mod::OP++(int)NE{Mod n{*TH};OP++();RE n;}TE CE Mod& Mod::OP--()NE{RE Ref(m_n == 0?m_n = COantsForMod::g_M_minus:--m_n);}TE CE Mod Mod::OP--(int)NE{Mod n{*TH};OP--();RE n;}DF_OF_CM_FOR_MOD(==);DF_OF_CM_FOR_MOD(!=);DF_OF_CM_FOR_MOD(>);DF_OF_CM_FOR_MOD(>=);DF_OF_CM_FOR_MOD(<);DF_OF_CM_FOR_MOD(<=);DF_OF_AR_FOR_MOD(+,Mod(forward(n))+= *TH);DF_OF_AR_FOR_MOD(-,Mod(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MOD(*,Mod(forward(n))*= *TH);DF_OF_AR_FOR_MOD(/,Mod(forward(n)).Invert()*= *TH);TE CE Mod Mod::OP<<(int n)CO NE{RE MO(Mod(*TH)<<= n);}TE CE Mod Mod::OP>>(int n)CO NE{RE MO(Mod(*TH)>>= n);}TE CE Mod Mod::OP-()CO NE{RE MO(Mod(*TH).SignInvert());}TE CE Mod& Mod::SignInvert()NE{RE Ref(m_n > 0?m_n = M - m_n:m_n);}TE CE Mod& Mod::Double()NE{RE Ref(Normalise(m_n <<= 1));}TE CE Mod& Mod::Halve()NE{RE Ref(((m_n & 1)== 0?m_n:m_n += M)>>= 1);}TE IN Mod& Mod::Invert(){assert(m_n > 0);uint m_n_neg;RE m_n < COantsForMod::g_memory_LE?Ref(m_n = Inverse(m_n).m_n):((m_n_neg = M - m_n)< COantsForMod::g_memory_LE)?Ref(m_n = M - Inverse(m_n_neg).m_n):PositivePW(uint(COantsForMod::g_M_minus_2));}TE <> IN Mod<2>& Mod<2>::Invert(){assert(m_n > 0);RE *TH;}TE TE CE Mod& Mod::PositivePW(T&& EX)NE{Mod PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <> TE CE Mod<2>& Mod<2>::PositivePW(T&& EX)NE{RE *TH;}TE TE CE Mod& Mod::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(m_n = 1):Ref(PositivePW(forward(EX)));}TE TE CE Mod& Mod::PW(T&& EX){bool neg = EX < 0;assert(!(neg && m_n == 0));neg?EX *= COantsForMod::g_M_minus_2_neg:EX;RE m_n == 0?*TH:(EX %= COantsForMod::g_M_minus)== 0?Ref(m_n = 1):PositivePW(forward(EX));}TE IN CO Mod& Mod::Inverse(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr].m_n = M - MN::MU(memory[M % LE_curr].m_n,M / LE_curr);LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::Factorial(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::Factorial(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::FactorialInverse(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::FactorialInverse(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN Mod Mod::Combination(CRUI n,CRUI i)NE{RE MN::Combination(n,i).Reduce();}TE CE VO Mod::swap(Mod& n)NE{std::swap(m_n,n.m_n);}TE CE CRUI Mod::RP()CO NE{RE m_n;}TE CE Mod Mod::DeRP(CRUI n)NE{Mod n_copy{};n_copy.m_n = n;RE n_copy;}TE CE uint& Mod::Normalise(uint& n)NE{RE n < M?n:n -= M;}TE IN CO Mod& Mod::zero()NE{ST CE CO Mod z{};RE z;}TE IN CO Mod& Mod::one()NE{ST CE CO Mod o{DeRP(1)};RE o;}TE TE CE Mod& Mod::Ref(T&& n)NE{RE *TH;}TE CE Mod Twice(CO Mod& n)NE{RE MO(Mod(n).Double());}TE CE Mod Half(CO Mod& n)NE{RE MO(Mod(n).Halve());}TE IN Mod Inverse(CO Mod& n){RE MO(Mod(n).Invert());}TE CE Mod Inverse_COrexpr(CRUI n)NE{RE MO(Mod::DeRP(RS(n)).NonNegativePW(M - 2));}TE CE Mod PW(Mod n,T EX){RE MO(n.PW(EX));}TE CE Mod<2> PW(Mod<2> n,const T& p){RE p == 0?Mod<2>::one():move(n);}TE CE VO swap(Mod& n0,Mod& n1)NE{n0.swap(n1);}TE IN string to_string(CO Mod& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_ostream& OP<<(basic_ostream& os,CO Mod& n){RE os << n.RP();} // IntervalAddBIT // c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/AffineSpace/BIT/IntervalAdd/a.hpp TECL PWInverse_CE{PU:int m_val;CE PWInverse_CE();}; TECE PWInverse_CE::PWInverse_CE():m_val(1){WH(m_val < N){m_val <<= 1;}} TE CL BIT{PU:T m_fenwick[N + 1];IN BIT();BIT(CO T(&a)[N]);IN T Get(CRI i)CO;IN VO Set(CRI i,CO T& n);IN VO Set(CO T(&a)[N]);IN VO Initialise();IN BIT& OP+=(CO T(&a)[N]);VO Add(CRI i,CO T& n);T InitialSegmentSum(CRI i_final)CO;IN T IntervalSum(CRI i_start,CRI i_final)CO;int BinarySearch(CO T& n)CO;IN int BinarySearch(CRI i_start,CO T& n)CO;}; TE IN BIT::BIT():m_fenwick(){static_assert(! is_same::value);}TE BIT::BIT(CO T(&a)[N]):m_fenwick(){static_assert(! is_same::value);for(int j = 1;j <= N;j++){T& fenwick_j = m_fenwick[j];int i = j - 1;fenwick_j = a[i];int i_lim = j -(j & -j);WH(i != i_lim){fenwick_j += m_fenwick[i];i -=(i & -i);}}}TE IN T BIT::Get(CRI i)CO{RE IntervalSum(i,i);}TE IN VO BIT::Set(CRI i,CO T& n){Add(i,n - IntervalSum(i,i));}TE IN VO BIT::Set(CO T(&a)[N]){BIT a_copy{a};swap(m_fenwick,a_copy.m_fenwick);}TE IN VO BIT::Initialise(){for(int j = 1;j <= N;j++){m_fenwick[j] = 0;}}TE IN BIT& BIT::OP+=(CO T(&a)[N]){for(int i = 0;i < N;i++){Add(i,a[i]);}RE *TH;}TE VO BIT::Add(CRI i,CO T& n){int j = i + 1;WH(j <= N){m_fenwick[j] += n;j +=(j & -j);}RE;}TE T BIT::InitialSegmentSum(CRI i_final)CO{T sum = 0;int j =(i_final < N?i_final:N - 1)+ 1;WH(j > 0){sum += m_fenwick[j];j -= j & -j;}RE sum;}TE IN T BIT::IntervalSum(CRI i_start,CRI i_final)CO{RE InitialSegmentSum(i_final)- InitialSegmentSum(i_start - 1);}TE int BIT::BinarySearch(CO T& n)CO{int j = 0;int PW = PWInverse_CE().m_val;T sum{};T sum_next{};WH(PW > 0){int j_next = j | PW;if(j_next < N){sum_next += m_fenwick[j_next];if(sum_next < n){sum = sum_next;j = j_next;}else{sum_next = sum;}}PW >>= 1;}RE j;}TE IN int BIT::BinarySearch(CRI i_start,CO T& n)CO{RE max(i_start,BinarySearch(InitialSegmentSum(i_start)+ n));} TE CL IntervalAddBIT{PU:BIT m_bit_0;BIT m_bit_1;IN IntervalAddBIT();IN IntervalAddBIT(CO T(&a)[N]);IN T Get(CRI i)CO;IN VO Set(CRI i,CO T& n);IN VO Set(CO T(&a)[N]);IN VO Initialise();IN IntervalAddBIT& OP+=(CO T(&a)[N]);IN VO Add(CRI i,CO T& n);IN VO IntervalAdd(CRI i_start,CRI i_final,CO T& n);IN T InitialSegmentSum(CRI i_final)CO;IN T IntervalSum(CRI i_start,CRI i_final)CO;}; TE IN IntervalAddBIT::IntervalAddBIT():m_bit_0(),m_bit_1(){}TE IN IntervalAddBIT::IntervalAddBIT(CO T(&a)[N]):m_bit_0(),m_bit_1(){OP+=(a);}TE IN T IntervalAddBIT::Get(CRI i)CO{RE IntervalSum(i,i);}TE IN VO IntervalAddBIT::Set(CRI i,CO T& n){Add(i,n - IntervalSum(i,i));}TE IN VO IntervalAddBIT::Set(CO T(&a)[N]){IntervalAddBIT a_copy{a};swap(m_bit_0,a_copy.m_bit_0);swap(m_bit_1,a_copy.m_bit_1);}TE IN VO IntervalAddBIT::Initialise(){m_bit_0.Initialise();m_bit_1.Initialise();}TE IN IntervalAddBIT& IntervalAddBIT::OP+=(CO T(&a)[N]){for(int i = 0;i < N;i++){Add(i,a[i]);}RE *TH;}TE IN VO IntervalAddBIT::Add(CRI i,CO T& n){IntervalAdd(i,i,n);}TE IN VO IntervalAddBIT::IntervalAdd(CRI i_start,CRI i_final,CO T& n){m_bit_0.Add(i_start,-(i_start - 1)* n);m_bit_0.Add(i_final + 1,i_final * n);m_bit_1.Add(i_start,n);m_bit_1.Add(i_final + 1,- n);}TE IN T IntervalAddBIT::InitialSegmentSum(CRI i_final)CO{RE m_bit_0.InitialSegmentSum(i_final)+ i_final * m_bit_1.InitialSegmentSum(i_final);}TE IN T IntervalAddBIT::IntervalSum(CRI i_start,CRI i_final)CO{RE InitialSegmentSum(i_final)- InitialSegmentSum(i_start - 1);} // AAA 常設ライブラリは以上に挿入する。 #define INCLUDE_LIBRARY #include __FILE__ #endif // INCLUDE_LIBRARY #endif // INCLUDE_SUB #endif // INCLUDE_MAIN