結果

問題 No.3112 区間和係数多項式?
ユーザー 👑 p-adicp-adic
提出日時 2024-03-13 14:31:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,255 ms / 6,000 ms
コード長 25,790 bytes
コンパイル時間 3,785 ms
コンパイル使用メモリ 229,912 KB
実行使用メモリ 343,508 KB
最終ジャッジ日時 2024-03-13 14:32:52
合計ジャッジ時間 40,151 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 12 ms
6,676 KB
testcase_16 AC 3,031 ms
343,508 KB
testcase_17 AC 2,606 ms
343,508 KB
testcase_18 AC 3,149 ms
343,508 KB
testcase_19 AC 2,568 ms
343,508 KB
testcase_20 AC 1,819 ms
343,508 KB
testcase_21 AC 3,255 ms
343,508 KB
testcase_22 AC 1,455 ms
343,508 KB
testcase_23 AC 2,393 ms
343,508 KB
testcase_24 AC 2,988 ms
343,508 KB
testcase_25 AC 2,971 ms
343,508 KB
testcase_26 AC 1,097 ms
343,508 KB
testcase_27 AC 1,125 ms
343,508 KB
testcase_28 AC 577 ms
343,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef INCLUDE_MODE
  #define INCLUDE_MODE
  // #define REACTIVE
  // #define USE_GETLINE
#endif

#ifdef INCLUDE_MAIN

inline void Solve()
{
  // Nの入力受け取り
  CEXPR( int , bound_N , 1e7 );
  CIN_ASSERT( N , 1 , bound_N );

  // Bの入力受け取り
  CEXPR( ll , bound_B , 1e9 );
  CIN_ASSERT( B , 1 , bound_B );
  
  // Qの入力受け取り
  CEXPR( int , bound_Q , 1e6 );
  CIN_ASSERT( Q , 1 , bound_Q );

  // 合同式での四則演算を扱う型の法をBに設定
  QuotientRing<ll>::SetModulo( &B );

  // C_1,D_1の入力受け取りとAの初期値計算
  CEXPR( ll , bound_CD1 , 1e9 );
  CIN_ASSERT( C_1 , 0 , bound_CD1 );
  CIN_ASSERT( D_1 , 0 , bound_CD1 );
  QuotientRing<ll> A_0 = C_1;
  QuotientRing<ll> R_1 = D_1;
  // A_1,...,A_{N-1}を格納
  vector<QuotientRing<ll>> A( N - 1 , A_0 * R_1 );
  FOR( i , 1 , N - 1 ){
    A[i] = A[i-1] * R_1;
  }
  // 抽象セグメント木を法Bの加法演算と(A_1,...,A_{N-1})で初期化
  AbstractSegmentTree segtree{ AdditiveMonoid<QuotientRing<ll>>() , A };

  // C_2,D_2の入力受け取りとi_qの初期値計算
  CEXPR( ll , bound_CD2 , 1e7 );
  CIN_ASSERT( C_2 , 0 , bound_CD2 );
  CIN_ASSERT( D_2 , 0 , bound_CD2 );
  ll i_q = move( C_2 %= N );

  // C_3,D_3の入力受け取りとj_qの初期値計算
  CEXPR( ll , bound_CD3 , 1e7 );
  CIN_ASSERT( C_3 , 0 , bound_CD3 );
  CIN_ASSERT( D_3 , 0 , bound_CD3 );
  ll j_q = move( C_3 %= N );

  // C_4,D_4の入力受け取りとx_q mod Bの初期値計算
  CEXPR( ll , bound_CD4 , 1e9 );
  CIN_ASSERT( C_4 , 0 , bound_CD4 );
  CIN_ASSERT( D_4 , 0 , bound_CD4 );
  QuotientRing<ll> x_q = C_4;
  QuotientRing<ll> D_4_mod_B = D_4;

  // C_5,D_5の入力受け取りとy_q mod Bの初期値計算
  CEXPR( ll , bound_CD5 , 1e9 );
  CIN_ASSERT( C_5 , 0 , bound_CD5 );
  CIN_ASSERT( D_5 , 0 , bound_CD5 );
  QuotientRing<ll> y_q = C_5;
  QuotientRing<ll> D_5_mod_B = D_5;

  REPEAT( Q ){
    // Aのi個目の成分をxに変更
    if( i_q == 0 ){
      A_0 = x_q;
    } else {
      segtree.Set( i_q - 1 , x_q );
    }

    // f(j_q,y_q)を格納する変数
    QuotientRing<ll> fjy{};
    // y_q羃をBで割った余りを格納する変数
    QuotientRing<ll> y_power{ 1 };
    int j = j_q;
    while( j > 0 ){
      int j_next = j - ( j & -j );
      // fjyにy_q羃とAの区間和の積を加算
      fjy += y_power * segtree.IntervalProduct( j_next , j - 1 );
      // y羃を更新
      y_power *= y_q;
      // jを更新
      j = j_next;
    }
    // 最高次の寄与も加算
    fjy += y_power * A_0;

    // 最終的なfjyの値を出力
    const ll& answer = fjy.Represent();
    COUT( answer < 0 ? answer + B : answer );

    // クエリを更新
    ( i_q *= D_2 ) %= N;
    ( j_q *= D_3 ) %= N;
    x_q *= D_4_mod_B;
    y_q *= D_5_mod_B;
  }
}
REPEAT_MAIN(1);

