結果
問題 | No.2432 Flip and Move |
ユーザー | 👑 p-adic |
提出日時 | 2023-08-31 19:49:15 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 128 ms / 2,000 ms |
コード長 | 23,547 bytes |
コンパイル時間 | 3,406 ms |
コンパイル使用メモリ | 240,256 KB |
実行使用メモリ | 35,712 KB |
最終ジャッジ日時 | 2024-06-11 01:30:32 |
合計ジャッジ時間 | 9,076 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 22 ms
34,688 KB |
testcase_01 | AC | 24 ms
34,688 KB |
testcase_02 | AC | 103 ms
35,712 KB |
testcase_03 | AC | 128 ms
35,584 KB |
testcase_04 | AC | 47 ms
35,712 KB |
testcase_05 | AC | 45 ms
35,712 KB |
testcase_06 | AC | 50 ms
35,200 KB |
testcase_07 | AC | 76 ms
35,328 KB |
testcase_08 | AC | 80 ms
35,456 KB |
testcase_09 | AC | 98 ms
35,584 KB |
testcase_10 | AC | 75 ms
35,328 KB |
testcase_11 | AC | 64 ms
35,456 KB |
testcase_12 | AC | 66 ms
35,200 KB |
testcase_13 | AC | 51 ms
35,456 KB |
testcase_14 | AC | 46 ms
34,816 KB |
testcase_15 | AC | 85 ms
35,456 KB |
testcase_16 | AC | 48 ms
35,072 KB |
testcase_17 | AC | 100 ms
35,456 KB |
testcase_18 | AC | 44 ms
34,944 KB |
testcase_19 | AC | 81 ms
35,456 KB |
testcase_20 | AC | 108 ms
35,712 KB |
testcase_21 | AC | 67 ms
35,200 KB |
testcase_22 | AC | 95 ms
35,456 KB |
testcase_23 | AC | 34 ms
34,944 KB |
testcase_24 | AC | 99 ms
35,584 KB |
testcase_25 | AC | 78 ms
35,328 KB |
testcase_26 | AC | 68 ms
35,328 KB |
testcase_27 | AC | 26 ms
34,816 KB |
testcase_28 | AC | 23 ms
34,816 KB |
testcase_29 | AC | 66 ms
35,200 KB |
testcase_30 | AC | 82 ms
35,584 KB |
testcase_31 | AC | 53 ms
35,328 KB |
testcase_32 | AC | 29 ms
34,816 KB |
testcase_33 | AC | 82 ms
35,584 KB |
testcase_34 | AC | 54 ms
35,328 KB |
testcase_35 | AC | 45 ms
35,200 KB |
testcase_36 | AC | 23 ms
34,688 KB |
testcase_37 | AC | 23 ms
34,688 KB |
testcase_38 | AC | 24 ms
34,688 KB |
ソースコード
#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 LIBRARY_SEARCH bool searched_library = false; LibrarySearch( searched_library ); if( searched_library ){ QUIT; }; #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 LIBRARY_SEARCH #define START_WATCH( PROCESS_NAME ) #define STOP_WATCH( HOW_MANY_TIMES ) #endif // #define RANDOM_TEST #include <bits/stdc++.h> 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<decltype( VAR )> #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 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 goto END_MAIN; END_MAIN: CERR( "" ); } #define QUIT return 0 #ifdef DEBUG inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } 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 ) ); goto END_MAIN #endif template <typename T> inline T Absolute( const T& a ){ return a > 0 ? a : -a; } template <typename T> inline T Residue( const T& a , const T& p ){ return a >= 0 ? a % p : p - 1 - ( ( - ( a + 1 ) ) % p ); } #define POWER( ANSWER , ARGUMENT , EXPONENT ) \ static_assert( ! is_same<TYPE_OF( ARGUMENT ),int>::value && ! is_same<TYPE_OF( ARGUMENT ),uint>::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<TYPE_OF( TARGET ),uint>::value && ! is_same<TYPE_OF( TARGET ),ull>::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 <typename T> inline typename set<T>::iterator MaximumLeq( set<T>& 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 <typename T> inline typename set<T>::iterator MaximumLt( set<T>& 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 <typename T> inline typename set<T>::iterator MinimumGeq( set<T>& S , const T& t ) { return S.lower_bound( t ); } // tより大きい値が存在すればその最小値のiterator、存在しなければend()を返す。 template <typename T> inline typename set<T>::iterator MinimumGt( set<T>& S , const T& t ) { return S.upper_bound( t ); } // 圧縮用 #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& #define SFINAE_FOR_LOOP_DETECTION_BODY( DEFAULT ) enable_if_t<is_convertible_v<U,T> >* DEFAULT // f:T->U-->Tのループ検出を行う。 template <typename T , typename U , U f(const T&) , int length_max> class LoopDetectionBody { private: T m_init; map<T,int> m_memory; vector<T> m_memory_inv; protected: int m_value[length_max]; int m_length; int m_loop_start; int m_loop_length; public: template <SFINAE_FOR_LOOP_DETECTION_BODY( = nullptr )> inline LoopDetectionBody( const T& init ); template <typename INT> T IteratedComposition( const INT& n ); inline const int& GetLength() const noexcept; inline const int& GetLoopStart() const noexcept; inline const int& GetLoopLength() const noexcept; void SearchLoopStart(); private: inline void SetInit(); virtual T e( const int& i ); virtual int e_inv( const T& t ); virtual void SetValue( const int& i ); }; template <int f(const int&) , int length_max> class LoopDetection : public LoopDetectionBody<int,int,f,length_max> { private: int m_value_inv[length_max]; public: inline LoopDetection( const int& init ); private: inline int e( const int& i ); inline int e_inv( const int& t ); void SetValue( const int& i ); }; template <typename T , typename U , U f(const T&) , int length_max> class MemorisationLoopDetection : public LoopDetectionBody<T,U,f,length_max> { public: inline MemorisationLoopDetection( const T& init ); }; template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> class EnumerationLoopDetection : public LoopDetectionBody<T,U,f,length_max> { private: int m_value_inv[length_max]; public: inline EnumerationLoopDetection( const T& init ); private: inline T e( const int& i ); inline int e_inv( const T& t ); void SetValue( const int& i ); }; template <typename T , typename U , U f(const T&) , int length_max> template <SFINAE_FOR_LOOP_DETECTION_BODY()> inline LoopDetectionBody<T,U,f,length_max>::LoopDetectionBody( const T& init ) : m_init( init ) , m_memory() , m_memory_inv() , m_value() , m_length() , m_loop_start( -1 ) , m_loop_length( -1 ) {} template <int f(const int&) , int length_max> inline LoopDetection<f,length_max>::LoopDetection( const int& init ) : LoopDetectionBody<int,int,f,length_max>( init ) , m_value_inv() { for( int i = 0 ; i < length_max ; i++ ){ m_value_inv[i] = -1; } } template <typename T , typename U , U f(const T&) , int length_max> inline MemorisationLoopDetection<T,U,f,length_max>::MemorisationLoopDetection( const T& init ) : LoopDetectionBody<T,U,f,length_max>( init ) {} template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::EnumerationLoopDetection( const T& init ) : LoopDetectionBody<T,U,f,length_max>( init ) , m_value_inv() { for( int i = 0 ; i < length_max ; i++ ){ m_value_inv[i] = -1; } } template <typename T , typename U , U f(const T&) , int length_max> template <typename INT> T LoopDetectionBody<T,U,f,length_max>::IteratedComposition( const INT& n ) { if( m_length == 0 ){ SetInit(); } if( n < m_length ){ return e( m_value[n] ); } if( m_loop_start != -1 ){ return e( m_value[ m_loop_start + ( n - m_loop_start ) % m_loop_length ] ); } SetValue( e_inv( f( e( m_value[m_length - 1] ) ) ) ); return IteratedComposition( n ); } template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLength() const noexcept { return m_length; } template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLoopStart() const noexcept { return m_loop_start; } template <typename T , typename U , U f(const T&) , int length_max> inline const int& LoopDetectionBody<T,U,f,length_max>::GetLoopLength() const noexcept { return m_loop_length; } template <typename T , typename U , U f(const T&) , int length_max> void LoopDetectionBody<T,U,f,length_max>::SearchLoopStart() { assert( m_loop_start == -1 ); int n = 0; while( m_loop_start == -1 ){ IteratedComposition( n++ ); } return; } template <typename T , typename U , U f(const T&) , int length_max> inline void LoopDetectionBody<T,U,f,length_max>::SetInit() { assert( m_length == 0 ); SetValue( e_inv( m_init ) ); } template <typename T , typename U , U f(const T&) , int length_max> T LoopDetectionBody<T,U,f,length_max>::e( const int& i ) { assert( i < m_length ); return m_memory_inv[i]; } template <int f(const int&) , int length_max> inline int LoopDetection<f,length_max>::e( const int& i ) { return i; } template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline T EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::e( const int& i ) { return enum_T( i ); } template <typename T , typename U , U f(const T&) , int length_max> int LoopDetectionBody<T,U,f,length_max>::e_inv( const T& t ) { if( m_memory.count( t ) == 0 ){ assert( m_length < length_max ); m_value[m_length] = m_length; m_memory_inv.push_back( t ); return m_memory[t] = m_length++; } m_loop_length = m_length - ( m_loop_start = m_memory[t] ); return m_loop_start; } template <int f(const int&) , int length_max> inline int LoopDetection<f,length_max>::e_inv( const int& t ) { return t; } template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> inline int EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::e_inv( const T& t ) { return enum_T_inv( t ); } template <typename T , typename U , U f(const T&) , int length_max> void LoopDetectionBody<T,U,f,length_max>::SetValue( const int& i ) {} template <int f(const int&) , int length_max> void LoopDetection<f,length_max>::SetValue( const int& i ) { using base = LoopDetectionBody<int,int,f,length_max>; int& m_value_inv_i = m_value_inv[i]; if( m_value_inv_i != -1 ){ base::m_loop_length = base::m_length - ( base::m_loop_start = m_value_inv_i ); } else { base::m_value[base::m_length] = i; m_value_inv_i = base::m_length++; } return; } template <typename T , typename U , U f(const T&) , int length_max , T enum_T(const int&) , int enum_T_inv(const T&)> void EnumerationLoopDetection<T,U,f,length_max,enum_T,enum_T_inv>::SetValue( const int& i ) { using base = LoopDetectionBody<T,U,f,length_max>; int& m_value_inv_i = m_value_inv[i]; if( m_value_inv_i != -1 ){ base::m_loop_length = base::m_length - ( base::m_loop_start = m_value_inv_i ); } else { base::m_value[base::m_length] = i; m_value_inv_i = base::m_length++; } return; } // inline CEXPR( ll , P , 998244353 ); // inline CEXPR( ll , P , 1000000007 ); // // 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個 // TYPE_OF( bound_N ) N; // // 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個 // TYPE_OF( bound_M ) M; // 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 ); DEXPR( int , bound_HW , 1000000 , 100 ); // 0が6個 // CEXPR( int , bound_HW , 1000000000 ); // 0が5個 int H , W , H_minus , W_minus , HW; inline pair<int,int> EnumHW( const int& v ) { return { v / W , v % W }; } inline int EnumHW_inv( const int& h , const int& w ) { return h * W + w; } // inline void SetEdgeOnGrid( const string& Si , const int& i , list<int> ( &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+1<H){e[EnumHW_inv(i+1,j)].push_back(v);}if(j>0){e[EnumHW_inv(i,j-1)].push_back(v);}if(j+1<W){e[EnumHW_inv(i,j+1)].push_back(v);}}}} const string direction[4] = {"U","R","D","L"}; inline int DirectionNumberOnGrid( const int& i , const int& j , const int& k , const int& h ){return i<k?2:i>k?0:j<h?1:j>h?3:(assert(false),-1);} 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);} inline int ReverseDirectionNumberOnGrid( const int& n ){assert(0<=n&&n<4);return(n+2)%4;} // bool open[bound_H+1][bound_W+1]={}; // inline void SetWallOnGrid( const string& Si , const int& i , bool ( &open )[bound_H+1][bound_W+1] , const char& walkable = '.' ){bool(&open_i)[bound_W+1]=open[i];FOR(j,0,W){open_i[j]=Si[j]==walkable?true:(assert(Si[j]=='#'),false);}} // using path_type = int; // // using path_type = pair<int,ll>; // list<path_type> e[bound_N]; // // list<path_type> e[bound_HW]; // list<path_type> E( const int& i ) // { // list<path_type> answer = e[i]; // // 入力によらない処理 // return answer; // } // template <typename T> inline T add( const T& t0 , const T& t1 ) { return t0 + t1; } // template <typename T> inline const T& zero() { static const T z = 0; return z; } // template <typename T> inline T add_inv( const T& t ) { return -t; } // template <typename T> inline T multiply( const T& t0 , const T& t1 ) { return t0 * t1; } // template <typename T> inline const T& one() { static const T o = 1; return o; } // inline int id( const int& v ) { return v; } using type = tuple<int,int,bool,bool>; type f( const type& t ) { auto [i,j,D,R] = t; if( D ){ if( i + 1 < H ){ i++; } else { D = !D; } } else { if( i > 0 ){ i--; } else { D = !D; } } if( R ){ if( j + 1 < W ){ j++; } else { R = !R; } } else { if( j > 0 ){ j--; } else { R = !R; } } return { i , j ,D , R }; } type enum_type( const int& t ){ auto [i,j] = EnumHW( t / 4 ); return { i , j , t % 4 / 2 , t % 2 }; } int enum_type_inv( const type& t ){ auto&& [i,j,D,R] = t; return EnumHW_inv( i , j ) * 4 + D * 2 + R; } int main() { UNTIE; LIBRARY_SEARCH; TEST_CASE_NUM( 1 ); START_MAIN; // // CIN( ll , N ); // SET_ASSERT( N , 1 , bound_N ); // // CIN( ll , M ); // SET_ASSERT( M , 1 , bound_M ); // // CIN( ll , K ); // CIN( string , S ); // CIN( string , T ); cin >> H >> W; H_minus = H - 1; W_minus = W - 1; HW = H * W; assert( HW <= bound_HW ); // FOR( i , 0 , H ){ // CIN( string , Si ); // SetEdgeOnGrid( Si , i , e ); // // SetWallOnGrid( Si , i , open ); // } CIN( ll , K ); EnumerationLoopDetection<type,type,f,bound_HW*4,enum_type,enum_type_inv> ld{ { 0 , 0 , true , true } }; ld.SearchLoopStart(); const int& loop_start = ld.GetLoopStart(); const int& loop_length = ld.GetLoopLength(); bool b[H][W] = {}; type t = { 0 , 0 , true , true }; if( K < loop_start ){ REPEAT( K ){ auto& [i,j,D,R] = t; b[i][j] = !b[i][j]; t = f( t ); } } else { REPEAT( loop_start ){ auto& [i,j,D,R] = t; b[i][j] = !b[i][j]; t = f( t ); } K -= loop_start; if( ( K / loop_length ) % 2 == 1 ){ REPEAT( loop_length ){ auto& [i,j,D,R] = t; b[i][j] = !b[i][j]; t = f( t ); } } K %= loop_length; REPEAT( K ){ auto& [i,j,D,R] = t; b[i][j] = !b[i][j]; t = f( t ); } } FOR( i , 0 , H ){ FOR( j , 0 , W ){ cout << ( b[i][j] ? "#" : "." ); } cout << "\n"; } // ll A[N]; // ll B[N]; // // ll A[bound_N]; // // ll B[bound_N]; // FOR( i , 0 , N ){ // cin >> A[i]; // cin >> B[i]; // } // FOR( i , 0 , M ){ // CIN_ASSERT( ui , 1 , N ); // CIN_ASSERT( vi , 1 , N ); // ui--; // vi--; // e[ui].push_back( vi ); // e[vi].push_back( ui ); // } // CIN( int , Q ); // // DEXPR( int , bound_Q , 100000 , 100 ); // // CIN_ASSERT( Q , 1 , bound_Q ); // REPEAT( Q ){ // COUT( N ); // } // // ll guchoku = Guchoku(); // ll answer = 0; // FOR( i , 0 , N ){ // answer += A[i]; // } // // COUT( ( answer ) ); // RETURN( answer ); FINISH_MAIN; QUIT; }