#ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE // #define USE_GETLINE #endif #ifdef INCLUDE_MAIN inline void Solve() { CIN( int , N , M ); CIN_A( ll , A , N ); constexpr PrimeEnumeration pe{}; map prime_set{}; vector Prime[N]; gA>.resize( N ); FOR( i , 0 , N ){ auto& Prime_i = Prime[i]; SetPrimeFactorisation( pe , A[i] , Prime_i , gA>[i] ); FOR_ITR( Prime_i ){ prime_set[*itr]; } } int prime_size = 0; FOR_ITR( prime_set ){ itr->second = prime_size++; } FOR( i , 0 , N ){ vector temp( prime_size ); int prime_i_size = Prime[i].size(); FOR( j , 0 , prime_i_size ){ temp[prime_set[Prime[i][j]]] = gA>[i][j]; } Prime[i].resize( prime_size ); gA>[i] = temp; } gE.resize( N ); FOR( j , 0 , M ){ CIN_ASSERT( uj , 1 , N ); CIN_ASSERT( vj , 1 , N ); uj--; vj--; gE[uj].push_back( vj ); gE[vj].push_back( uj ); } int current_num = 0; auto edge = [&]( const int& i ){ list answer{}; auto& gEi = gE[i]; FOR_ITR( gEi ){ answer.push_back( { *itr , max( gA>[i][current_num] , gA>[*itr][current_num] ) } ); } return answer; }; FOR_ITR( prime_set ){ EnumerationDijkstra d{ N , Id , Id , Max , Zero , 1LL << 18 , -1LL , edge }; vector answer{}; d.Solve( 0 , answer ); FOR( i , 1 , N ){ Prime[i][current_num] = answer[i]; } current_num++; } COUT( A[0] ); FOR( i , 1 , N ){ MP answer = 1; FOR_ITR( prime_set ){ answer *= PW( MP( itr-> first ) , Prime[i][itr->second] ); } COUT( answer ); } } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_SUB // グラフ用 template Map gF; template vector gA; template vector > gE; // 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 常設でないライブラリは以下に挿入する。 // nの素因数分解:SetPrimeFactorisation(CO PrimeEnumeration& prime,CO INT& n,VE& P,VE& EX) // 他の関数は Divisor/compress.txt に。 TE CL PrimeEnumeration{PU:bool m_is_composite[val_limit];INT m_val[LE_max];int m_LE;CE PrimeEnumeration();CE CO INT& OP[](CRI n) CO;CE CO INT& Get(CRI n) CO;CE CO bool& IsComposite(CRI i) CO;CE CRI LE() CO NE;}; TE CE PrimeEnumeration::PrimeEnumeration():m_is_composite(),m_val(),m_LE(0){for(INT i = 2;i < val_limit;i++){if(! m_is_composite[i]){INT j = i;WH((j += i)< val_limit){m_is_composite[j] = true;}m_val[m_LE++] = i;if(m_LE >= LE_max){break;}}}}TE CE CO INT& PrimeEnumeration::OP[](CRI n)CO{assert(n < m_LE);RE m_val[n];}TE CE CO INT& PrimeEnumeration::Get(CRI n)CO{RE OP[](n);}TE CE CO bool& PrimeEnumeration::IsComposite(CRI i)CO{assert(i < val_limit);RE m_is_composite[i];}TE CE CRI PrimeEnumeration::LE()CO NE{RE m_LE;} TE VO SetPrimeFactorisation(CO PrimeEnumeration& prime,CO INT1& n,VE& P,VE& EX){INT1 n_copy = n;int i = 0;WH(i < prime.m_LE){CO INT2& p = prime[i];if(p * p > n_copy){break;}if(n_copy % p == 0){P.push_back(p);EX.push_back(1);INT3& EX_back = EX.back();n_copy /= p;WH(n_copy % p == 0){EX_back++;n_copy /= p;}}i++;}if(n_copy != 1){P.push_back(n_copy);EX.push_back(1);}RE;} #define DIJKSTRA_BODY( SET_FOUND , SET_WEIGHT , UPDATE_FOUND , CHECK_FOUND , INITIALISE_PREV , SET_PREV ) \ static const U& unit = Unit(); \ assert( unit != m_found && unit < m_infty ); \ const int i_start = e_inv( t_start ); \ set > vertex{}; \ SET_FOUND; \ SET_WEIGHT; \ vertex.insert( pair( weight[i_start] = unit , i_start ) ); \ INITIALISE_PREV; \ \ if( i_start != i_final ){ \ \ while( ! vertex.empty() ){ \ \ auto itr_vertex = vertex.begin(); \ const pair v = *itr_vertex; \ const int& i = v.second; \ \ if( i == i_final ){ \ \ break; \ \ } \ \ const U& u = v.first; \ UPDATE_FOUND; \ vertex.erase( itr_vertex ); \ const list > edge_i = m_edge( e( i ) ); \ list > changed_vertex{}; \ \ for( auto itr_edge_i = edge_i.begin() , end_edge_i = edge_i.end() ; itr_edge_i != end_edge_i ; itr_edge_i++ ){ \ \ const int& j = e_inv( itr_edge_i->first ); \ U& weight_j = weight[j]; \ \ if( CHECK_FOUND ){ \ \ const U& edge_ij = itr_edge_i->second; \ const U temp = Addition( u , edge_ij ); \ assert( edge_ij != m_found && temp != m_found && !( temp < edge_ij ) && temp < m_infty ); \ \ if( weight_j > temp ){ \ \ if( weight_j != m_infty ){ \ \ vertex.erase( pair( weight_j , j ) ); \ \ } \ \ SET_PREV; \ changed_vertex.push_back( pair( weight_j = temp , j ) ); \ \ } \ \ } \ \ } \ \ for( auto itr_changed = changed_vertex.begin() , end_changed = changed_vertex.end() ; itr_changed != end_changed ; itr_changed++ ){ \ \ vertex.insert( *itr_changed ); \ \ } \ \ } \ \ } \ // Eは写像edge:T->(T \times U)^{< \omega}に相当する型。 // メモリが不足する場合はEの定義を前計算しないでその都度計算させること。 // O((size+|edge|)log size)が間に合わない場合は、 // 始点からの距離を格納して一番近い未訪問点を全探策で探し距離を更新するO(size^2)版を検討。 template class Dijkstra_Body { private: int m_size; U m_infty; U m_found; E m_edge; public: inline Dijkstra_Body( const int& size , const U& infty , const U& found , E edge ); // 経路が存在しない場合の返り値はm_infty U Solve( const T& t_start , const T& t_final ); U Solve( const T& t_start , const T& t_final , list& path ); void Solve( const T& t_start , vector& weight ); void Solve( const T& t_start , vector& weight , vector>& path ); const U& Infty() const; private: virtual const U& Unit() const = 0; virtual U Addition( const U& , const U& ) const = 0; virtual T e( const int& i ) = 0; virtual int e_inv( const T& t ) = 0; virtual void Reset(); }; // 入力の範囲内で要件 // (1) edgeの値の各成分の第2成分が0以上である。 // (2) 2^{31}-1がEの値の各成分の第2成分size個以下の和で表せるいかなる数よりも大きい。 // (6) Vの各要素u,vに対し、辺u->vが複数存在する場合は重みが最小のものが前にpushされている。 // が成り立つ場合にのみサポート。 // 単一始点単一終点最短経路探索/経路復元なしO((size+|edge|)log size) // 単一始点単一終点最短経路探索/経路復元ありO((size+|edge|)log size) // 単一始点全終点最短経路探索/経路復元なしO((size+|edge|)log size) // 単一始点全終点最短経路探索/経路復元ありO(size^2 + |edge| log size) template class Dijkstra : public Dijkstra_Body { public: inline Dijkstra( const int& size , E edge ); private: inline const ll& Unit() const; inline ll Addition( const ll& , const ll& ) const; inline int e( const int& i ); inline int e_inv( const int& t ); }; template class MonoidForDijkstra : public Dijkstra_Body { private: M_U m_m_U; E_U m_e_U; public: inline MonoidForDijkstra( const int& size , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ); private: inline const U& Unit() const; inline U Addition( const U& , const U& ) const; }; // 入力の範囲内で要件 // (1) edgeの値の各成分の第2成分がe_U()以上である。 // (2) inftyがEの値の各成分の第2成分size個以下の和で表せるいかなる項よりも大きい。 // (3) foundがEの値の各成分の第2成分size個以下の和で表せず、inftyとも異なる。 // (4) (U,m_U:U^2->U,e_U:1->U)がbool operator<(const U&,const U&)に関して全順序モノイドである。 // (6) Vの各要素u,vに対し、辺u->vが複数存在する場合は重みが最小のものが前にpushされている。 // (7) edgeはデフォルト引数による呼び出し可能(推論補助に用いる) // が成り立つ場合にのみサポート。 // 単一始点単一終点最短経路探索/経路復元なしO((size+|edge|)(log size)^2) // 単一始点単一終点最短経路探索/経路復元ありO((size+|edge|)(log size)^2) // 単一始点全終点最短経路探索/経路復元なしO((size+|edge|)(log size)^2) // 単一始点全終点最短経路探索/経路復元ありO(size^2 log size + |edge|(log size)^2) template class MemorisationDijkstra : public MonoidForDijkstra { private: int m_length; Map m_memory; vector m_memory_inv; public: inline MemorisationDijkstra( const int& size , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ); private: inline T e( const int& i ); inline int e_inv( const T& t ); inline void Reset(); }; template MemorisationDijkstra( const int& , M_U , E_U , const U& , const U& , E ) -> MemorisationDijkstra().first),U,M_U,E_U,E>; // 入力の範囲内で要件 // (1) edgeの値の各成分の第2成分がe_U()以上である。 // (2) inftyがEの値の各成分の第2成分size個以下の和で表せるいかなる項よりも大きい。 // (3) foundがEの値の各成分の第2成分size個以下の和で表せず、inftyとも異なる。 // (4) (U,m_U:U^2->U,e_U:1->U)がbool operator<(const U&,const U&)に関して全順序モノイドである。 // (5) (enum_T,enum_T_inv)が互いに逆写像である。 // (6) Vの各要素u,vに対し、辺u->vが複数存在する場合は重みが最小のものが前にpushされている。 // が成り立つ場合にのみサポート。 // 単一始点単一終点最短経路探索/経路復元なしO((size+|edge|)log size) // 単一始点単一終点最短経路探索/経路復元ありO((size+|edge|)log size) // 単一始点全終点最短経路探索/経路復元なしO((size+|edge|)log size) // 単一始点全終点最短経路探索/経路復元ありO(size^2 + |edge| log size) template class EnumerationDijkstra : public MonoidForDijkstra { private: Enum_T m_enum_T; Enum_T_inv m_enum_T_inv; public: inline EnumerationDijkstra( const int& size , Enum_T enum_T , Enum_T_inv enum_T_inv , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ); private: inline T e( const int& i ); inline int e_inv( const T& t ); }; template EnumerationDijkstra( const int& , Enum_T , Enum_T_inv , M_U , E_U , const U& , const U& , E ) -> EnumerationDijkstra()(0)),Enum_T,Enum_T_inv,U,M_U,E_U,E>; template inline Dijkstra_Body::Dijkstra_Body( const int& size , const U& infty , const U& found , E edge ) : m_size( size ) , m_infty( infty ) , m_found( found ) , m_edge( move( edge ) ) { static_assert( ! is_same_v && is_invocable_r_v>,E,T> ); } template inline Dijkstra::Dijkstra( const int& size , E edge ) : Dijkstra_Body( size , 9223372036854775807 , -1 , move( edge ) ) {} template inline MonoidForDijkstra::MonoidForDijkstra( const int& size , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ) : Dijkstra_Body( size , infty , found , move( edge ) ) , m_m_U( move( m_U ) ) , m_e_U( move( e_U ) ) {} template inline MemorisationDijkstra::MemorisationDijkstra( const int& size , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ) : MonoidForDijkstra( size , move( m_U ) , move( e_U ) , infty , found , move( edge ) ) , m_length() , m_memory() , m_memory_inv() {} template inline EnumerationDijkstra::EnumerationDijkstra( const int& size , Enum_T enum_T , Enum_T_inv enum_T_inv , M_U m_U , E_U e_U , const U& infty , const U& found , E edge ) : MonoidForDijkstra( size , move( m_U ) , move( e_U ) , infty , found , move( edge ) ) , m_enum_T( move( enum_T ) ) , m_enum_T_inv( move( enum_T_inv ) ) {} template U Dijkstra_Body::Solve( const T& t_start , const T& t_final ) { const int i_final = e_inv( t_final ); DIJKSTRA_BODY( , vector weight( m_size , m_infty ) , weight[i] = m_found , weight_j != m_found , , ); Reset(); return weight[i_final]; } template U Dijkstra_Body::Solve( const T& t_start , const T& t_final , list& path ) { const int i_final = e_inv( t_final ); \ DIJKSTRA_BODY( , vector weight( m_size , m_infty ) , weight[i] = m_found , weight_j != m_found , vector prev( m_size ) , prev[j] = i ); int i = i_final; while( i != i_start ){ path.push_front( e( i ) ); i = prev[i]; } path.push_front( t_start ); Reset(); return weight[i_final]; } template void Dijkstra_Body::Solve( const T& t_start , vector& weight ) { constexpr const int i_final = -1; DIJKSTRA_BODY( vector found( m_size ) , weight = vector( m_size , m_infty ) , found[i] = true , !found[j] , , ); Reset(); return; } template void Dijkstra_Body::Solve( const T& t_start , vector& weight , vector>& path ) { constexpr const int i_final = -1; DIJKSTRA_BODY( vector found( m_size ) , weight = vector( m_size , m_infty ) , found[i] = true , !found[j] , vector prev( m_size ) , prev[j] = i ); for( int j = 0 ; j < m_size ; j++ ){ list& path_j = path[j]; int i = j; while( i != i_start ){ path_j.push_front( e( i ) ); i = prev[i]; } path_j.push_front( t_start ); } Reset(); return; } template const U& Dijkstra_Body::Infty() const { return m_infty; } template inline const ll& Dijkstra::Unit() const { static const ll unit = 0; return unit; } template inline const U& MonoidForDijkstra::Unit() const { return m_e_U(); } template inline ll Dijkstra::Addition( const ll& u0 , const ll& u1 ) const { return u0 + u1; } template inline U MonoidForDijkstra::Addition( const U& u0 , const U& u1 ) const { return m_m_U( u0 , u1 ); } template inline int Dijkstra::e( const int& i ) { return i; } template inline T MemorisationDijkstra::e( const int& i ) { assert( i < m_length ); return m_memory_inv[i]; } template inline T EnumerationDijkstra::e( const int& i ) { return m_enum_T( i ); } template inline int Dijkstra::e_inv( const int& t ) { return t; } template inline int MemorisationDijkstra::e_inv( const T& t ) { using base = Dijkstra_Body; if( m_memory.count( t ) == 0 ){ assert( m_length < base::m_size ); m_memory_inv.push_back( t ); return m_memory[t] = m_length++; } return m_memory[t]; } template inline int EnumerationDijkstra::e_inv( const T& t ) { return m_enum_T_inv( t ); } template inline void Dijkstra_Body::Reset() {} template inline void MemorisationDijkstra::Reset() { m_length = 0; m_memory.clear(); m_memory_inv.clear(); } // AAA 常設でないライブラリは以上に挿入する。 #define INCLUDE_SUB #include __FILE__ #else // INCLUDE_LIBRARY #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 typename V> inline V operator+( const V& a0 , const V& a1 ) { if( a0.empty() ){ return a1; } if( a1.empty() ){ return a0; } assert( a0.size() == a1.size() ); V answer{}; for( auto itr0 = a0.begin() , itr1 = a1.begin() , end0 = a0.end(); itr0 != end0 ; itr0++ , itr1++ ){ answer.push_back( *itr0 + *itr1 ); } return answer; } template inline pair operator+( const pair& t0 , const pair& t1 ) { return { t0.first + t1.first , t0.second + t1.second }; } template inline tuple operator+( const tuple& t0 , const tuple& t1 ) { return { get<0>( t0 ) + get<0>( t1 ) , get<1>( t0 ) + get<1>( t1 ) , get<2>( t0 ) + get<2>( t1 ) }; } template inline tuple operator+( const tuple& t0 , const tuple& t1 ) { return { get<0>( t0 ) + get<0>( t1 ) , get<1>( t0 ) + get<1>( t1 ) , get<2>( t0 ) + get<2>( t1 ) , get<3>( t0 ) + get<3>( t1 ) }; } 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{}; 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);}} // デバッグ用 #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 常設ライブラリは以下に挿入する。 // Map // c:/Users/user/Documents/Programming/Mathematics/Function/Map CL is_ordered{PU:is_ordered()= delete;TE ST CE auto Check(CO T& t)-> decltype(t < t,true_type());ST CE false_type Check(...);TE ST CE CO bool value = is_same_v< decltype(Check(declval())),true_type >;}; TE US Map = conditional_t>,unordered_map,conditional_t,map,void>>; // 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_istream& OP>>(basic_istream& is,Mod& n){ll m;is >> m;n = m;RE is;}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