#else // INCLUDE_MAIN

#ifdef INCLUDE_SUB

// グラフ用
template <typename T , template <typename...> typename V> inline auto Get( const V<T>& a ) { return [&]( const int& i ){ return a[i]; }; }

// VVV テンプレート引数用の関数は以下に挿入する。














// AAA テンプレート引数用の関数は以上に挿入する。

#define INCLUDE_MAIN
#include __FILE__

#else // INCLUDE_SUB

#ifdef INCLUDE_LIBRARY

// https://github.com/p-adic/cpp
// VVV ライブラリは以下に挿入する。

template <typename INT>
class QuotientRing
{

protected:
  INT m_n;
  static const INT* g_p_M;

public:
  inline QuotientRing() noexcept;
  inline QuotientRing( const INT& n ) noexcept;
  inline QuotientRing( const QuotientRing<INT>& n ) noexcept;
  inline QuotientRing( QuotientRing<INT>&& n ) noexcept;

  inline QuotientRing<INT>& operator=( QuotientRing<INT> n ) noexcept;
  inline QuotientRing<INT>& operator+=( const QuotientRing<INT>& n ) noexcept;
  // operator<が定義されていても負の数は正に直さず剰余を取ることに注意。
  inline QuotientRing<INT>& operator-=( const QuotientRing<INT>& n ) noexcept;
  inline QuotientRing<INT>& operator*=( const QuotientRing<INT>& n ) noexcept;
  // *g_p_Mが素数でかつnの逆元が存在する場合のみサポート。
  inline QuotientRing<INT>& operator/=( QuotientRing<INT> n );

  // m_nの正負込みの等号。
  inline bool operator==( const QuotientRing<INT>& n ) const noexcept;
  // m_nの正負込みの等号。
  inline bool operator!=( const QuotientRing<INT>& n ) const noexcept;

  inline QuotientRing<INT> operator+( QuotientRing<INT> n1 ) const noexcept;
  inline QuotientRing<INT> operator-() const noexcept;
  inline QuotientRing<INT> operator-( QuotientRing<INT> n1 ) const noexcept;
  inline QuotientRing<INT> operator*( QuotientRing<INT> n1 ) const noexcept;
  // *g_p_Mが素数でかつn1の逆元が存在する場合のみサポート。
  inline QuotientRing<INT> operator/( const QuotientRing<INT>& n1 ) const;
  template <typename T> QuotientRing<INT> operator^( T exponent );

  inline const INT& Represent() const noexcept;
  static inline const INT& GetModulo() noexcept;
  static inline void SetModulo( const INT* const& p_M ) noexcept;

  // *g_p_Mが素数でかつnの逆元が存在する場合のみサポート。
  static QuotientRing<INT> Inverse( const QuotientRing<INT>& n );
  
};

