結果

問題 No.3105 Міжнародний підрядок саміт
ユーザー 👑 p-adicp-adic
提出日時 2023-04-16 19:59:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,536 ms / 3,153 ms
コード長 7,714 bytes
コンパイル時間 4,007 ms
コンパイル使用メモリ 239,328 KB
実行使用メモリ 167,488 KB
最終ジャッジ日時 2024-11-15 04:10:43
合計ジャッジ時間 8,046 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
167,392 KB
testcase_01 AC 1,536 ms
167,432 KB
testcase_02 AC 158 ms
167,356 KB
testcase_03 AC 1,493 ms
167,452 KB
testcase_04 AC 122 ms
167,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ( "O3" )
#pragma GCC optimize( "unroll-loops" )
#pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) )
#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 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 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 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 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;				\
    }									\
  }									\

inline CEXPR( int , bound_N , 13 );
inline CEXPR( int , lim_B , 1 << bound_N );

// O(2^N)
struct Card
{
  int m_val[lim_B];
  constexpr Card() : m_val()
  {
    int two_power = 1;
    FOR( d , 0 , bound_N ){
      FOR( B , 0 , two_power ){
	m_val[B | two_power] = m_val[B] + 1;
      }
      two_power <<= 1;
    }
  }
};

inline CEXPR( int , lim_x_shift , bound_N * ( bound_N - 1 ) + 1 );
inline CEXPR( int , bound_x , lim_x_shift >> 1 );
inline CEXPR( int , bound_three_power , 1594323 ); // 3^13

// O(3^N)
struct X
{
  vector<vector<bool> > m_val[lim_B];
  inline X( const int ( &card )[lim_B] ) : m_val()
  {
    FOR( B , 0 , lim_B ){
      vector<vector<bool> >& m_val_B = m_val[B];
      const int& B_card = card[B];
      m_val_B.reserve( B_card + 1 );
      FOREQ( p , 0 , B_card ){
	m_val_B[p].reserve( lim_x_shift );
      }
    }
    int x[bound_three_power] = { bound_x };
    int B[bound_three_power] = {};
    int p[bound_three_power] = {};
    int three_power = 1;
    int three_power2 = 2;
    int two_power = 1;
    FOR( d , 0 , bound_N ){
      FOR( i , 0 , three_power ){
	int& xi = x[i];
	int i_plus = i + three_power;
	int i_plus2 = i + three_power2;
	x[i_plus] = xi - d;
	x[i_plus2] = xi + d;
	B[i_plus] = B[i_plus2] = B[i] | two_power;
	p[i_plus2] = ( p[i_plus] = p[i] ) + 1;
      }
      three_power = three_power2 + three_power;
      three_power2 = three_power << 1;
      two_power <<= 1;
    }
    FOR( i , 1 , bound_three_power ){
      m_val[B[i]][p[i]][x[i]] = true;
    }
  }
};

// O(N^3 2^N)
struct Xlr
{
  int m_val[2][lim_B][bound_N+1][lim_x_shift];
  inline Xlr( const int ( &card )[lim_B] , const vector<vector<bool> > ( &x )[lim_B] ) : m_val()
  {
    int ( &xl )[lim_B][bound_N+1][lim_x_shift] = m_val[0];
    int ( &xr )[lim_B][bound_N+1][lim_x_shift] = m_val[1];
    FOR( B , 1 , lim_B ){
      const vector<vector<bool> > &xB = x[B];
      const int& B_card = card[B];
      int ( &xlB )[bound_N+1][lim_x_shift] = xl[B];
      int ( &xrB )[bound_N+1][lim_x_shift] = xr[B];
      FOREQ( p , 0 , B_card ){
	const vector<bool>& xBp = xB[p];
	int ( &xlBp )[lim_x_shift] = xlB[p];
	int y_prev = lim_x_shift - 1;
	FOREQINV( y , lim_x_shift - 1 , 0 ){
	  if( xBp[y] ){
	    FOREQINV( z , y_prev , y ){
	      xlBp[z] = y - bound_x;
	    }
	    y_prev = y - 1;
	  }
	}
	FOREQINV( z , y_prev , 0 ){
	  xlBp[z] = bound_x + 1;
	}
	int ( &xrBp )[lim_x_shift] = xrB[p];
	y_prev = 0;
	FOR( y , 0 , lim_x_shift ){
	  if( xBp[y] ){
	    FOR( z , y_prev , y ){
	      xrBp[z] = y - bound_x;
	    }
	    y_prev = y + 1;
	  }
	}
	FOR( z , y_prev , lim_x_shift ){
	  xrBp[z] = bound_x + 1;
	}
      }
    }
  }
};

