// 入力制約/フォーマットチェック #ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE #define USE_GETLINE #endif #ifdef INCLUDE_MAIN void Solve() { static vector> Im{}; if( Im.empty() ){ Im.resize( 40 ); FOR( e , 2 , 40 ){ FOREQ( n , 1 , ll(1e6) ){ ll t = TruncatedPower( n , e , ll(1e12)+1 ); if( t > ll(1e12) ){ break; } Im[e] <<= t; } } } CEXPR( ll , bound_N , 1e12 ); GETLINE_COUNT_ASSERT( N_str , ' ' , 1 ); STOI( N_str , N , 1 , bound_N ); FOREQINV( e , 39 , 2 ){ if( In( Im[e] , N ) ){ RETURN( e ); } } RETURN( 1 ); } REPEAT_MAIN(1e5); #else // INCLUDE_MAIN #ifdef INCLUDE_LIBRARY // https://github.com/p-adic/cpp // VVV ライブラリは以下に挿入する。 /* 圧縮用 */ #define TE template #define TY typename #define US using #define ST static #define AS assert #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 LE length #define PW Power #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& #define VI virtual #define IS basic_istream #define OS basic_ostream #define ST_AS static_assert #define reMO_CO remove_const #define is_COructible_v is_constructible_v #define rBE rbegin // CEXPRがCEに依存しているので削除しない。 // redefinitionを避けるため圧縮元はincludeしない。 template ll TruncatedPower( ll n , INT1 e , const INT2& bound ) { assert( n >= 0 && e >= 0 && bound >= 0 ); if( e == 0 ){ return 1; } if( n <= 1 ){ return move( n ); } if( e >= 63 ){ return bound + 1; } ll answer = ( e & 1 ) == 0 ? 1 : n; e >>= 1; while( e > 0 ){ if( n <= bound / n ){ n *= n; } else { return bound + 1; } if( ( e & 1 ) == 1 ){ if( answer <= bound / n ){ answer *= n; } else { return bound + 1; } } e >>= 1; } return answer; } // AAA ライブラリは以上に挿入する。 #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_LIBRARY #ifdef DEBUG #define _GLIBCXX_DEBUG #define SIGNAL signal( SIGABRT , &AlertAbort ); #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE2 ) #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define CERRNS( ... ) VariadicCoutNonSep( cerr , __VA_ARGS__ ) #define CERR_A( I , N , A ) CoutArray( cerr , I , N , A ) << endl int exec_mode = 0; #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define SIGNAL #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #define ASSERT( A , MIN , MAX ) AS( ( MIN ) <= A && A <= ( MAX ) ) #define CERR( ... ) #define CERRNS( ... ) #define CERR_A( I , N , A ) #endif #ifdef REACTIVE #ifdef DEBUG #define RSET( A , ... ) A = __VA_ARGS__ #else #define RSET( A , ... ) cin >> A #endif #define RCIN( LL , A , ... ) LL A; RSET( A , __VA_ARGS__ ) #define ENDL endl #else #define ENDL "\n" #endif #ifdef USE_GETLINE #define SET_SEPARATE( SEPARATOR , ... ) VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define SET( ... ) SET_SEPARATE( '\n' , __VA_ARGS__ ) #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; SET_SEPARATE( SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #define FINISH_MAIN GETLINE( test_case_num_str ); test_case_num = stoi( test_case_num_str ); ASSERT( test_case_num , 1 , test_case_num_bound ); } REPEAT( test_case_num ){ Solve(); } CHECK_REDUNDANT_INPUT; } #else #define SET( ... ) VariadicCin( cin , __VA_ARGS__ ) #define CIN( LL , ... ) LL __VA_ARGS__; SET( __VA_ARGS__ ) #define SET_A( I , N , ... ) VariadicResize( N + I , __VA_ARGS__ ); FOR( VARIABLE_FOR_SET_A , 0 , N ){ VariadicSet( cin , VARIABLE_FOR_SET_A + I , __VA_ARGS__ ); } #define CIN_A( LL , I , N , ... ) VE __VA_ARGS__; SET_A( I , N , __VA_ARGS__ ) #define CIN_AA( LL , I0 , N0 , I1 , N1 , VAR ) VE> VAR( N0 + I0 ); FOR( VARIABLE_FOR_CIN_AA , 0 , N0 ){ SET_A( I1 , N1 , VAR[VARIABLE_FOR_CIN_AA + I0] ); } #define FINISH_MAIN SET_ASSERT( test_case_num , 1 , test_case_num_bound ); } REPEAT( test_case_num ){ Solve(); } CHECK_REDUNDANT_INPUT; } #endif #include using namespace std; #define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; #define REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , test_case_num_bound , BOUND ); int test_case_num = 1; if constexpr( test_case_num_bound > 1 ){ CERR( "テストケースの個数を入力してください。" ); FINISH_MAIN; #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now(); double loop_average_time = 0.0 , loop_start_time = loop_average_time , current_time = loop_start_time; int loop_count = current_time; assert( loop_count == 0 ) #define CURRENT_TIME ( current_time = static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME , loop_count == 0 ? loop_start_time = current_time : loop_average_time = ( current_time - loop_start_time ) / loop_count , ++loop_count , current_time < TL_MS - loop_average_time * 2 - 100.0 ) #define CEXPR( LL , BOUND , VALUE ) CE LL BOUND = VALUE #define SET_ASSERT( A , MIN , MAX ) SET( A ); ASSERT( A , MIN , MAX ) #define SET_A_ASSERT( I , N , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A + I] , MIN , MAX ); } #define SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_AA0 , 0 , N0 ){ FOR( VARIABLE_FOR_SET_AA1 , 0 , N1 ){ SET_ASSERT( A[VARIABLE_FOR_SET_AA0 + I0][VARIABLE_FOR_SET_AA1 + I1] , MIN , MAX ); } } #define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define CIN_A_ASSERT( I , N , A , MIN , MAX ) vector A( N + I ); SET_A_ASSERT( I , N , A , MIN , MAX ) #define CIN_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) vector A( N0 + I0 , vector( N1 + I1 ) ); SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) #define PR1( A1 , ... ) A1 #define PR2( A1 , A2 , ... ) A2 #define PR3( A1 , A2 , A3 , ... ) A3 #define FOR_( VAR , INITIAL , FINAL , UPPER , COMP , INCR ) for( decldecay_t( UPPER ) VAR = INITIAL ; VAR COMP FINAL ; VAR INCR ) #define FOR( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , PR1( __VA_ARGS__ ) , < , PR3( __VA_ARGS__ , += PR2( __VA_ARGS__ , ? ) , ++ ) ) #define FOREQ( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , PR1( __VA_ARGS__ ) , <= , PR3( __VA_ARGS__ , += PR2( __VA_ARGS__ , ? ) , ++ ) ) #define FOREQINV( VAR , INITIAL , ... ) FOR_( VAR , INITIAL , PR1( __VA_ARGS__ ) , INITIAL , + 1 > , PR3( __VA_ARGS__ , -= PR2( __VA_ARGS__ , ? ) , -- ) ) #define ITR( ARRAY ) auto begin_ ## ARRAY = ARRAY .BE() , itr_ ## ARRAY = begin_ ## ARRAY , end_ ## ARRAY = ARRAY .EN() #define FOR_ITR( ARRAY ) for( ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define RUN( ARRAY , ... ) for( auto&& __VA_ARGS__ : ARRAY ) #define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) #define SET_PRECISION( DECIMAL_DIGITS ) cout << fixed << setprecision( DECIMAL_DIGITS ); cerr << fixed << setprecision( DECIMAL_DIGITS ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) #define COUT_A( I , N , A ) CoutArray( cout , I , N , A ) << ENDL #define RETURN( ... ) COUT( __VA_ARGS__ ); return // 型のエイリアス #define decldecay_t( VAR ) decay_t template using ret_t = decltype( declval()( declval()... ) ); template using inner_t = typename T::type; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; using path = pair; /* VVV 常設ライブラリの非圧縮版は以下に挿入する。*/ // BinarySearch // 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 ANSWER ## _L = MINIMUM; \ ll ANSWER ## _R = MAXIMUM; \ ANSWER = UPDATE_ANSWER; \ ll EXPRESSION_BS; \ const ll CONST_TARGET_BS = ( CONST_TARGET ); \ ll DIFFERENCE_BS; \ while( ANSWER ## _L < ANSWER ## _R ){ \ DIFFERENCE_BS = ( EXPRESSION_BS = ( EXPRESSION ) ) - CONST_TARGET_BS; \ CERR( "二分探索中:" , string{ #ANSWER } + "_L" , "=" , ANSWER ## _L , "<=" , #ANSWER , "=" , ANSWER , "<=" , ANSWER ## _R , "=" , string{ #ANSWER } + "_R" , ":" , #EXPRESSION , "=" , EXPRESSION_BS , DIFFERENCE_BS > 0 ? ">" : DIFFERENCE_BS < 0 ? "<" : "=" , CONST_TARGET_BS , "=" , #CONST_TARGET ); \ if( DIFFERENCE_BS INEQUALITY_FOR_CHECK 0 ){ \ ANSWER ## _R = UPDATE_U; \ } else { \ ANSWER ## _L = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ if( ANSWER ## _L > ANSWER ## _R ){ \ CERR( "二分探索失敗:" , string{ #ANSWER } + "_L" , "=" , ANSWER ## _L , ">" , ANSWER ## _R , "=" , string{ #ANSWER } + "_R" , ":" , #ANSWER , ":=" , #MAXIMUM , "+ 1 =" , MAXIMUM + 1 ); \ CERR( "二分探索マクロにミスがある可能性があります。変更前の版に戻してください。" ); \ ANSWER = MAXIMUM + 1; \ } else { \ CERR( "二分探索終了:" , string{ #ANSWER } + "_L" , "=" , ANSWER ## _L , "<=" , #ANSWER , "=" , ANSWER , "<=" , ANSWER ## _R , "=" , string{ #ANSWER } + "_R" ); \ 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 MIN_GEQ( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CONST_TARGET , >= , ANSWER , ANSWER + 1 , Mid( ANSWER ## _L , ANSWER ## _R ) ) // 単調増加の時にEXPRESSION <= CONST_TARGETの最大解を格納。 #define MAX_LEQ( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CONST_TARGET , > , ANSWER - 1 , ANSWER , Mid( ANSWER ## _L + 1 , ANSWER ## _R ) ) // 単調減少の時にEXPRESSION >= CONST_TARGETの最大解を格納。 #define MAX_GEQ( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CONST_TARGET , < , ANSWER - 1 , ANSWER , Mid( ANSWER ## _L + 1 , ANSWER ## _R ) ) // 単調減少の時にEXPRESSION <= CONST_TARGETの最小解を格納。 #define MIN_LEQ( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CONST_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CONST_TARGET , <= , ANSWER , ANSWER + 1 , Mid( ANSWER ## _L , ANSWER ## _R ) ) template inline constexpr INT Mid( const INT& l , const INT& r ) { return l + ( ( r - l ) >> 1 ); } // TwoPoitnterApproach // VAR_TPAは尺取り法用の変数名の接頭辞で、実際の変数名ではなく、_Lと_Rと_infoがつく。 // ANSWER ## _temp = {VAR_TPA ## _L,VAR_TPA ## _R,VPA_TPA ## _info}を // {INIT,INIT,INFO_init}で初期化する。VPA_TPA ## _infoは区間和など。 // ANSWER ## _tempがCONTINUE_CONDITIONを満たす限り、ANSWER ## _tempが // 条件ON_CONDITIONを満たすか否かを判定し、それがtrueになるか // VAR_TAR ## _LがVAR_TAR ## _Rに追い付くまでVAR_TPA ## _LとVPA_TPA ## _infoの // 更新操作UPDATE_Lを繰り返し、その後VAR_TPA ## _RとVPA_TPA ## _infoの // 更新操作UPDATE_Rを行う。(マクロとコンマの制約上、関数オブジェクトを用いる) // ON_CONDITIONがtrueとなる極大閉区間とその時点でのinfoをANSWERに格納する。 // 例えば長さNの非負整数値配列Aで極大な正値区間とそこでの総和を取得したい場合 // auto update_L = [&]( int& i_L , auto& i_info ){ i_info -= A[i_L++]; }; // auto update_R = [&]( int& i_R , auto& i_info ){ if( ++i_R < N ){ i_info += A[i_R]; } }; // TPA( interval , i , 0 , i_R < N , update_L( i_L , i_info ) , update_R( i_R , i_info ) , A[i_L] > 0 && A[i_R] > 0 , ll( A[0] ) ); // とすればtuple値配列intervalに{左端,右端,総和}の列が格納される。 // VAR_TPA ## _infoもintervalにコピーされるので、setやvectorなどのコピーのコストが // 大きいデータを用いてon,off判定する時はTPAより前に宣言して使う。 #define TPA( ANSWER , VAR_TPA , INIT , CONTINUE_CONDITION , UPDATE_L , UPDATE_R , ON_CONDITION , INFO_init ) \ vector> ANSWER{}; \ { \ auto init_TPA = INIT; \ decldecay_t( ANSWER.front() ) ANSWER ## _temp = { init_TPA , init_TPA , INFO_init }; \ auto ANSWER ## _prev = ANSWER ## _temp; \ auto& VAR_TPA ## _L = get<0>( ANSWER ## _temp ); \ auto& VAR_TPA ## _R = get<1>( ANSWER ## _temp ); \ auto& VAR_TPA ## _info = get<2>( ANSWER ## _temp ); \ bool on_TPA_prev = false; \ while( true ){ \ bool continuing = CONTINUE_CONDITION; \ bool on_TPA = continuing && ( ON_CONDITION ); \ CERR( continuing ? "尺取り中" : "尺取り終了" , ": [L,R] = [" , VAR_TPA ## _L , "," , VAR_TPA ## _R , "] ," , on_TPA_prev ? "on" : "off" , "->" , on_TPA ? "on" : "off" , ", info =" , VAR_TPA ## _info ); \ if( on_TPA_prev && ! on_TPA ){ \ ANSWER.push_back( ANSWER ## _prev ); \ CERR( #ANSWER , "に" , ANSWER ## _prev , "を格納します。" ); \ } \ if( continuing ){ \ if( on_TPA || VAR_TPA ## _L == VAR_TPA ## _R ){ \ ANSWER ## _prev = ANSWER ## _temp; \ UPDATE_R; \ } else { \ UPDATE_L; \ } \ } else { \ break; \ } \ on_TPA_prev = on_TPA; \ } \ } \ // Random ll GetRand( const ll& Rand_min , const ll& Rand_max ) { assert( Rand_min <= Rand_max ); ll answer = time( NULL ); return answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; } // Set #define DECLARATION_OF_HASH( ... ) \ struct hash<__VA_ARGS__> \ { \ \ inline size_t operator()( const __VA_ARGS__& n ) const; \ \ }; \ #define DEFINITION_OF_POP_FOR_SET( SET ) \ template inline T pop_max( SET& S ) { assert( !S.empty() ); auto itr = --S.end(); T answer = *itr; S.erase( itr ); return answer; } \ template inline T pop_min( SET& S ) { assert( !S.empty() ); auto itr = S.begin(); T answer = *itr; S.erase( itr ); return answer; } \ template inline SET& operator<<=( SET& S , T t ) { S.insert( move( t ) ); return S; } \ template inline SET& operator<<=( SET& S , U&& u ) { S.insert( T{ forward( u ) } ); return S; } \ template inline SET& operator>>=( SET& S , const T& t ) { auto itr = S.lower_bound( t ); assert( itr != S.end() && *itr == t ); S.erase( itr ); return S; } \ template inline SET& operator>>=( SET& S , const U& u ) { return S >>= T{ u }; } \ template inline const T& Get( const SET& S , int i ) { auto begin = S.begin() , end = S.end(); auto& itr = i < 0 ? ( ++i , --end ) : begin; while( i > 0 && itr != end ){ --i; ++itr; } while( i < 0 && itr != begin ){ ++i; --itr; } assert( i == 0 ); return *itr; } \ #define DEFINITION_OF_UNION_FOR_SET( SET ) \ template inline SET& operator|=( SET& S0 , const SET& S1 ) { for( auto& t : S1 ){ S0 += t; } return S0; } \ template inline SET operator|( SET S0 , const SET& S1 ) { return move( S0 |= S1 ); } \ class is_ordered { private: is_ordered() = delete; template static constexpr auto Check( const T& t ) -> decltype( t < t , true_type() ); static constexpr false_type Check( ... ); public: template static constexpr const bool value = is_same_v< decltype( Check( declval() ) ) , true_type >; }; template using Set = conditional_t>,unordered_set,conditional_t,set,void>>; template inline typename SET::const_iterator MaximumLeq( const SET& S , const T& t ) { auto itr = S.upper_bound( t ); return itr == S.begin() ? S.end() : --itr; } template inline typename SET::const_iterator MaximumLt( const SET& S , const T& t ) { auto itr = S.lower_bound( t ); return itr == S.begin() ? S.end() : --itr; } template inline typename SET::const_iterator MinimumGeq( const SET& S , const T& t ) { return S.lower_bound( t ); } template inline typename SET::const_iterator MinimumGt( const SET& S , const T& t ) { return S.upper_bound( t ); } template inline void EraseBack( SET& S , ITERATOR& itr ) { itr = S.erase( itr ); } template inline void EraseFront( SET& S , ITERATOR& itr ) { itr = S.erase( itr ); itr == S.begin() ? itr = S.end() : --itr; } template