template <typename INT , typename T> inline QuotientRing<INT> Power( const QuotientRing<INT>& n , T exponent );
// *g_p_Mが素数でかつnの逆元が存在する場合のみサポート。
template <typename INT> inline QuotientRing<INT> Inverse( const QuotientRing<INT>& n );

template <typename INT , class Traits> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , QuotientRing<INT>& n );
template <typename INT , class Traits> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const QuotientRing<INT>& n );


template <typename INT> const INT* QuotientRing<INT>::g_p_M = nullptr;
template <typename INT> inline QuotientRing<INT>::QuotientRing() noexcept : m_n() {}
template <typename INT> inline QuotientRing<INT>::QuotientRing( const INT& n ) noexcept : m_n( n % *g_p_M ) {}
template <typename INT> inline QuotientRing<INT>::QuotientRing( const QuotientRing<INT>& n ) noexcept : m_n( n.m_n ) {}
template <typename INT> inline QuotientRing<INT>::QuotientRing( QuotientRing<INT>&& n ) noexcept : m_n( move( n.m_n ) ) {}

template <typename INT> inline QuotientRing<INT>& QuotientRing<INT>::operator=( QuotientRing<INT> n ) noexcept { m_n = move( n.m_n ); return *this; }
template <typename INT> inline QuotientRing<INT>& QuotientRing<INT>::operator+=( const QuotientRing<INT>& n ) noexcept { ( m_n += n.m_n ) %= *g_p_M; return *this; }
template <typename INT> inline QuotientRing<INT>& QuotientRing<INT>::operator-=( const QuotientRing<INT>& n ) noexcept { ( m_n -= n.m_n ) %= *g_p_M; return *this; }
template <typename INT> inline QuotientRing<INT>& QuotientRing<INT>::operator*=( const QuotientRing<INT>& n ) noexcept { ( m_n *= n.m_n ) %= *g_p_M; return *this; }
template <typename INT> inline QuotientRing<INT>& QuotientRing<INT>::operator/=( QuotientRing<INT> n ) { return *this *= Inverse( n ); }

template <typename INT> inline bool QuotientRing<INT>::operator==( const QuotientRing<INT>& n ) const noexcept { return m_n == n.m_n; }
template <typename INT> inline bool QuotientRing<INT>::operator!=( const QuotientRing<INT>& n ) const noexcept { return !( *this == n ); }

template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::operator+( QuotientRing<INT> n ) const noexcept { return move( n += *this ); }
template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::operator-() const noexcept { QuotientRing<INT> answer{}; answer.m_n = -answer.m_n; return answer; }
template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::operator-( QuotientRing<INT> n ) const noexcept { return n -= *this; }
template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::operator*( QuotientRing<INT> n ) const noexcept { return n *= *this; }
template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::operator/( const QuotientRing<INT>& n ) const { return Inverse( n ) *= *this; }

template <typename INT> template <typename T>
QuotientRing<INT> QuotientRing<INT>::operator^( T exponent )
{

  QuotientRing<INT> answer{ 1 };
  QuotientRing<INT> power{ *this };
  exponent < 0 ? ( exponent = -exponent , power = Inverse( power ) ) : power;

  while( exponent > 0 ){

    if( ( exponent & 2 ) == 1 ){

      answer *= power;

    }

    power *= power;
    exponent >>= 1;

  }

  return answer;

}

template <typename INT> inline const INT& QuotientRing<INT>::Represent() const noexcept { return m_n; }
template <typename INT> inline const INT& QuotientRing<INT>::GetModulo() noexcept { ssert( g_p_M != nullptr ); return *g_p_M; }
template <typename INT> inline void QuotientRing<INT>::SetModulo( const INT* const& p_M ) noexcept { assert( ( g_p_M = p_M ) != nullptr ); }

template <typename INT> inline QuotientRing<INT> QuotientRing<INT>::Inverse( const QuotientRing<INT>& n ) { return n ^ ( *g_p_M - 2 ); }

template <typename INT , typename T> inline QuotientRing<INT> Power( const QuotientRing<INT>& n , T exponent ) { return QuotientRing<INT>::template Power<T>( n , exponent ); }
template <typename INT> inline QuotientRing<INT> Inverse( const QuotientRing<INT>& n ) { return QuotientRing<INT>::Inverse( n ); }