int main()
{
  UNTIE;
  CEXPR( int , bound_T , 6000 );
  CIN_ASSERT( T , 1 , bound_T );
  CEXPR( int , bound_Pl , 100000000 );
  CEXPR( int , bound_Pr , 1000000000 );
  CEXPR( ll , bound_Ai , 1000000000 );
  CEXPR( ll , bound_evenness , ll( 1 ) << 62 );
  constexpr Card card{};
  static Xlr xlr( card.m_val , X( card.m_val ).m_val );
  int ( &xl )[lim_B][bound_N+1][lim_x_shift] = xlr.m_val[0];
  int ( &xr )[lim_B][bound_N+1][lim_x_shift] = xlr.m_val[1];
  ll answer;
  pair<ll,ll> key;
  ll& d = key.second;
  map<pair<ll,ll>,ll> memory{};
  CEXPR( int , bound_N_full , 300 );  
  CEXPR( ll , two , 2 );  
  REPEAT( T ){
    CIN_ASSERT( N , 1 , bound_N_full );
    CIN_ASSERT( P , bound_Pl , bound_Pr );
    CIN_ASSERT( A0 , 1 , bound_Ai );
    CIN_ASSERT( A1 , 1 , bound_Ai );
    d = A1 - A0;
    FOR( i , 2 , N ){
      cin >> A1;
    }
    if( d == 0 ){
      if( N == 2 ){
	answer = 2;
      } else {
	POWER_MOD( power , two , N - 1 , P );
	answer = power;
      }
      answer *= A0;
    } else {
      answer = 0;
      if( d < 0 ){
	d *= -1;
	A0 -= d * ( N - 1 );
      }
      key.first = A0;
      if( memory.count( key ) == 1 ){
	answer = memory[key];
      } else {
	int power_N = 1 << N;
	FOR( B , 1 , power_N ){
	  int ( &xlB )[bound_N+1][lim_x_shift] = xl[B];
	  int ( &xrB )[bound_N+1][lim_x_shift] = xr[B];
	  const int& B_card = card.m_val[B];
	  ll evenness = bound_evenness;
	  ll A0_factor = B_card * A0;
	  ll A02 = A0 << 1;
	  int p = 0;
	  int B_card_non_negative = A0_factor / A02 + 1;
	  B_card_non_negative > B_card ? B_card_non_negative = B_card : B_card_non_negative;
	  while( p < B_card_non_negative ){
	    ll y = A0_factor / d + bound_x;
	    y >= lim_x_shift ? y = lim_x_shift - 1 : y < 0 ? y = 0 : y;
	    int ( &xlBp )[lim_x_shift] = xlB[p];
	    int& yl = xlBp[y];
	    if( yl <= bound_x ){
	      ll evenness_curr = -A0_factor + yl * d ;
	      evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr;
	      evenness > evenness_curr ? evenness = evenness_curr : evenness;
	    }
	    int ( &xrBp )[lim_x_shift] = xrB[p];
	    int& yr = xrBp[y];
	    if( yr <= bound_x ){
	      ll evenness_curr = -A0_factor + yr * d ;
	      evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr;
	      evenness > evenness_curr ? evenness = evenness_curr : evenness;
	    }
	    A0_factor -= A02;
	    p++;
	  }
	  while( p < B_card ){
	    ll y = A0_factor / d - ( A0_factor % d != 0 ? 1 : 0 ) + bound_x;
	    y >= lim_x_shift ? y = lim_x_shift - 1 : y < 0 ? y = 0 : y;
	    int ( &xlBp )[lim_x_shift] = xlB[p];
	    int& yl = xlBp[y];
	    if( yl <= bound_x ){
	      ll evenness_curr = -A0_factor + yl * d ;
	      evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr;
	      evenness > evenness_curr ? evenness = evenness_curr : evenness;
	    }
	    int ( &xrBp )[lim_x_shift] = xrB[p];
	    int& yr = xrBp[y];
	    if( yr <= bound_x ){
	      ll evenness_curr = -A0_factor + yr * d ;
	      evenness_curr < 0 ? evenness_curr *= -1 : evenness_curr;
	      evenness > evenness_curr ? evenness = evenness_curr : evenness;
	    }
	    A0_factor -= A02;
	    p++;
	  }
	  answer += evenness;
	}
	memory[key] = answer;
      }
    }
    COUT( answer % P );
  }
  QUIT;
}
0