結果

問題 No.2149 Vanitas Vanitatum
ユーザー 👑 p-adicp-adic
提出日時 2022-12-07 11:11:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 7,893 bytes
コンパイル時間 3,782 ms
コンパイル使用メモリ 222,484 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 04:19:02
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 4 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 6 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ( "O3" )
#pragma GCC target ( "avx" )
#include <bits/stdc++.h>
using namespace std;

using uint = unsigned int;
using ll = long long;

#define TYPE_OF( VAR ) remove_const<remove_reference<decltype( VAR )>::type >::type
#define UNTIE ios_base::sync_with_stdio( false ); cin.tie( nullptr ) 
#define CEXPR( LL , BOUND , VALUE ) constexpr const LL BOUND = VALUE 
#define CIN( LL , A ) LL A; cin >> A 
#define ASSERT( A , MIN , MAX ) assert( MIN <= A && A <= MAX ) 
#define CIN_ASSERT( A , MIN , MAX ) CIN( TYPE_OF( MAX ) , A ); 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 FOR_ITR( ARRAY , ITR , END ) for( auto ITR = ARRAY .begin() , END = ARRAY .end() ; ITR != END ; ITR ++ ) 
#define REPEAT( HOW_MANY_TIMES ) FOR( VARIABLE_FOR_REPEAT , 0 , HOW_MANY_TIMES ) 
#define QUIT return 0 
#define COUT( ANSWER ) cout << ( ANSWER ) << "\n"; 
#define RETURN( ANSWER ) COUT( ANSWER ); QUIT 
#define DOUBLE( PRECISION , ANSWER ) cout << fixed << setprecision( PRECISION ) << ( ANSWER ) << "\n"; QUIT 

#define POWER( ANSWER , ARGUMENT , EXPONENT )				\
  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 )		\
  TYPE_OF( ARGUMENT ) ANSWER{ 1 };					\
  {									\
    TYPE_OF( ARGUMENT ) 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 BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  ll ANSWER = MAXIMUM;							\
  {									\
    ll VARIABLE_FOR_BINARY_SEARCH_L = MINIMUM;				\
    ll VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;				\
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
    if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
      VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;				\
    } else {								\
      ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
    }									\
    while( VARIABLE_FOR_BINARY_SEARCH_L != ANSWER ){			\
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;				\
	break;								\
      } else {								\
	if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){		\
	  VARIABLE_FOR_BINARY_SEARCH_L = ANSWER;			\
	} else {							\
	  VARIABLE_FOR_BINARY_SEARCH_U = ANSWER;			\
	}								\
	ANSWER = ( VARIABLE_FOR_BINARY_SEARCH_L + VARIABLE_FOR_BINARY_SEARCH_U ) / 2; \
      }									\
    }									\
  }									\
									\


// 二進法の二分探索
#define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , TARGET )		\
  ll ANSWER = MINIMUM;							\
  {									\
    ll VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 = 1;			\
    ll VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( MAXIMUM ) - ANSWER; \
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 <= VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH ){ \
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 *= 2;			\
    }									\
    VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 /= 2;			\
    ll VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
    while( VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 != 0 ){		\
      ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 + VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2; \
      VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH = ( TARGET ) - ( EXPRESSION ); \
      if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH == 0 ){		\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
	break;								\
      } else if( VARIABLE_FOR_DIFFERENCE_FOR_BINARY_SEARCH > 0 ){	\
	VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2 = ANSWER;		\
      }									\
      VARIABLE_FOR_POWER_FOR_BINARY_SEARCH_2 /= 2;			\
    }									\
    ANSWER = VARIABLE_FOR_ANSWER_FOR_BINARY_SEARCH_2;			\
  }									\
									\


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 - ( - a - 1 ) % p - 1; }



inline vector<int> Difference( const vector<int>& A )
{
  int size = A.size();
  vector<int> answer{};
  if( size == 0 ){
    return answer;
  }
  answer.push_back( A[0] );
  FOR( i , 1 , size ){
    answer.push_back( A[i] - A[i-1] );
  }
  return answer;
}

inline vector<int> inv_Difference( const vector<int>& A )
{
  int size = A.size();
  vector<int> answer{};
  int sum = 0;
  FOR( i , 0 , size ){
    answer.push_back( sum += A[i] );
  }
  return answer;
}

inline string to_bit( const vector<int>& A )
{
  int size = A.size();
  string answer{};
  static const string zero = "0";
  FOR( i , 0 , size ){
    int Ai = A[i];
    if( Ai != 0 ){
      string ones = "1";
      while( Ai != 0 ){
	if( Ai % 2 == 1 ){
	  answer += ones;
	}
	ones += ones;
	Ai /= 2;
      }
    }
    answer += zero;
  }
  return answer;
}

inline vector<int> inv_to_bit( const string& A , const int& r )
{
  int size = A.size();
  vector<int> answer{};
  int i_start = 0;
  int i = 0;
  int i2r = r;
  while( i2r < size ){
    if( A.substr( i2r , 1 ) == "0" ){
      answer.push_back( i - i_start );
      i_start = i + 1;
    }
    i++;
    i2r += 2;
  }
  return answer;
}

int main()
{
  UNTIE;
  CEXPR( ll , bound , 1000 );
  CIN_ASSERT( N , 1 , bound );
  vector<int> A{};
  int Ai_prev = 1;
  ll sum_A = 0;
  REPEAT( N ){
    CIN_ASSERT( Ai , Ai_prev , bound );
    A.push_back( Ai_prev = Ai );
    sum_A += Ai;
  }
  string A_bit = to_bit( Difference( A ) );
  vector<int> B_dif[2] = { move( inv_to_bit( A_bit , 0 ) ) , move( inv_to_bit( A_bit , 1 ) ) };
  vector<int> B[2] = { move( inv_Difference( B_dif[0] ) ) , move( inv_Difference( B_dif[1] ) ) };
  int sum_B[2] = {};
  FOR( d , 0 , 2 ){
    int& sum_B_d = sum_B[d];
    vector<int>& B_d = B[d];
    int size_B_d = B_d.size();
    FOR( i , 0 , size_B_d ){
      sum_B_d += B_d[i];
    }
  }
  ll sum_B_01 = sum_B[0] + sum_B[1];
  if( sum_B_01 * 2 != sum_A ){
    RETURN( 0 );
  }
  CEXPR( ll , P , 998244353 );
  ll answer = 1;
  FOREQ( i , 1 , sum_B_01 ){
    ( answer *= i ) %= P;
  }
  ll q = 1;
  FOR( d , 0 , 2 ){
    vector<int>& B_d = B[d];
    int i_min = 0;
    int size_B_d = B_d.size();
    if( size_B_d > 0 ){
      int i_max = size_B_d - 1;
      int j_lim = B_d[i_max];
      FOR( j , 0 , j_lim ){
	BS( i_start , i_min , i_max , B_d[i_start] <= j ? -1 : i_start == 0 ? 0 : B_d[i_start - 1] <= j ? 0 : 1 , 0 );
	i_min = i_start;
	FOR( i , i_start , size_B_d ){
	  ( q *= ( B_d[i] - j ) + ( i - i_start ) ) %= P;
	}
      }
    }
  }
  POWER_MOD( inv_q , q , P - 2 , P );
  ( answer *= inv_q ) %= P;
  RETURN( answer );
}
0