template <typename INT , class Traits> inline basic_istream<char,Traits>& operator>>( basic_istream<char,Traits>& is , QuotientRing<INT>& n ) { INT m; is >> m; n = m; return is; }
template <typename INT , class Traits> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const QuotientRing<INT>& n ) { return os << n.Represent(); }


#define DECLARATION_OF_CPOINT( POINT ) inline const U& POINT() const noexcept
#define DECLARATION_OF_POINT( POINT ) inline U& POINT() noexcept
#define DEFINITION_OF_CPOINT( POINT ) template <typename U> inline const U& VirtualPointedSet<U>::POINT() const noexcept { return Point(); }
#define DEFINITION_OF_POINT( POINT ) template <typename U> inline U& VirtualPointedSet<U>::POINT() noexcept { return Point(); }

// - インタフェースをなるべく抽象型で与えて仮想継承する。
//   - 具体的構造が2種類以上あるものはもちろん抽象型を仮想継承する。
//   - VirtualPointedSetのように具体的構造が1種類しかないものも仮想継承のコンストラクタ呼び出しを
//     省略するためになるべく抽象型を用意する。
//   - AbstractDijkstraのように全ての具体的構造が1つの具体的構造の派生である場合は
//     インタフェースを必要としない。
// - コンストラクタはなるべく省略できるようにするが、ポインタはメンバ変数にしない。
//   - VirtualGraphのように具体的構造が2種類以上あるもので全てに共通の定義本体を持つ関数(Edge)が
//     必要な場合は実装が膨れ上がらないように抽象型に関数の定義をし、そのため抽象型にメンバ変数が
//     必要になる場合はコンストラクタを非自明なものにする
//   - 代わりにポインタを抽象型のメンバとして
//     派生クラスのコンストラクタの定義内でアドレスを渡すようにすると、ムーブなどで意図せず
//     ポインタの指すアドレスが意図通りでなくなることに注意する。
template <typename U>
class UnderlyingSet
{

public:
  using type = U;

};

template <typename U>
class VirtualPointedSet :
  virtual public UnderlyingSet<U>
{

public:
  virtual const U& Point() const noexcept = 0;
  virtual U& Point() noexcept = 0;
  DECLARATION_OF_CPOINT( Unit );
  DECLARATION_OF_CPOINT( Zero );
  DECLARATION_OF_CPOINT( One );
  DECLARATION_OF_CPOINT( Infty );
  DECLARATION_OF_POINT( init );
  DECLARATION_OF_POINT( root );

};

template <typename U>
class PointedSet :
  virtual public VirtualPointedSet<U>
{

private:
U m_b_U;

public:
  inline PointedSet( const U& b_u = U() );
  inline const U& Point() const noexcept;
  inline U& Point() noexcept;

};

template <typename U>
class VirtualNSet :
  virtual public UnderlyingSet<U>
{

public:
  virtual U Transfer( const U& u ) = 0;
  inline U Inverse( const U& u );

};

template <typename U , typename F_U>
class AbstractNSet :
  virtual public VirtualNSet<U>
{

private:
  F_U& m_f_U;

public:
  inline AbstractNSet( F_U& f_U );
  inline U Transfer( const U& u );

};

template <typename U>
class VirtualMagma :
  virtual public UnderlyingSet<U>
{

public:
  virtual U Product( const U& u0 , const U& u1 ) = 0;
  inline U Sum( const U& u0 , const U& u1 );

};

template <typename U = ll>
class AdditiveMagma :
  virtual public VirtualMagma<U>
{

public:
  inline U Product( const U& u0 , const U& u1 );

};

template <typename U = ll>
class MultiplicativeMagma :
  virtual public VirtualMagma<U>
{

public:
  inline U Product( const U& u0 , const U& u1 );

};

template <typename U , typename M_U>
class AbstractMagma :
  virtual public VirtualMagma<U>
{

private:
  M_U& m_m_U;

public:
  inline AbstractMagma( M_U& m_U );
  inline U Product( const U& u0 , const U& u1 );

};

