#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; }; #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 #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( IS_FIXED , DECIMAL_DIGITS ) if constexpr( IS_FIXED ){ cout << fixed; } cout << 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 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 FINISH_MAIN QUIT; } END_MAIN: CERR( "" ); #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } void AutoCheck( bool& auto_checked ); #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 T xor_add( 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 add_inv( const T& t ) { return -t; } 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& // 大きな素数 // inline CEXPR( ll , P , 998244353 ); // // inline CEXPR( ll , P2 , 1000000007 ); // データ構造使用畤のNの上限 // inline CEXPR( int , bound_N , 10 ); inline DEXPR( int , bound_N , 100000 , 100 ); // 0が5個 // inline CEXPR( int , bound_N , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_N , 1000000000000000000 ); // 0が18個 // データ構造使用畤のMの上限 // inline CEXPR( TYPE_OF( bound_N ) , bound_M , bound_N ); // inline CEXPR( int , bound_M , 10 ); inline DEXPR( int , bound_M , 100000 , 100 ); // 0が5個 // inline CEXPR( int , bound_M , 1000000000 ); // 0が9個 // inline CEXPR( ll , bound_M , 1000000000000000000 ); // 0が18個 // データ構造や壁配列使用畤のH,Wの上限 inline DEXPR( int , bound_H , 1000 , 10 ); // inline DEXPR( int , bound_H , 100000 , 10 ); // 0が5個 // inline CEXPR( int , bound_H , 1000000000 ); // 0が9個 inline CEXPR( int , bound_W , bound_H ); static_assert( ll( bound_H ) * bound_W < ll( 1 ) << 31 ); inline CEXPR( int , bound_HW , bound_H * bound_W ); // CEXPR( int , bound_HW , 100000 ); // 0が5個 // CEXPR( int , bound_HW , 1000000 ); // 0が6個 inline void SetEdgeOnGrid( const string& Si , const int& i , list ( &e )[bound_HW] , 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; // list e[bound_M]; // bound_Mのデフォルト値は10^5 // // list e[bound_HW]; // bound_HWのデフォルト値は10^6 // list E( const int& i ) // { // list answer = e[i]; // // 入力によらない処理 // return answer; // } template class Extended_ull; template class ConstantsForExtended_ull { friend class Extended_ull; private: ConstantsForExtended_ull() = delete; static constexpr const int g_digit_half = digit >> 1; static constexpr const int g_full_digit = digit << 1; static constexpr const int g_full_digit_minus = g_full_digit - 1; static constexpr const UINT g_low_bit = ( UINT( 1 ) << g_digit_half ) - 1; }; template class Extended_ull { private: UINT m_n[2]; public: inline constexpr Extended_ull( const UINT& n0 = 0 , const UINT& n1 = 0 ) noexcept; inline constexpr Extended_ull( const UINT ( &n )[2] ) noexcept; inline constexpr Extended_ull( UINT ( &&n )[2] ) noexcept; inline constexpr Extended_ull( const Extended_ull& n ) noexcept; inline constexpr Extended_ull( Extended_ull&& n ) noexcept; inline constexpr Extended_ull& operator=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator=( Extended_ull&& n ) noexcept; inline constexpr Extended_ull& operator+=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator-=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator*=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator/=( const Extended_ull& n ); inline constexpr Extended_ull& operator%=( const Extended_ull& n ); inline constexpr Extended_ull& operator&=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator|=( const Extended_ull& n ) noexcept; inline constexpr Extended_ull& operator<<=( const int& n ) noexcept; inline constexpr Extended_ull& operator>>=( const int& n ) noexcept; inline constexpr Extended_ull& operator++() noexcept; inline constexpr Extended_ull& operator--() noexcept; inline constexpr Extended_ull operator++( int ) noexcept; inline constexpr Extended_ull operator--( int ) noexcept; inline constexpr Extended_ull operator+( const Extended_ull& n ) const noexcept; inline constexpr Extended_ull operator-() const noexcept; inline constexpr Extended_ull operator-( const Extended_ull& n ) const noexcept; inline constexpr Extended_ull operator*( const Extended_ull& n ) const noexcept; inline constexpr Extended_ull operator/( const Extended_ull& n ) const; inline constexpr Extended_ull operator%( const Extended_ull& n ) const; inline constexpr Extended_ull operator~() const noexcept; inline constexpr Extended_ull operator&( const Extended_ull& n ) const noexcept; inline constexpr Extended_ull operator|( const Extended_ull& n ) const noexcept; inline constexpr Extended_ull operator<<( const int& n ) const noexcept; inline constexpr Extended_ull operator>>( const int& n ) const noexcept; inline constexpr bool operator==( const Extended_ull& n ) const noexcept; inline constexpr bool operator!=( const Extended_ull& n ) const noexcept; inline constexpr bool operator<=( const Extended_ull& n ) const noexcept; inline constexpr bool operator>=( const Extended_ull& n ) const noexcept; inline constexpr bool operator<( const Extended_ull& n ) const noexcept; inline constexpr bool operator>( const Extended_ull& n ) const noexcept; inline constexpr const UINT& GetLowerDigit() const noexcept; inline constexpr const UINT& GetUpperDigit() const noexcept; inline constexpr Extended_ull Quotient( const Extended_ull& n ); inline constexpr UINT Sqrt() const noexcept; static inline constexpr Extended_ull Sum( const UINT& n0 , const UINT& n1 ) noexcept; static inline constexpr Extended_ull Prod( const UINT& n0 , const UINT& n1 ) noexcept; }; template inline string to_string( Extended_ull n ); template inline basic_ostream& operator<<( basic_ostream& os , const Extended_ull& n ); template inline constexpr Extended_ull::Extended_ull( const UINT& n0 , const UINT& n1 ) noexcept : m_n{ n0 , n1 } {} template inline constexpr Extended_ull::Extended_ull( const UINT ( &n )[2] ) noexcept : m_n{ n[0] , n[1] } {} template inline constexpr Extended_ull::Extended_ull( UINT ( &&n )[2] ) noexcept : m_n{ move( n[0] ) , move( n[1] ) } {} template inline constexpr Extended_ull::Extended_ull( const Extended_ull& n ) noexcept : m_n{ n.m_n[0] , n.m_n[1] } {} template inline constexpr Extended_ull::Extended_ull( Extended_ull&& n ) noexcept : m_n{ move( n.m_n[0] ) , move( n.m_n[1] ) } {} template inline constexpr Extended_ull& Extended_ull::operator=( const Extended_ull& n ) noexcept { m_n[0] = n.m_n[0]; m_n[1] = n.m_n[1]; return *this; } template inline constexpr Extended_ull& Extended_ull::operator=( Extended_ull&& n ) noexcept { swap( m_n , n.m_n ); return *this; } template inline constexpr Extended_ull& Extended_ull::operator+=( const Extended_ull& n ) noexcept { return operator=( operator+( n ) ); } template inline constexpr Extended_ull& Extended_ull::operator-=( const Extended_ull& n ) noexcept { return operator=( operator-( n ) ); } template inline constexpr Extended_ull& Extended_ull::operator*=( const Extended_ull& n ) noexcept { return operator=( operator*( n ) ); } template inline constexpr Extended_ull& Extended_ull::operator/=( const Extended_ull& n ) { return *this = Quotient( n ); } template inline constexpr Extended_ull& Extended_ull::operator%=( const Extended_ull& n ) { assert( n != 0 ); constexpr const Extended_ull full_power = Extended_ull( 1 ) << ConstantsForExtended_ull::g_full_digit_minus; Extended_ull power = full_power; for( int d = ConstantsForExtended_ull::g_full_digit_minus ; d >= 0 ; d-- ){ if( ( *this >> d ) >= n ){ *this -= ( n << d ); } power >>= 1; } return *this; } template inline constexpr Extended_ull& Extended_ull::operator&=( const Extended_ull& n ) noexcept { m_n[0] &= n.m_n[0]; m_n[1] &= n.m_n[1]; return *this; } template inline constexpr Extended_ull& Extended_ull::operator|=( const Extended_ull& n ) noexcept { m_n[0] |= n.m_n[0]; m_n[1] |= n.m_n[1]; return *this; } template inline constexpr Extended_ull& Extended_ull::operator<<=( const int& n ) noexcept { n == 0 ? m_n[1] : n < digit ? ( ( m_n[1] <<= n ) |= ( m_n[0] >> ( digit - n ) ) , m_n[0] <<= n ) : n < ConstantsForExtended_ull::g_full_digit ? ( m_n[1] = ( m_n[0] << ( n - digit ) ) , m_n[0] = 0 ) : ( m_n[1] = m_n[0] = 0 ); return *this; } template inline constexpr Extended_ull& Extended_ull::operator>>=( const int& n ) noexcept { n == 0 ? m_n[0] : n < digit ? ( ( m_n[0] >>= n ) |= ( m_n[1] << ( digit - n ) ) , m_n[1] >>= n ) : n < ConstantsForExtended_ull::g_full_digit ? ( m_n[0] = ( m_n[1] >> ( n - digit ) ) , m_n[1] = 0 ) : ( m_n[0] = m_n[1] = 0 ); return *this; } template inline constexpr Extended_ull& Extended_ull::operator++() noexcept { if( ++m_n[0] == 0 ){ ++m_n[1]; } return *this; } template inline constexpr Extended_ull& Extended_ull::operator--() noexcept { if( m_n[0]-- == 0 ){ m_n[1]--; } return *this; } template inline constexpr Extended_ull Extended_ull::operator++( int ) noexcept { const Extended_ull answer = *this; if( ++m_n[0] == 0 ){ ++m_n[1]; } return answer; } template inline constexpr Extended_ull Extended_ull::operator--( int ) noexcept { const Extended_ull answer = *this; if( m_n[0]-- == 0 ){ m_n[1]--; } return answer; } template inline constexpr Extended_ull Extended_ull::operator+( const Extended_ull& n ) const noexcept { Extended_ull answer = Sum( m_n[0] , n.m_n[0] ); answer.m_n[1] += m_n[1] + n.m_n[1]; return answer; } template inline constexpr Extended_ull Extended_ull::operator-() const noexcept { return ++( operator~() ); } template inline constexpr Extended_ull Extended_ull::operator-( const Extended_ull& n ) const noexcept { return operator+( -n ); } template inline constexpr Extended_ull Extended_ull::operator*( const Extended_ull& n ) const noexcept { Extended_ull answer = Prod( m_n[0] , n.m_n[0] ); answer.m_n[1] += m_n[1] * n.m_n[0] + m_n[0] * n.m_n[1]; return answer; } template inline constexpr Extended_ull Extended_ull::operator/( const Extended_ull& n ) const { return move( Extended_ull( *this ) /= n ); } template inline constexpr Extended_ull Extended_ull::operator%( const Extended_ull& n ) const { return move( Extended_ull( *this ) %= n ); } template inline constexpr Extended_ull Extended_ull::operator~() const noexcept { return Extended_ull( ~m_n[0] , ~m_n[1] ); } template inline constexpr Extended_ull Extended_ull::operator&( const Extended_ull& n ) const noexcept { Extended_ull answer = *this; return move( answer &= n ); } template inline constexpr Extended_ull Extended_ull::operator|( const Extended_ull& n ) const noexcept { Extended_ull answer = *this; return move( answer |= n ); } template inline constexpr Extended_ull Extended_ull::operator<<( const int& n ) const noexcept { Extended_ull answer = *this; return move( answer <<= n ); } template inline constexpr Extended_ull Extended_ull::operator>>( const int& n ) const noexcept { Extended_ull answer = *this; return move( answer >>= n ); } template inline constexpr bool Extended_ull::operator==( const Extended_ull& n ) const noexcept { return m_n[0] == n.m_n[0] && m_n[1] == n.m_n[1]; } template inline constexpr bool Extended_ull::operator!=( const Extended_ull& n ) const noexcept { return !( *this == n ); } template inline constexpr bool Extended_ull::operator<=( const Extended_ull& n ) const noexcept { return !( n < *this ); } template inline constexpr bool Extended_ull::operator>=( const Extended_ull& n ) const noexcept { return !( *this < n ); } template inline constexpr bool Extended_ull::operator<( const Extended_ull& n ) const noexcept { return m_n[1] < n.m_n[1] || ( m_n[1] == n.m_n[1] && m_n[0] < n.m_n[0] ); } template inline constexpr bool Extended_ull::operator>( const Extended_ull& n ) const noexcept { return n < *this; } template inline constexpr const UINT& Extended_ull::GetLowerDigit() const noexcept { return m_n[0]; } template inline constexpr const UINT& Extended_ull::GetUpperDigit() const noexcept { return m_n[1]; } template inline constexpr Extended_ull Extended_ull::Quotient( const Extended_ull& n ) { assert( n != 0 ); constexpr const Extended_ull full_power = Extended_ull( 1 ) << ConstantsForExtended_ull::g_full_digit_minus; Extended_ull answer{}; Extended_ull power = full_power; for( int d = ConstantsForExtended_ull::g_full_digit_minus ; d >= 0 ; d-- ){ if( ( *this >> d ) >= n ){ *this -= ( n << d ); answer |= power; } power >>= 1; } return answer; } template inline constexpr UINT Extended_ull::Sqrt() const noexcept { constexpr const UINT UINT_max = ~( UINT( 0 ) ); constexpr const Extended_ull threshold{ 1 , UINT_max - 1 }; if( threshold <= *this ){ return UINT_max; } UINT l = 0; UINT r = UINT_max; while( l + 1 < r ){ UINT m = ( ( l + r ) >> 1 ); ( Extended_ull::Prod( m , m ) <= *this ? l : r ) = move( m ); } return l; } template inline constexpr Extended_ull Extended_ull::Sum( const UINT& n0 , const UINT& n1 ) noexcept { UINT temp[2] = { ( n0 & ConstantsForExtended_ull::g_low_bit ) + ( n1 & ConstantsForExtended_ull::g_low_bit ) , ( n0 >> ConstantsForExtended_ull::g_digit_half ) + ( n1 >> ConstantsForExtended_ull::g_digit_half ) }; temp[1] += ( temp[0] >> ConstantsForExtended_ull::g_digit_half ); ( temp[0] &= ConstantsForExtended_ull::g_low_bit ) |= ( temp[1] << ConstantsForExtended_ull::g_digit_half ); temp[1] >>= ConstantsForExtended_ull::g_digit_half; return Extended_ull( move( temp ) ); } template inline constexpr Extended_ull Extended_ull::Prod( const UINT& n0 , const UINT& n1 ) noexcept { const UINT n0_copy[2] = { n0 & ConstantsForExtended_ull::g_low_bit , n0 >> ConstantsForExtended_ull::g_digit_half }; const UINT n1_copy[2] = { n1 & ConstantsForExtended_ull::g_low_bit , n1 >> ConstantsForExtended_ull::g_digit_half }; UINT temp0[2] = { n0_copy[0] * n1_copy[0] , n0_copy[1] * n1_copy[1] }; UINT temp1[2] = { n0_copy[0] * n1_copy[1] , n0_copy[1] * n1_copy[0] , }; temp0[1] += ( temp1[0] >> ConstantsForExtended_ull::g_digit_half ) + ( temp1[1] >> ConstantsForExtended_ull::g_digit_half ); ( temp1[1] &= ConstantsForExtended_ull::g_low_bit ) += ( temp1[0] &= ConstantsForExtended_ull::g_low_bit ) + ( temp0[0] >> ConstantsForExtended_ull::g_digit_half ); temp0[1] += temp1[1] >> ConstantsForExtended_ull::g_digit_half; ( temp0[0] &= ConstantsForExtended_ull::g_low_bit ) |= ( temp1[1] << ConstantsForExtended_ull::g_digit_half ); return Extended_ull( move( temp0 ) ); } template inline string to_string( Extended_ull n ) { list temp{}; static const Extended_ull zero{ 0 }; static const Extended_ull ten{ 10 }; while( n != zero ){ Extended_ull n_div = n / ten; Extended_ull n_res = n - ( n_div * ten ); temp.push_front( to_string( n_res.GetLowerDigit() )[0] ); n = move( n_div ); } if( temp.empty() ){ temp.push_back( '0' ); } string answer = ""; for( auto itr = temp.begin() , end = temp.end() ; itr != end ; itr++ ){ answer += *itr; } return answer; } template inline basic_ostream& operator<<( basic_ostream& os , const Extended_ull& n ) { return os << to_striing( n ); } template class Extended_udouble; template inline string to_string( Extended_udouble n ); template class ConstantsForExtended_udouble { friend class Extended_udouble; friend string to_string<>( Extended_udouble n ); private: static constexpr UINT g_full_bit = UINT( 1 ) << ( digit - 1 ); static constexpr UINT g_ten_power = UINT( 1000000000000000000 ); static constexpr int g_ten_exponent = 18; }; template class Extended_udouble { private: UINT m_n; // 2羃の指数 int m_exponent; public: inline constexpr Extended_udouble( const UINT& n = 0 , const int& exponent = 0 ) noexcept; inline constexpr Extended_udouble( const Extended_udouble& n ) noexcept; inline constexpr Extended_udouble( Extended_udouble&& n ) noexcept; inline constexpr Extended_udouble( Extended_ull n , const int exponent = 0 ) noexcept; inline constexpr Extended_udouble& operator=( const Extended_udouble& n ) noexcept; inline constexpr Extended_udouble& operator=( Extended_udouble&& n ) noexcept; inline constexpr Extended_udouble& operator+=( const Extended_udouble& n ) noexcept; inline constexpr Extended_udouble& operator*=( const Extended_udouble& n ) noexcept; inline constexpr Extended_udouble& operator/=( const Extended_udouble& n ); inline constexpr Extended_udouble& operator<<=( const int& n ) noexcept; inline constexpr Extended_udouble& operator>>=( const int& n ) noexcept; inline constexpr Extended_udouble operator+( const Extended_udouble& n ) const noexcept; inline constexpr Extended_udouble operator*( const Extended_udouble& n ) const noexcept; inline constexpr Extended_udouble operator/( const Extended_udouble& n ) const; inline constexpr Extended_udouble operator<<( const int& n ) const noexcept; inline constexpr Extended_udouble operator>>( const int& n ) const noexcept; // 近似的等号 inline constexpr bool operator==( const Extended_udouble& n ) const noexcept; inline constexpr bool operator!=( const Extended_udouble& n ) const noexcept; inline constexpr bool operator<=( const Extended_udouble& n ) const noexcept; inline constexpr bool operator>=( const Extended_udouble& n ) const noexcept; inline constexpr bool operator<( const Extended_udouble& n ) const noexcept; inline constexpr bool operator>( const Extended_udouble& n ) const noexcept; inline constexpr const UINT& GetDigit() const noexcept; inline constexpr const int& GetExponent() const noexcept; inline constexpr Extended_udouble Sqrt() const noexcept; }; template inline string to_string( Extended_udouble n ); template inline basic_ostream& operator<<( basic_ostream& os , const Extended_udouble& n ); template inline constexpr Extended_udouble::Extended_udouble( const UINT& n , const int& exponent ) noexcept : m_n( n ) , m_exponent( exponent ) {} template inline constexpr Extended_udouble::Extended_udouble( const Extended_udouble& n ) noexcept : m_n( n.m_n ) , m_exponent( n.m_exponent ) {} template inline constexpr Extended_udouble::Extended_udouble( Extended_udouble&& n ) noexcept : m_n( move( n.m_n ) ) , m_exponent( n.m_exponent ) {} template inline constexpr Extended_udouble::Extended_udouble( Extended_ull n , const int exponent ) noexcept : m_n() , m_exponent() { const UINT& n1 = n.GetUpperDigit(); int l = 0; int r = digit; while( l + 1 < r ){ int m = ( l + r ) / 2; ( ( n1 >> m ) == 0 ? r : l ) = m; } m_n = ( n >>= r ).GetLowerDigit(); m_exponent = exponent + r; } template inline constexpr Extended_udouble& Extended_udouble::operator=( const Extended_udouble& n ) noexcept { m_n = n.m_n; m_exponent = n.m_exponent; return *this; } template inline constexpr Extended_udouble& Extended_udouble::operator=( Extended_udouble&& n ) noexcept { m_n = move( n.m_n ); m_exponent = n.m_exponent; return *this; } template inline constexpr Extended_udouble& Extended_udouble::operator+=( const Extended_udouble& n ) noexcept { return operator=( *this + n ); } template inline constexpr Extended_udouble& Extended_udouble::operator*=( const Extended_udouble& n ) noexcept { return operator=( *this * n ); } template inline constexpr Extended_udouble& Extended_udouble::operator/=( const Extended_udouble& n ) { return operator=( *this / n ); } template inline constexpr Extended_udouble& Extended_udouble::operator<<=( const int& n ) noexcept { m_exponent += n; return *this; } template inline constexpr Extended_udouble& Extended_udouble::operator>>=( const int& n ) noexcept { m_exponent -= n; return *this; } template inline constexpr Extended_udouble Extended_udouble::operator+( const Extended_udouble& n ) const noexcept { if( m_exponent < n.m_exponent ){ return n + *this; } if( m_exponent >= n.m_exponent + digit ){ return *this; } Extended_ull sum{ this->m_n }; ( sum <<= ( m_exponent - n.m_exponent ) ) += Extended_ull( n.m_n ); return Extended_udouble( sum , n.m_exponent ); } template inline constexpr Extended_udouble Extended_udouble::operator*( const Extended_udouble& n ) const noexcept { return Extended_udouble( Extended_ull::Prod( m_n , n.m_n ) , m_exponent + n.m_exponent ); } template inline constexpr Extended_udouble Extended_udouble::operator/( const Extended_udouble& n ) const { Extended_ull div{ this->m_n }; ( div <<= digit ) /= Extended_ull( n.m_n ); return Extended_udouble( div , -digit ); } template inline constexpr Extended_udouble Extended_udouble::operator<<( const int& n ) const noexcept { return Extended_udouble( m_n , m_exponent + n ); } template inline constexpr Extended_udouble Extended_udouble::operator>>( const int& n ) const noexcept { return Extended_udouble( m_n , m_exponent - n ); } template inline constexpr bool Extended_udouble::operator==( const Extended_udouble& n ) const noexcept { return m_exponent + digit <= n.m_exponent ? false : m_exponent >= n.m_exponent + digit ? false : m_exponent < n.m_exponent ? Extended_ull( *this ) == ( Extended_ull( n ) << ( n.m_exponent - m_exponent ) ) : ( Extended_ull( *this ) << ( m_exponent - n.m_exponent ) ) == Extended_ull( n ); } template inline constexpr bool Extended_udouble::operator!=( const Extended_udouble& n ) const noexcept { return !( *this == n ); } template inline constexpr bool Extended_udouble::operator<=( const Extended_udouble& n ) const noexcept { return !( n < *this ); } template inline constexpr bool Extended_udouble::operator>=( const Extended_udouble& n ) const noexcept { return !( *this < n ); } template inline constexpr bool Extended_udouble::operator<( const Extended_udouble& n ) const noexcept { return m_exponent + digit <= n.m_exponent ? true : m_exponent >= n.m_exponent + digit ? false : m_exponent < n.m_exponent ? Extended_ull( *this ) < ( Extended_ull( n ) << ( n.m_exponent - m_exponent ) ) : ( Extended_ull( *this ) << ( m_exponent - n.m_exponent ) ) < Extended_ull( n ); } template inline constexpr bool Extended_udouble::operator>( const Extended_udouble& n ) const noexcept { return n < *this; } template inline constexpr const UINT& Extended_udouble::GetDigit() const noexcept { return m_n; } template inline constexpr const int& Extended_udouble::GetExponent() const noexcept { return m_exponent; } template inline constexpr Extended_udouble Extended_udouble::Sqrt() const noexcept { Extended_ull this_copy{ this->m_n }; int exponent = m_exponent - digit; return exponent % 2 == 0 ? Extended_udouble( ( this_copy <<= digit ).Sqrt() , exponent >>= 1 ) : Extended_udouble( ( this_copy <<= ( digit - 1 ) ).Sqrt() , ( ++exponent ) >>= 1 ); } template inline string to_string( Extended_udouble n ) { static const Extended_udouble ten_power{ ConstantsForExtended_udouble::g_ten_power }; if( n.GetDigit() == 0 ){ return "0"; } const int& exponent = n.GetExponent(); int ten_digit = 0; while( exponent < 0 ){ n *= ten_power; ten_digit++; } Extended_ull n_copy{ n.GetDigit() }; n_copy <<= exponent; string answer = to_string( n_copy ); if( ten_digit > 0 ){ ten_digit *= ConstantsForExtended_udouble::g_ten_exponent; int size = answer.size(); if( size <= ten_digit ){ string leading = ""; for( int d = size ; d < ten_digit ; d++ ){ leading += "0"; } answer = "0." + leading + answer; } else { size -= ten_digit; answer = answer.substr( 0 , size ) + "." + answer.substr( size ); } } return answer; } template inline basic_ostream& operator<<( basic_ostream& os , const Extended_udouble& n ) { return os << to_string( n ); } using udouble = Extended_udouble; int main() { UNTIE; AUTO_CHECK; TEST_CASE_NUM( 1 ); START_MAIN; CIN( int , N ); udouble a{}; REPEAT( N ){ CIN( ull , x ); a += udouble( x ).Sqrt(); COUT( a ); } FINISH_MAIN; }