結果

問題 No.2432 Flip and Move
ユーザー 👑 p-adicp-adic
提出日時 2023-08-31 20:09:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 19,903 bytes
コンパイル時間 3,584 ms
コンパイル使用メモリ 237,088 KB
実行使用メモリ 35,828 KB
最終ジャッジ日時 2024-06-11 01:31:17
合計ジャッジ時間 8,912 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
34,688 KB
testcase_01 AC 11 ms
34,560 KB
testcase_02 AC 110 ms
35,828 KB
testcase_03 AC 138 ms
35,584 KB
testcase_04 AC 33 ms
35,712 KB
testcase_05 AC 34 ms
35,712 KB
testcase_06 AC 44 ms
35,200 KB
testcase_07 AC 74 ms
35,240 KB
testcase_08 AC 80 ms
35,456 KB
testcase_09 AC 106 ms
35,712 KB
testcase_10 AC 73 ms
35,272 KB
testcase_11 AC 66 ms
35,328 KB
testcase_12 AC 67 ms
35,296 KB
testcase_13 AC 47 ms
35,456 KB
testcase_14 AC 40 ms
34,816 KB
testcase_15 AC 91 ms
35,664 KB
testcase_16 AC 50 ms
35,072 KB
testcase_17 AC 149 ms
35,440 KB
testcase_18 AC 39 ms
34,816 KB
testcase_19 AC 113 ms
35,300 KB
testcase_20 AC 151 ms
35,672 KB
testcase_21 AC 108 ms
35,456 KB
testcase_22 AC 133 ms
35,328 KB
testcase_23 AC 27 ms
34,816 KB
testcase_24 AC 146 ms
35,368 KB
testcase_25 AC 100 ms
35,328 KB
testcase_26 AC 83 ms
35,456 KB
testcase_27 AC 15 ms
34,616 KB
testcase_28 AC 15 ms
34,696 KB
testcase_29 AC 88 ms
35,304 KB
testcase_30 AC 135 ms
35,456 KB
testcase_31 AC 68 ms
35,300 KB
testcase_32 AC 19 ms
34,688 KB
testcase_33 AC 120 ms
35,444 KB
testcase_34 AC 57 ms
35,072 KB
testcase_35 AC 45 ms
35,328 KB
testcase_36 AC 11 ms
34,560 KB
testcase_37 AC 12 ms
34,688 KB
testcase_38 AC 11 ms
34,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 SFINAE_FOR_LOOP_DETECTION_BODY( DEFAULT ) enable_if_t<is_convertible_v<U,T> >* DEFAULT

// 写像f:T->Uとcast U->Tの合成のループ検出を行う。
// fとcastの合成の像の要素数がlength_max以下の場合にのみサポート。

// 反復合成O(loop_start+(n-loop_start)%loop_length)
// 像の要素数取得O(1)(ただしループ検出前の時のみO(loop_start+loop_length))
// ループ始点取得O(1)(ただしループ検出前の時のみO(loop_start+loop_length))
// ループ長取得O(1)(ただしループ検出前の時のみO(loop_start+loop_length))
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() noexcept;
  // ループ始点取得
  inline const int& GetLoopStart() noexcept;
  // ループ長取得
  inline const int& GetLoopLength() noexcept;
  
private:
  inline void SetInit();
  void SearchLoop();
  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() noexcept { if( m_loop_length == -1 ){ SearchLoop(); } 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() noexcept { if( m_loop_start == -1 ){ SearchLoop(); } 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() noexcept { if( m_loop_length == -1 ){ SearchLoop(); } return m_loop_length; }

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>
void LoopDetectionBody<T,U,f,length_max>::SearchLoop()
{

  assert( m_loop_length == -1 );
  int n = 0;
  
  while( m_loop_length == -1 ){

    IteratedComposition( n++ );
    
  }

  return;

}

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;

}

DEXPR( int , bound_HW , 1000000 , 100 ); // 0が6個
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; }

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 >> H >> W;
  H_minus = H - 1;
  W_minus = W - 1;
  HW = H * W;
  assert( HW <= bound_HW );

  CIN( ll , K );
  EnumerationLoopDetection<type,type,f,bound_HW*4,enum_type,enum_type_inv> ld{ { 0 , 0 , true , true } };
  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";
  }

  FINISH_MAIN;
  QUIT;
}
0