template <typename U> inline PointedSet<U>::PointedSet( const U& b_U ) : m_b_U( b_U ) {}
template <typename U> inline const U& PointedSet<U>::Point() const noexcept { return m_b_U; }
template <typename U> inline U& PointedSet<U>::Point() noexcept { return m_b_U; }
DEFINITION_OF_CPOINT( Unit );
DEFINITION_OF_CPOINT( Zero );
DEFINITION_OF_CPOINT( One );
DEFINITION_OF_CPOINT( Infty );
DEFINITION_OF_POINT( init );
DEFINITION_OF_POINT( root );

template <typename U , typename F_U> inline AbstractNSet<U,F_U>::AbstractNSet( F_U& f_U ) : m_f_U( f_U ) { static_assert( is_invocable_r_v<U,F_U,U> ); }
template <typename U , typename F_U> inline U AbstractNSet<U,F_U>::Transfer( const U& u ) { return m_f_U( u ); }
template <typename U> inline U VirtualNSet<U>::Inverse( const U& u ) { return Transfer( u ); }

template <typename U , typename M_U> inline AbstractMagma<U,M_U>::AbstractMagma( M_U& m_U ) : m_m_U( m_U ) { static_assert( is_invocable_r_v<U,M_U,U,U> ); }


template <typename U> inline U AdditiveMagma<U>::Product( const U& u0 , const U& u1 ) { return u0 + u1; }
template <typename U> inline U MultiplicativeMagma<U>::Product( const U& u0 , const U& u1 ) { return u0 * u1; }
template <typename U , typename M_U> inline U AbstractMagma<U,M_U>::Product( const U& u0 , const U& u1 ) { return m_m_U( u0 , u1 ); }
template <typename U> inline U VirtualMagma<U>::Sum( const U& u0 , const U& u1 ) { return Product( u0 , u1 ); }

template <typename U>
class VirtualMonoid :
  virtual public VirtualMagma<U> ,
  virtual public VirtualPointedSet<U>
{};

template <typename U = ll>
class AdditiveMonoid :
  virtual public VirtualMonoid<U> ,
  public AdditiveMagma<U> ,
  public PointedSet<U>
{};

template <typename U = ll>
class MultiplicativeMonoid :
  virtual public VirtualMonoid<U> ,
  public MultiplicativeMagma<U> ,
  public PointedSet<U>
{

public:
  inline MultiplicativeMonoid( const U& e_U );

};

template <typename U , typename M_U>
class AbstractMonoid :
  virtual public VirtualMonoid<U> ,
  public AbstractMagma<U,M_U> ,
  public PointedSet<U>
{

public:
  inline AbstractMonoid( M_U& m_U , const U& e_U );

};

template <typename U> inline MultiplicativeMonoid<U>::MultiplicativeMonoid( const U& e_U ) : PointedSet<U>( e_U ) {}
template <typename U , typename M_U> inline AbstractMonoid<U,M_U>::AbstractMonoid( M_U& m_U , const U& e_U ) : AbstractMagma<U,M_U>( m_U ) , PointedSet<U>( e_U ) {}

// M.One()による初期化O(size)
// 配列による初期化O(N)

// 一点更新O(log_2 N)

// 一点取得O(1)
// 区間積取得O(log_2 N)
template <typename U , typename MONOID>
class AbstractSegmentTree
{
private:
  int m_size;
  int m_power;
  MONOID m_M;
  vector<U> m_a;

public:
  inline AbstractSegmentTree( MONOID M );
  inline AbstractSegmentTree( MONOID M , const vector<U>& a );

  inline void Set( const vector<U>& a );
  void Set( const int& i , const U& u );

  inline const U& operator[]( const int& i ) const;
  inline const U& Get( const int& i ) const;
  U IntervalProduct( const int& i_start , const int& i_final );

};
template <typename MONOID> AbstractSegmentTree( MONOID M ) -> AbstractSegmentTree<inner_t<MONOID>,MONOID>;

template <typename U>
class SegmentTree :
  public AbstractSegmentTree<U,MultiplicativeMonoid<U>>
{

public:
  template <typename...Args> inline SegmentTree( const U& one_U , const Args&... args );

};

