#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 AUTO_CHECK bool auto_checked = true; AutoCheck( auto_checked ); if( auto_checked ){ return 0; }; #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 AUTO_CHECK #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 SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ) #define QUIT goto END_MAIN #define TEST_CASE_NUM( BOUND ) DEXPR( int , bound_T , BOUND , min( BOUND , 100 ) ); int T = 1; if constexpr( bound_T > 1 ){ SET_ASSERT( T , 1 , bound_T ); } #define START_MAIN REPEAT( T ){ if constexpr( bound_T > 1 ){ CERR( "testcase " << VARIABLE_FOR_REPEAT_T << ":" ); } #define FINISH_MAIN QUIT; } END_MAIN: CERR( "" ); #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( bool& auto_checked ); void StartWatch( const string& process_name = "nothing" ); void StopWatch( const int& how_many_times = 1 ); #endif #if defined( DEBUG ) && defined( RANDOM_TEST ) 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 ); goto END_MAIN; } else { CERR( ( ANSWER ) << " != " << guchoku ); QUIT; } #else #define SET_ASSERT( A , MIN , MAX ) cin >> A; ASSERT( A , MIN , MAX ) #define RETURN( ANSWER ) COUT( ( ANSWER ) ); QUIT #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 ); } inline ll MIN( const ll& a , const ll& b ){ return min( a , b ); } inline ull MIN( const ull& a , const ull& b ){ return min( a , b ); } inline ll MAX( const ll& a , const ll& b ){ return max( a , b ); } inline ull MAX( const ull& a , const ull& b ){ return max( a , b ); } #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 ) \ // 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 const T& zero() { static const T z = 0; return z; } template inline T add_inv( const T& t ) { return -t; } 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; } template inline T id( const T& v ) { return v; } // グリッド問題用関数 int H , W , H_minus , W_minus , HW; inline pair 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;} // 圧縮用 #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& // 入力の範囲内で要件 // (1) (T,m_T:T^2->T,i_T:T->T)が群である。 // が成り立つ場合のみサポート。(単位元はテンプレート引数に渡さなくてよい) template class CumulativeProd_Body { protected: int m_size; T m_a[size_max]; T m_a_reverse[size_max]; public: inline CumulativeProd_Body( const int& size ); // 0 <= i,j < m_sizeの場合のみサポート。 // iからへのpathがi=v_0->...->v_k=jの時m_a[v_0]...m_a[v_k]を // Prodや逆順のProdに関して計算する。 inline T PathProd( const int& i , const int& j ); private: virtual int Parent( const int& i ) = 0; virtual int LCA( const int& i , const int& j ) = 0; }; // 通常の配列上の累積積。 // テンプレート引数に単位元e_T:1->Tも渡す。 // 配列による初期化O(size) // 右区間積取得O(1) // 左区間積取得O(1) // 区間積が単位元である区間の数え上げO(size log size) // 区間積が単位元である区間の列挙O(size log size + 区間数) template class CumulativeProd : public CumulativeProd_Body { public: inline CumulativeProd( const T ( &a )[size_max] , const int& size ); // 0 <= iかつi-1 <= j < m_sizeの場合のみサポート。 // m_a[i]...m_a[j]をm_Tに関してで計算する。 inline T RightProd( const int& i , const int& j ); // m_a[j]...m_a[i]をm_Tに関して計算する。 inline T LeftProd( const int& i , const int& j ); // 区間積がe_T()と等しい区間の個数を計算する。 ll CountUnitProdRange(); // 区間積がe_T()と等しい区間を列挙する。 list > UnitProdRange(); private: inline int Parent( const int& i ); inline int LCA( const int& i , const int& j ); }; template inline CumulativeProd_Body::CumulativeProd_Body( const int& size ) : m_size( size ) , m_a() , m_a_reverse() { assert( size <= size_max ); } template inline CumulativeProd::CumulativeProd( const T ( &a )[size_max] , const int& size ) : CumulativeProd_Body( size ) { using base = CumulativeProd_Body; T temp , temp_reverse; base::m_a[0] = base::m_a_reverse[0] = temp = temp_reverse = a[0]; for( int i = 1 ; i < size ; i++ ){ base::m_a[i] = temp = m_T( temp , a[i] ); base::m_a_reverse[i] = temp_reverse = m_T( a[i] , temp_reverse ); } } template inline T CumulativeProd_Body::PathProd( const int& i , const int& j ) { assert( 0 <= i && i < m_size && 0 <= j && j < m_size ); const int k = LCA( i , j ); return m_T( m_T( m_a_reverse[i] , i_T( m_a_reverse[k] ) ) , k == 0 ? m_a[j] : m_T( i_T( m_a[Parent( k ) ] ) , m_a[j] )); } template inline T CumulativeProd::RightProd( const int& i , const int& j ) { assert( i - 1 <= j ); using base = CumulativeProd_Body; return i <= j ? i == 0 ? base::m_a[j] : m_T( i_T( base::m_a[i-1] ) , base::m_a[j] ) : e_T(); } template inline T CumulativeProd::LeftProd( const int& i , const int& j ) { assert( i - 1 <= j ); using base = CumulativeProd_Body; return i <= j ? i == 0 ? base::m_a_reverse[j] : m_T( base::m_a_reverse[j] , i_T( base::m_a_reverse[i - 1] ) ) : e_T(); } template ll CumulativeProd::CountUnitProdRange() { using base = CumulativeProd_Body; map f{}; f[e_T()]++; for( int i = 0 ; i < base::m_size ; i++ ){ f[base::m_a[i]]++; } ll answer = 0; for( auto itr_f = f.begin() , end_f = f.end() ; itr_f != end_f ; itr_f++ ){ const ll& num = itr_f->second; answer += num * ( num - 1 ) / 2; } return answer; } template list > CumulativeProd::UnitProdRange() { using base = CumulativeProd_Body; map > f{}; f[e_T()].push_back( -1 ); for( int i = 0 ; i < base::m_size ; i++ ){ f[base::m_a[i]].push_back( i ); } list > answer{}; for( auto itr_f = f.begin() , end_f = f.end() ; itr_f != end_f ; itr_f++ ){ const auto& a = itr_f->second; for( auto itr_a_L = a.begin() , end_a = a.end() ; itr_a_L != end_a ; itr_a_L++ ){ const int i = *itr_a_L + 1; auto itr_a_R = itr_a_R; itr_a_R++; while( itr_a_R != end_a ){ answer.push_back( i , *itr_a_R ); } } } return answer; } template inline int CumulativeProd::Parent( const int& i ) { return i - 1; } template inline int CumulativeProd::LCA( const int& i , const int& j ) { return min( i , j ); } template class ZobristHashBody { protected: ull m_hash; public: inline ZobristHashBody( const ull& hash ); ull Encode( const set& S ); inline ull Encode( const list& S , const bool& non_overlapping = false ); template inline ull Encode( const T ( &a )[length_max] , const int& length , const bool& non_overlapping = false ); inline ull SymmetricDifference( const ull& code0 , const ull& code1 ); inline ull Add( set& S , const ull& code , const T& t ); inline ull Erase( set& S , const ull& code , const T& t ); inline ull AddErase( const ull& code , const T& t ); private: ull OverlappingEncode( const list& S ); template ull OverlappingEncode( const T ( &a )[length_max] , const int& length ); ull NonOverlappingEncode( const list& S ); template ull NonOverlappingEncode( const T ( &a )[length_max] , const int& length ); virtual ull Hash( const T& t ) = 0; }; // 集合のコードO(要素数) // リストの像のコードO(要素数 log 要素数)(無重複保証畤はO(要素数)) // 配列の像のコードO(要素数 log 要素数)(無重複保証畤はO(要素数)) // 集合の対称差O(1) // 要素追加O(log要素数) // 要素削除O(log要素数) // 要素がある場合は削除、ない場合は追加O(1) class ZobristHash : public ZobristHashBody { public: inline ZobristHash( const ull& hash = 14177381365537266759ULL ); private: inline ull Hash( const ull& t ); }; // 集合のコードO(要素数 log_size) // リストの像のコードO(要素数(log 要素数)(log size))(無重複保証畤はO(要素数 log size)) // 配列の像のコードO(要素数(log 要素数)(log size))(無重複保証畤はO(要素数 log size)) // 集合の対称差O(1) // 要素追加O((log要素数)(log size)) // 要素削除O((log要素数)(log size)) // 要素がある場合は削除、ない場合は追加O(log size) template class MemorisationZobristHash : public ZobristHashBody { private: map m_f; public: inline MemorisationZobristHash( const ull& hash = 14177381365537266759ULL ); private: inline ull Hash( const T& t ); }; // 集合のコードO(要素数) // リストの像のコードO(要素数 log 要素数)(無重複保証畤はO(要素数)) // 配列の像のコードO(要素数 log 要素数)(無重複保証畤はO(要素数)) // 集合の対称差O(1) // 要素追加O(log要素数) // 要素削除O(log要素数) // 要素がある場合は削除、ない場合は追加O(1) template class EnumerationZobristHash : public ZobristHashBody { public: inline EnumerationZobristHash( const ull& hash = 14177381365537266759ULL ); private: inline ull Hash( const T& t ); }; template inline ZobristHashBody::ZobristHashBody( const ull& hash ) : m_hash( hash ) {} inline ZobristHash::ZobristHash( const ull& hash ) : ZobristHashBody( hash ) {} template inline MemorisationZobristHash::MemorisationZobristHash( const ull& hash ) : ZobristHashBody( hash ) {} template inline EnumerationZobristHash::EnumerationZobristHash( const ull& hash ) : ZobristHashBody( hash ) {} template ull ZobristHashBody::Encode( const set& S ) { ull answer = 0; for( auto itr = S.begin() , end = S.end() ; itr != end ; itr++ ){ answer ^= Hash( *itr ); } return answer; } template inline ull ZobristHashBody::Encode( const list& S , const bool& non_overlapping ) { return non_overlapping ? NonOverlappingEncode( S ) : OverlappingEncode( S ); } template template inline ull ZobristHashBody::Encode( const T ( &a )[length_max] , const int& length , const bool& non_overlapping ) { return non_overlapping ? NonOverlappingEncode( a , length ) : OverlappingEncode( a , length ); } template ull ZobristHashBody::OverlappingEncode( const list& S ) { set S_set{}; for( auto itr = S.begin() , end = S.end() ; itr != end ; itr++ ){ S_set.insert( *itr ); } return Encode( S_set ); } template template ull ZobristHashBody::OverlappingEncode( const T ( &a )[length_max] , const int& length ) { set S_set{}; for( int i = 0 ; i < length ; i++ ){ S_set.insert( a[i] ); } return Encode( S_set ); } template ull ZobristHashBody::NonOverlappingEncode( const list& S ) { ull answer = 0; for( auto itr = S.begin() , end = S.end() ; itr != end ; itr++ ){ answer ^= Hash( *itr ); } return answer; } template template ull ZobristHashBody::NonOverlappingEncode( const T ( &a )[length_max] , const int& length ) { ull answer = 0; for( int i = 0 ; i < length ; i++ ){ answer ^= Hash( a[i] ); } return answer; } template inline ull ZobristHashBody::SymmetricDifference( const ull& code0 , const ull& code1 ) { return code0 ^ code1; } template inline ull ZobristHashBody::Add( set& S , const ull& code , const T& t ) { return S.count( t ) == 0 ? ( S.insert( t ) , code ^ Hash( t ) ) : code; } template inline ull ZobristHashBody::Erase( set& S , const ull& code , const T& t ) { return S.count( t ) == 0 ? code : ( S.erase( t ) , code ^ Hash( t ) ); } template inline ull ZobristHashBody::AddErase( const ull& code , const T& t ){ return code ^ Hash( t ); } inline ull ZobristHash::Hash( const ull& t ) { return t * ZobristHashBody::m_hash; } template inline ull MemorisationZobristHash::Hash( const T& t ) { if( m_f.count( t ) == 0 ){ const ull size = m_f.size() + 1; return m_f[t] = size * ZobristHashBody::m_hash; } return m_f[t]; } template inline ull EnumerationZobristHash::Hash( const T& t ) { return enum_T_inv( t ) * ZobristHashBody::m_hash; } TE CL PrimeEnumeration{PU:INT m_val[LE_max];int m_LE;CE PrimeEnumeration();};TE CE PrimeEnumeration::PrimeEnumeration():m_val(),m_LE(0){bool is_comp[val_limit] ={};for(INT i = 2;i < val_limit;i++){if(is_comp[i] == false){INT j = i;WH((j += i) < val_limit){is_comp[j] = true;}m_val[m_LE++] = i;if(m_LE >= LE_max){break;}}}}TE VO SetPrimeFactorisation(CO PrimeEnumeration& prime,CO INT& n,VE& P,VE& EX){INT n_copy = n;int i = 0;WH(i < prime.m_LE){CO INT& p = prime.m_val[i];if(p * p > n_copy){break;}if(n_copy % p == 0){P.push_back(p);EX.push_back(1);INT& 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;} // データ構造使用畤のNの上限 inline DEXPR( int , bound_N , 200000 , 100 ); // 0が5個 inline ull xor_add( const ull& t0 , const ull& t1 ){ return t0 ^ t1; } int main() { UNTIE; AUTO_CHECK; TEST_CASE_NUM( 1 ); START_MAIN; constexpr PrimeEnumeration pe{}; // ZobristHash zh{}; MemorisationZobristHash zh{}; CIN( int , N ); // ll A[N]; ull code_A[bound_N]; // 関数(コンストラクタ)の引数に使う。 FOR( i , 0 , N ){ CIN( int , Ai ); vector P; vector exponent; SetPrimeFactorisation( pe , Ai , P , exponent ); int size = P.size(); ull& code_Ai = code_A[i] = 0; FOR( i , 0 , size ){ code_Ai = exponent[i] % 2 == 0 ? code_Ai : zh.AddErase( code_Ai , P[i] ); } } CumulativeProd cp{ code_A , N }; RETURN( cp.CountUnitProdRange() ); FINISH_MAIN; }