template <typename U , typename MONOID> inline AbstractSegmentTree<U,MONOID>::AbstractSegmentTree( MONOID M ) : AbstractSegmentTree( move( M ) , vector<U>() ) {}

template <typename U , typename MONOID> inline AbstractSegmentTree<U,MONOID>::AbstractSegmentTree( MONOID M , const vector<U>& a ) : m_size( a.size() ) , m_power( 1 ) , m_M( move( M ) ) , m_a()
{

  static_assert( is_same_v<U,inner_t<MONOID>> );
  while( m_size > m_power ){

    m_power <<= 1;

  }

  m_a.resize( m_power << 1 , m_M.One() );
  
  for( int i = 0 ; i < m_size ; i++ ){

    m_a[m_power | i] = a[i];

  }

  for( int j = m_power - 1 ; j >= 1 ; j-- ){

    int j2 = j << 1;
    m_a[j] = m_M.Product( m_a[j2] , m_a[j2+1] );

  }

}

template <typename U> template <typename...Args> inline SegmentTree<U>::SegmentTree( const U& one_U , const Args&... args ) : AbstractSegmentTree<U,MultiplicativeMonoid<U>>( MultiplicativeMonoid<U>( one_U ) , args... ) {}

template <typename U , typename MONOID> inline void AbstractSegmentTree<U,MONOID>::Set( const vector<U>& a ) { *this = AbstractSegmentTree( move( m_M ) , a ); }

template <typename U , typename MONOID>
void AbstractSegmentTree<U,MONOID>::Set( const int& i , const U& u )
{

  assert( 0 <= i && i < m_size );
  int j = m_power | i;
  m_a[j] = u;
  
  while( ( j >>= 1 ) >= 1 ){
    
    int j2 = j << 1;
    m_a[j] = m_M.Product( m_a[j2] , m_a[j2+1] );

  }

  return;

}

template <typename U , typename MONOID> inline const U& AbstractSegmentTree<U,MONOID>::operator[]( const int& i ) const { assert( 0 <= i && i < m_size ); return m_a[m_power + i]; }
template <typename U , typename MONOID> inline const U& AbstractSegmentTree<U,MONOID>::Get( const int& i ) const { return operator[]( i ); }

template <typename U , typename MONOID>
U AbstractSegmentTree<U,MONOID>::IntervalProduct( const int& i_start , const int& i_final ) 
{

  int j_min = m_power | max( 0 , i_start );
  int j_ulim = m_power + min( i_final + 1 , m_size );
  U answer0 = m_M.One();
  U answer1 = answer0;

  while( j_min < j_ulim ){

    ( j_min & 1 ) == 1 ? answer0 = m_M.Product( answer0 , m_a[j_min++] ) : answer0;
    ( j_ulim & 1 ) == 1 ? answer1 = m_M.Product( m_a[--j_ulim] , answer1 ) : answer1;
    j_min >>= 1;
    j_ulim >>= 1;

  }

  return m_M.Product( answer0 , answer1 );

}

// AAA ライブラリは以上に挿入する。

#define INCLUDE_SUB
#include __FILE__

#else // INCLUDE_LIBRARY

#ifdef DEBUG
  #define _GLIBCXX_DEBUG
  #define SIGNAL signal( SIGABRT , &AlertAbort );
  #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , DEBUG_VALUE )
  #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) )
  #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl
  #define COUT( ... ) VariadicCout( cout << "出力: " , __VA_ARGS__ ) << endl
  #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl
  #define COUT_A( A , N ) cout << "出力: "; OUTPUT_ARRAY( cout , A , N ) << endl
  #define CERR_ITR( A ) OUTPUT_ITR( cerr , A ) << endl
  #define COUT_ITR( A ) cout << "出力: "; OUTPUT_ITR( cout , A ) << endl
#else
  #pragma GCC optimize ( "O3" )
  #pragma GCC optimize ( "unroll-loops" )
  #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" )
  #define SIGNAL 
  #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE )
  #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) )
  #define CERR( ... ) 
  #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL
  #define CERR_A( A , N ) 
  #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL
  #define CERR_ITR( A ) 
  #define COUT_ITR( A ) OUTPUT_ITR( cout , A ) << ENDL
#endif
#ifdef REACTIVE
  #define ENDL endl
#else
  #define ENDL "\n"
#endif
#ifdef USE_GETLINE
  #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A ## _str ); }
  #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ )
  #define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ )
#else
  #define SET_LL( A ) cin >> A
  #define CIN( LL , ... ) LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ )
  #define SET_A( A , N ) FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; }
  #define CIN_A( LL , A , N ) vector<LL> A( N ); SET_A( A , N );
#endif
#include <bits/stdc++.h>
using namespace std;
#define REPEAT_MAIN( BOUND ) int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } }
#define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now()
#define CURRENT_TIME static_cast<double>( chrono::duration_cast<chrono::microseconds>( chrono::system_clock::now() - watch ).count() / 1000.0 )
#define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 )
#define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE
#define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX )
#define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX )
#define FOR( VAR , INITIAL , FINAL_PLUS_ONE ) for( decldecay_t( FINAL_PLUS_ONE ) VAR = INITIAL ; VAR < FINAL_PLUS_ONE ; VAR ++ )
#define FOREQ( VAR , INITIAL , FINAL ) for( decldecay_t( FINAL ) VAR = INITIAL ; VAR <= FINAL ; VAR ++ )
#define FOREQINV( VAR , INITIAL , FINAL ) for( decldecay_t( INITIAL ) VAR = INITIAL ; VAR + 1 > 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 OUTPUT_ARRAY( OS , A , N ) FOR( VARIABLE_FOR_OUTPUT_ARRAY , 0 , N ){ OS << A[VARIABLE_FOR_OUTPUT_ARRAY] << (VARIABLE_FOR_OUTPUT_ARRAY==N-1?"":" "); } OS
#define OUTPUT_ITR( OS , A ) { auto ITERATOR_FOR_OUTPUT_ITR = A.begin() , END_FOR_OUTPUT_ITR = A.end(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; while( VARIABLE_FOR_OUTPUT_ITR ){ OS << *ITERATOR_FOR_COUT_ITR; ( VARIABLE_FOR_OUTPUT_ITR = ++ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR ) ? OS : OS << " "; } } OS
#define RETURN( ... ) COUT( __VA_ARGS__ ); return

// 型のエイリアス
#define decldecay_t( VAR ) decay_t<decltype( VAR )>
template <typename F , typename...Args> using ret_t = decltype( declval<F>()( declval<Args>()... ) );
template <typename T> using inner_t = typename T::type;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using lld = __float128;
template <typename INT> using T2 = pair<INT,INT>;
template <typename INT> using T3 = tuple<INT,INT,INT>;
template <typename INT> using T4 = tuple<INT,INT,INT,INT>;

// 入出力用
template <class Traits> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is ) { return is; }
template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicCin( basic_istream<char,Traits>& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); }
template <class Traits> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , const char& separator ) { return is; }
template <class Traits , typename Arg , typename... ARGS> inline basic_istream<char,Traits>& VariadicGetline( basic_istream<char,Traits>& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); }
template <class Traits , typename Arg> inline basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os , const vector<Arg>& arg ) { auto begin = arg.begin() , end = arg.end(); auto itr = begin; while( itr != end ){ ( itr == begin ? os : os << " " ) << *itr; itr++; } return os; }
template <class Traits , typename Arg> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , const Arg& arg ) { return os << arg; }
template <class Traits , typename Arg1 , typename Arg2 , typename... ARGS> inline basic_ostream<char,Traits>& VariadicCout( basic_ostream<char,Traits>& os , const Arg1& arg1 , const Arg2& arg2 , const ARGS&... args ) { return VariadicCout( os << arg1 << " " , arg2 , args... ); }

// デバッグ用
#ifdef DEBUG
  inline void AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); }
  void AutoCheck( bool& auto_checked );
#endif

#define INCLUDE_LIBRARY
#include __FILE__

#endif // INCLUDE_LIBRARY

#endif // INCLUDE_SUB

#endif // INCLUDE_MAIN
0