#ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE // #define USE_GETLINE #endif #ifdef INCLUDE_MAIN inline void Solve() { CEXPR( ll , bound_N , 1e5 ); CIN_ASSERT( N , 1 , bound_N ); CEXPR( ll , bound_P , 1e9 ); CIN_ASSERT( P , 1 , bound_P ); CIN_ASSERT( C , 0 , P - 1 ); using ModP = QuotientRing; ModP::SetModulo( &P ); vector> A( 1 ); FOR( i , 0 , N ){ CIN_ASSERT( Ai , 0 , P - 1 ); if( Ai == 0 ){ A.push_back( {} ); } else { A.back().push_back( Ai ); } } ll answer = 0; if( C == 0 ){ FOR_ITR( A ){ ll size = itr->size(); answer += size * ( size + 1 ) / 2; } answer = N * ( N + 1 ) / 2 - answer; } else { FOR_ITR( A ){ AbstractCumulativeProduct cp{ AbstractGroup( Multiplication , ModP( 1 ) , Id ) , *itr }; answer += cp.CountRightIntervalProductInverseImage( C ); } } RETURN( answer ); } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_LIBRARY // https://github.com/p-adic/cpp // VVV ライブラリは以下に挿入する。 #define DECLARATION_OF_COMPARISON_FOR_QUOTIENT( OPR ) \ inline constexpr bool operator OPR( const QuotientRing& n ) const noexcept \ #define DEFINITION_OF_COMPARISON_FOR_QUOTIENT( OPR ) \ template inline constexpr bool QuotientRing::operator OPR( const QuotientRing& n ) const noexcept { return m_n OPR n.m_n; } \ // インスタンスごとに異なる法を定めたい場合は // MultiBase/a.hpp // のクラスを用いる。 template 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& n ) noexcept; inline QuotientRing( QuotientRing&& n ) noexcept; inline QuotientRing& operator=( QuotientRing n ) noexcept; inline QuotientRing& operator+=( const QuotientRing& n ) noexcept; // operator<が定義されていても負の数は正に直さず剰余を取ることに注意。 inline QuotientRing& operator-=( const QuotientRing& n ) noexcept; inline QuotientRing& operator*=( const QuotientRing& n ) noexcept; // *g_p_Mが素数でかつnの逆元が存在する場合のみサポート。 inline QuotientRing& operator/=( QuotientRing n ); // m_nの正負込みの等号/不等号。不等号はsetやmapに必要。 DECLARATION_OF_COMPARISON_FOR_QUOTIENT( == ); DECLARATION_OF_COMPARISON_FOR_QUOTIENT( != ); DECLARATION_OF_COMPARISON_FOR_QUOTIENT( <= ); DECLARATION_OF_COMPARISON_FOR_QUOTIENT( >= ); DECLARATION_OF_COMPARISON_FOR_QUOTIENT( < ); DECLARATION_OF_COMPARISON_FOR_QUOTIENT( > ); inline QuotientRing operator+( QuotientRing n1 ) const noexcept; inline QuotientRing operator-() const noexcept; inline QuotientRing operator-( QuotientRing n1 ) const noexcept; inline QuotientRing operator*( QuotientRing n1 ) const noexcept; // *g_p_Mが素数でかつn1の逆元が存在する場合のみサポート。 inline QuotientRing operator/( const QuotientRing& n1 ) const; template QuotientRing 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 Inverse( const QuotientRing& n ); }; template inline QuotientRing Power( const QuotientRing& n , T exponent ); // *g_p_Mが素数でかつnの逆元が存在する場合のみサポート。 template inline QuotientRing Inverse( const QuotientRing& n ); template inline basic_istream& operator>>( basic_istream& is , QuotientRing& n ); template inline basic_ostream& operator<<( basic_ostream& os , const QuotientRing& n ); template const INT* QuotientRing::g_p_M = nullptr; template inline QuotientRing::QuotientRing() noexcept : m_n() {} template inline QuotientRing::QuotientRing( const INT& n ) noexcept : m_n( n % *g_p_M ) {} template inline QuotientRing::QuotientRing( const QuotientRing& n ) noexcept : m_n( n.m_n ) {} template inline QuotientRing::QuotientRing( QuotientRing&& n ) noexcept : m_n( move( n.m_n ) ) {} template inline QuotientRing& QuotientRing::operator=( QuotientRing n ) noexcept { m_n = move( n.m_n ); return *this; } template inline QuotientRing& QuotientRing::operator+=( const QuotientRing& n ) noexcept { ( m_n += n.m_n ) %= *g_p_M; return *this; } template inline QuotientRing& QuotientRing::operator-=( const QuotientRing& n ) noexcept { ( m_n -= n.m_n ) %= *g_p_M; return *this; } template inline QuotientRing& QuotientRing::operator*=( const QuotientRing& n ) noexcept { ( m_n *= n.m_n ) %= *g_p_M; return *this; } template inline QuotientRing& QuotientRing::operator/=( QuotientRing n ) { return *this *= Inverse( n ); } DEFINITION_OF_COMPARISON_FOR_QUOTIENT( == ); DEFINITION_OF_COMPARISON_FOR_QUOTIENT( != ); DEFINITION_OF_COMPARISON_FOR_QUOTIENT( <= ); DEFINITION_OF_COMPARISON_FOR_QUOTIENT( >= ); DEFINITION_OF_COMPARISON_FOR_QUOTIENT( < ); DEFINITION_OF_COMPARISON_FOR_QUOTIENT( > ); template inline QuotientRing QuotientRing::operator+( QuotientRing n ) const noexcept { return move( n += *this ); } template inline QuotientRing QuotientRing::operator-() const noexcept { QuotientRing answer{}; answer.m_n = -answer.m_n; return answer; } template inline QuotientRing QuotientRing::operator-( QuotientRing n ) const noexcept { return n -= *this; } template inline QuotientRing QuotientRing::operator*( QuotientRing n ) const noexcept { return n *= *this; } template inline QuotientRing QuotientRing::operator/( const QuotientRing& n ) const { return Inverse( n ) *= *this; } template template QuotientRing QuotientRing::operator^( T exponent ) { QuotientRing answer{ 1 }; QuotientRing 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 inline const INT& QuotientRing::Represent() const noexcept { return m_n; } template inline const INT& QuotientRing::GetModulo() noexcept { ssert( g_p_M != nullptr ); return *g_p_M; } template inline void QuotientRing::SetModulo( const INT* const& p_M ) noexcept { assert( ( g_p_M = p_M ) != nullptr ); } template inline QuotientRing QuotientRing::Inverse( const QuotientRing& n ) { return n ^ ( *g_p_M - 2 ); } template inline QuotientRing Power( const QuotientRing& n , T exponent ) { return QuotientRing::template Power( n , exponent ); } template inline QuotientRing Inverse( const QuotientRing& n ) { return QuotientRing::Inverse( n ); } template inline basic_istream& operator>>( basic_istream& is , QuotientRing& n ) { INT m; is >> m; n = m; return is; } template inline basic_ostream& operator<<( basic_ostream& os , const QuotientRing& 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 inline const U& VirtualPointedSet::POINT() const noexcept { return Point(); } #define DEFINITION_OF_POINT( POINT ) template inline U& VirtualPointedSet::POINT() noexcept { return Point(); } // - インタフェースをなるべく抽象型で与えて仮想継承する。 // - 具体的構造が2種類以上あるものはもちろん抽象型を仮想継承する。 // - VirtualPointedSetのように具体的構造が1種類しかないものも仮想継承のコンストラクタ呼び出しを // 省略するためになるべく抽象型を用意する。 // - AbstractDijkstraのように全ての具体的構造が1つの具体的構造の派生である場合は // インタフェースを必要としない。 // - コンストラクタはなるべく省略できるようにするが、ポインタはメンバ変数にしない。 // - VirtualGraphのように具体的構造が2種類以上あるもので全てに共通の定義本体を持つ関数(Edge)が // 必要な場合は実装が膨れ上がらないように抽象型に関数の定義をし、そのため抽象型にメンバ変数が // 必要になる場合はコンストラクタを非自明なものにする // - 代わりにポインタを抽象型のメンバとして // 派生クラスのコンストラクタの定義内でアドレスを渡すようにすると、ムーブなどで意図せず // ポインタの指すアドレスが意図通りでなくなることに注意する。 // - データ構造に渡すことを想定する。 // - データ構造の配列や初期化をムーブセマンティクスで処理できるようにするために // 代数構造もムーブコンストラクタがdeleteされないようにする。 // - そのために演算に対応する関数オブジェクトは参照ではなく実体としてメンバに持つ。 template class UnderlyingSet { public: using type = U; }; template class VirtualPointedSet : virtual public UnderlyingSet { 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 class PointedSet : virtual public VirtualPointedSet { private: U m_b_U; public: inline PointedSet( const U& b_u = U() ); inline const U& Point() const noexcept; inline U& Point() noexcept; }; template class VirtualNSet : virtual public UnderlyingSet { public: virtual U Transfer( const U& u ) = 0; inline U Inverse( const U& u ); }; template class AbstractNSet : virtual public VirtualNSet { private: F_U m_f_U; public: inline AbstractNSet( F_U f_U ); inline U Transfer( const U& u ); }; template class VirtualMagma : virtual public UnderlyingSet { public: virtual U Product( const U& u0 , const U& u1 ) = 0; inline U Sum( const U& u0 , const U& u1 ); }; template class AdditiveMagma : virtual public VirtualMagma { public: inline U Product( const U& u0 , const U& u1 ); }; template class MultiplicativeMagma : virtual public VirtualMagma { public: inline U Product( const U& u0 , const U& u1 ); }; template class AbstractMagma : virtual public VirtualMagma { private: M_U m_m_U; public: inline AbstractMagma( M_U m_U ); inline U Product( const U& u0 , const U& u1 ); }; template inline PointedSet::PointedSet( const U& b_U ) : m_b_U( b_U ) {} template inline const U& PointedSet::Point() const noexcept { return m_b_U; } template inline U& PointedSet::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 inline AbstractNSet::AbstractNSet( F_U f_U ) : m_f_U( move( f_U ) ) { static_assert( is_invocable_r_v ); } template inline U AbstractNSet::Transfer( const U& u ) { return m_f_U( u ); } template inline U VirtualNSet::Inverse( const U& u ) { return Transfer( u ); } template inline AbstractMagma::AbstractMagma( M_U m_U ) : m_m_U( move( m_U ) ) { static_assert( is_invocable_r_v ); } template inline U AdditiveMagma::Product( const U& u0 , const U& u1 ) { return u0 + u1; } template inline U MultiplicativeMagma::Product( const U& u0 , const U& u1 ) { return u0 * u1; } template inline U AbstractMagma::Product( const U& u0 , const U& u1 ) { return m_m_U( u0 , u1 ); } template inline U VirtualMagma::Sum( const U& u0 , const U& u1 ) { return Product( u0 , u1 ); } template class VirtualMonoid : virtual public VirtualMagma , virtual public VirtualPointedSet {}; template class AdditiveMonoid : virtual public VirtualMonoid , public AdditiveMagma , public PointedSet {}; template class MultiplicativeMonoid : virtual public VirtualMonoid , public MultiplicativeMagma , public PointedSet { public: inline MultiplicativeMonoid( const U& e_U ); }; template class AbstractMonoid : virtual public VirtualMonoid , public AbstractMagma , public PointedSet { public: inline AbstractMonoid( M_U m_U , const U& e_U ); }; template inline MultiplicativeMonoid::MultiplicativeMonoid( const U& e_U ) : PointedSet( e_U ) {} template inline AbstractMonoid::AbstractMonoid( M_U m_U , const U& e_U ) : AbstractMagma( move( m_U ) ) , PointedSet( e_U ) {} template class VirtualGroup : virtual public VirtualMonoid , virtual public VirtualPointedSet , virtual public VirtualNSet {}; template class AdditiveGroup : virtual public VirtualGroup , public AdditiveMonoid { public: inline U Transfer( const U& u ); }; template class AbstractGroup : virtual public VirtualGroup , public AbstractMonoid , public AbstractNSet { public: inline AbstractGroup( M_U m_U , const U& e_U , I_U i_U ); }; template inline AbstractGroup::AbstractGroup( M_U m_U , const U& e_U , I_U i_U ) : AbstractMonoid( move( m_U ) , e_U ) , AbstractNSet( move( i_U ) ) {} template inline U AdditiveGroup::Transfer( const U& u ) { return -u; } // static_assertではU=llの時にintが渡された時にコンパイルエラーとなる。 #define SFINAE_FOR_CP_BS enable_if_t>* // 木上で群に値を持つ関数の累積積を計算する。 template class VirtualCumulativeProduct { public: // 0 <= i,j < m_sizeの場合のみサポート。 // iからへのpathがi=v_0->...->v_k=jの時、初期化に用いた配列aに対する // 右総乗a[v_0]...a[v_k]を計算する。 virtual U PathProduct( const int& i , const int& j ) = 0; protected: virtual int Parent( const int& i ) = 0; virtual int LCA( const int& i , const int& j ) = 0; }; template class PathProductImplementation : virtual public VirtualCumulativeProduct { protected: GROUP m_M; int m_size; vector m_right; vector m_left; public: inline PathProductImplementation( GROUP M , const int& size ); inline U PathProduct( const int& i_start , const int& i_final ); }; // 木が特に通常の配列である場合。 // 入力の範囲内で要件 // (1) MがUの群構造である。 // が成り立つ場合にのみサポート。 // ただし区間積か区間積の二分探索を用いない場合はモノイド構造でよい。 // M.one()による初期化O(size)(モノイド構造を使う) // 配列による初期化O(size)(半群構造を使う) // 一点代入O(size)(半群構造を使う) // 一点右乗算O(size)(半群構造を使う) // 一点左乗算O(size)(半群構造を使う) // 右始切片積取得O(1)(モノイド構造を使う) // 左始切片積取得O(1)(モノイド構造を使う) // 右区間積取得O(1)(群構造を使う) // 左区間積取得O(1)(群構造を使う) // 右区間積逆像数え上げO(size log size)(半群構造を使う) // 左区間積逆像数え上げO(size log size)(半群構造を使う) // 以下は入力の範囲内で要件 // (2) operator<(const U&,const U&)に関してMがUの全順序群構造である。 // (3) 各成分がM.One()より小さくない。 // を満たす場合にのみサポート。 // 左始切片積がu以上となる要素の添字の最小値の二分探索O(log_2 size)(全順序モノイド構造を使う) // 右始切片積がu以上となる要素の添字の最小値の二分探索O(log_2 size)(全順序モノイド構造を使う) // 左区間積がu以上となる要素の添字の最小値の二分探索O(log_2 size)(全順序群構造を使う) // 右区間積がu以上となる要素の添字の最小値の二分探索O(log_2 size)(全順序群構造を使う) template class AbstractCumulativeProduct : public PathProductImplementation { private: vector m_a; public: inline AbstractCumulativeProduct( GROUP M , const int& size = 0 ); inline AbstractCumulativeProduct( GROUP M , vector a ); inline void Reset( const vector& a ); // a[i]をuに置き変える。 inline void Set( const int& i , const U& u ); // a[i]をM.Product(a[i],u)に置き変える。 inline void RightMultiply( const int& i , const U& u ); // a[i]をM.Product(u,a[i])に置き変える。 inline void LeftMultiply( const int& i , const U& u ); // 0 <= i_final < m_sizeの場合のみサポート。 // 右始切片積a[0]...a[i_final]をMに関して計算する。 inline const U& RightInitialSegmentProduct( int i_final ); // 左始切片積a[i_final]...a[0]をMに関して計算する。 inline const U& LeftInitialSegmentProduct( int i_final ); // 0 <= i_startかつi_start-1 <= i_final < m_sizeの場合のみサポート。 // 右区間積a[i_start]...a[i_final]をMに関して計算する。 inline U RightIntervalProduct( const int& i_start , int i_final ); // 左区間積a[i_final]...a[i_start]をMに関して計算する。 inline U LeftIntervalProduct( const int& i_start , int i_final ); // Mに関する右区間積a[i]...a[j]がuと等しい区間[i,j]の個数を計算する。 ll CountRightIntervalProductInverseImage( const U& u ); // Mに関する左区間積a[j]...a[i]がuと等しい区間[i,j]の個数を計算する。 ll CountLeftIntervalProductInverseImage( const U& u ); // Fは積順序に関して単調な写像f:U \times int -> {0,1}に相当する型。 // f( RightInitialSegmentProduct( i ) , i )がtrueとなるi_start以上のiが // 存在する場合にその最小値を2進法で探索。存在しない場合はNを返す。 template int RightBinarySearch( const F& f ); // f( LeftInitialSegmentProduct( i ) , i )がtrueとなるi_start以上のiが // 存在する場合にその最小値を2進法で探索。存在しない場合はNを返す。 template int LeftBinarySearch( const F& f ); // f( RightIntervalProduct( i_start , i ) , i )がtrueとなるi_start以上のiが // 存在する場合にその最小値を2進法で探索。存在しない場合はNを返す。 template int RightBinarySearch( const int& i_start , const F& f ); // f( LeftIntervalProduct( i_start , i ) , i )がtrueとなるi_start以上のiが // 存在する場合にその最小値を2進法で探索。存在しない場合はNを返す。 template int LeftBinarySearch( const int& i_start , const F& f ); // RightInitialSegmentProduct( i )がu以上となるiが存在する場合にその最小値を2進法で探索。 // 存在しない場合はNを返す。 int RightBinarySearch( const U& u ); // LeftInitialSegmentProduct( i )がu以上となるiが存在する場合にその最小値を2進法で探索。 // 存在しない場合はNを返す。 int LeftBinarySearch( const U& u ); // RightIntervalProduct( i_start , i )がu以上となるi_start以上のiが存在する場合に // その最小値を2進法で探索。存在しない場合はNを返す。 int RightBinarySearch( const int& i_start , const U& u ); // LeftIntervalProduct( i_start , i )がu以上となるi_start以上のiが存在する場合に // その最小値を2進法で探索。存在しない場合はNを返す。 int LeftBinarySearch( const int& i_start , const U& u ); private: inline int Parent( const int& i ); inline int LCA( const int& i , const int& j ); }; template AbstractCumulativeProduct( GROUP M , const Args&... args ) -> AbstractCumulativeProduct,GROUP>; template class CumulativeSum : public AbstractCumulativeProduct> { public: inline CumulativeSum( const int& size = 0 ); inline CumulativeSum( vector a ); // a[i]をM.Sum(u,a[i])に置き変える。 inline void Add( const int& i , const U& u ); // 始切片和a[0]+...+a[i_final]を計算する。 inline const U& InitialSegmentSum( int i_final ); // 区間和a[i_start]+...+a[i_final]を計算する。 inline U IntervalSum( const int& i_start , int i_final ); // 区間和a[i]+...+a[j]がuと等しい区間[i,j]の個数を計算する。 ll CountIntervalSumInverseImage( const U& u = U() ); template int BinarySearch( const Args&... args ); }; // 乗法群に使いたい状況と乗法モノイドに使いたい状況が同程度であるため // 非AbstractなCumulativeProductは定義せずAbstractを使う。 template inline PathProductImplementation::PathProductImplementation( GROUP M , const int& size ) : m_M( move( M ) ) , m_size( size ) , m_right( m_size , m_M.One() ) , m_left( m_right ) {} template inline AbstractCumulativeProduct::AbstractCumulativeProduct( GROUP M , const int& size ) : AbstractCumulativeProduct( M , vector( size , M.One() ) ) {} template inline AbstractCumulativeProduct::AbstractCumulativeProduct( GROUP M , vector a ) : PathProductImplementation( move( M ) , a.size() ) , m_a( move( a ) ) { if( !m_a.empty() ){ this->m_right[0] = this->m_left[0] = m_a[0]; for( int i = 1 ; i < this->m_size ; i++ ){ this->m_right[i] = this->m_M.Product( this->m_right[i-1] , m_a[i] ); this->m_left[i] = this->m_M.Product( m_a[i] , this->m_left[i-1] ); } } } template inline CumulativeSum::CumulativeSum( const int& size ) : CumulativeSum( vector( size ) ){} template inline CumulativeSum::CumulativeSum( vector a ) : AbstractCumulativeProduct>( AdditiveGroup() , move( a ) ) {} template inline void AbstractCumulativeProduct::Reset( const vector& a ) { *this = AbstractCumulativeProduct( move( this->m_M ) , a ); } template inline void AbstractCumulativeProduct::Set( const int& i , const U& u ) { assert( 0 <= i && i < this->_size ); m_a[i] = u; if( i == 0 ){ this->m_right[0] = this->m_left[0] = m_a[0]; } for( int j = max( i , 1 ) ; j < this->m_size ; j++ ){ this->m_right[j] = this->m_M.Product( this->m_right[j-1] , m_a[j] ); this->m_left[j] = this->m_M.Product( m_a[j] , this->m_left[j-1] ); } } template inline void AbstractCumulativeProduct::RightMultiply( const int& i , const U& u ) { Set( i , this->m_M.Product( m_a[i] , u ) ); } template inline void AbstractCumulativeProduct::LeftMultiply( const int& i , const U& u ) { Set( i , this->m_M.Product( u , m_a[i] ) ); } template inline void CumulativeSum::Add( const int& i , const U& u ) { this->RightMultiply( i , u ); } template inline U PathProductImplementation::PathProduct( const int& i_start , const int& i_final ) { assert( 0 <= i_start && i_start < m_size && 0 <= i_final && i_final < m_size ); const int k = this->LCA( i_start , i_final ); return m_M.Product( m_M.Product( m_left[i_start] , m_M.Inverse( m_left[k] ) ) , k == 0 ? m_right[i_final] : m_M.Product( m_M.Inverse( m_right[this->Parent( k ) ] ) , m_right[i_final] )); } template inline const U& AbstractCumulativeProduct::RightInitialSegmentProduct( int i_final ) { i_final = min( i_final , this->m_size - 1 ); return 0 <= i_final ? this->m_right[i_final] : this->m_M.One(); } template inline const U& AbstractCumulativeProduct::LeftInitialSegmentProduct( int i_final ) { i_final = min( i_final , this->m_size - 1 ); return 0 <= i_final ? this->m_left[i_final] : this->m_M.One();; } template inline const U& CumulativeSum::InitialSegmentSum( int i_final ) { return this->RightInitialSegmentProduct( move( i_final ) ); } template inline U AbstractCumulativeProduct::RightIntervalProduct( const int& i_start , int i_final ) { i_final = min( i_final , this->m_size - 1 ); return i_start <= i_final ? i_start <= 0 ? this->m_right[i_final] : this->m_M.Product( this->m_M.Inverse( this->m_right[i_start-1] ) , this->m_right[i_final] ) : this->m_M.One(); } template inline U AbstractCumulativeProduct::LeftIntervalProduct( const int& i_start , int i_final ) { i_final = min( i_final , this->m_size - 1 ); return i_start <= i_final ? i_start <= 0 ? this->m_left[i_final] : this->m_M.Product( this->m_left[i_final] , this->m_M.Inverse( this->m_left[i_start - 1] ) ) : this->m_M.One(); } template inline U CumulativeSum::IntervalSum( const int& i_start , int i_final ) { return this->RightIntervalProduct( i_start , move( i_final ) ); } template ll AbstractCumulativeProduct::CountRightIntervalProductInverseImage( const U& u ) { // Map f{}; map f{}; f[u]++; ll answer = 0; for( int i = 0 ; i < this->m_size ; i++ ){ const U& m_right_i = this->m_right[i]; f.count( m_right_i ) == 1 ? answer += f[m_right_i] : answer; f[this->m_M.Product( m_right_i , u )]++; } return answer; } template ll AbstractCumulativeProduct::CountLeftIntervalProductInverseImage( const U& u ) { // Map f{}; map f{}; f[u]++; ll answer = 0; for( int i = 0 ; i < this->m_size ; i++ ){ const U& m_left_i = this->m_left[i]; f.count( m_left_i ) == 1 ? answer += f[m_left_i] : answer; f[this->m_M.Product( u , m_left_i )]++; } return answer; } template ll CumulativeSum::CountIntervalSumInverseImage( const U& u ) { return this->CountRightIntervalProductInverseImage( u ); } template template int AbstractCumulativeProduct::RightBinarySearch( const F& f ) { if( f( m_a[0] , 0 ) ){ return 0; } int l = 0; int r = this->m_size; while( l + 1 < r ){ int m = ( l + r ) / 2; ( f( RightInitialSegmentProduct( m ) , m ) ? r : l ) = m; } return r; } template template int AbstractCumulativeProduct::LeftBinarySearch( const F& f ) { if( f( m_a[0] , 0 ) ){ return 0; } int l = 0; int r = this->m_size; while( l + 1 < r ){ int m = ( l + r ) / 2; ( f( LeftInitialSegmentProduct( m ) , m ) ? r : l ) = m; } return r; } template template int AbstractCumulativeProduct::RightBinarySearch( const int& i_start , const F& f ) { if( f( m_a[i_start] , i_start ) ){ return i_start; } int l = i_start; int r = this->m_size; while( l + 1 < r ){ int m = ( l + r ) / 2; ( f( RightIntervalProduct( i_start , m ) , m ) ? r : l ) = m; } return r; } template template int AbstractCumulativeProduct::LeftBinarySearch( const int& i_start , const F& f ) { if( f( m_a[i_start] , i_start ) ){ return i_start; } int l = i_start; int r = this->m_size; while( l + 1 < r ){ int m = ( l + r ) / 2; ( f( LeftIntervalProduct( i_start , m ) , m ) ? r : l ) = m; } return r; } template inline int AbstractCumulativeProduct::RightBinarySearch( const U& u ) { return RightBinarySearch( [&]( const U& prod , const int& i ){ return !( prod < u ); } ); } template inline int AbstractCumulativeProduct::LeftBinarySearch( const U& u ) { return LeftBinarySearch( [&]( const U& prod , const int& i ){ return !( prod < u ); } ); } template inline int AbstractCumulativeProduct::RightBinarySearch( const int& i_start , const U& u ) { return RightBinarySearch( i_start , [&]( const U& prod , const int& i ){ return !( prod < u ); } ); } template inline int AbstractCumulativeProduct::LeftBinarySearch( const int& i_start , const U& u ) { return LeftBinarySearch( i_start , [&]( const U& prod , const int& i ){ return !( prod < u ); } ); } template template int CumulativeSum::BinarySearch( const Args&... args ) { return this->RightBinarySearch( args... ); } template inline int AbstractCumulativeProduct::Parent( const int& i ) { return i - 1; } template inline int AbstractCumulativeProduct::LCA( const int& i , const int& j ) { return min( i , j ); } // AAA ライブラリは以上に挿入する。 #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_LIBRARY #ifdef DEBUG #define _GLIBCXX_DEBUG #define SIGNAL signal( SIGABRT , &AlertAbort ); #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE2 ) #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 , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #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_SET_A , 0 , N ){ cin >> A[VARIABLE_FOR_SET_A]; } #define CIN_A( LL , A , N ) vector A( N ); SET_A( A , N ); #endif #include using namespace std; #define REPEAT_MAIN( BOUND ) int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ CERR( "テストケースの個数を入力してください。" ); 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( chrono::duration_cast( 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 SET_A_ASSERT( A , N , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A] , MIN , MAX ); } #define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define CIN_A_ASSERT( A , N , MIN , MAX ) vector A( N ); SET_A_ASSERT( A , N , 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 template using ret_t = decltype( declval()( declval()... ) ); template 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 using T2 = pair; template using T3 = tuple; template using T4 = tuple; using path = pair; // 入出力用 template inline basic_istream& VariadicCin( basic_istream& is ) { return is; } template inline basic_istream& VariadicCin( basic_istream& is , Arg& arg , ARGS&... args ) { return VariadicCin( is >> arg , args... ); } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator ) { return is; } template inline basic_istream& VariadicGetline( basic_istream& is , const char& separator , Arg& arg , ARGS&... args ) { return VariadicGetline( getline( is , arg , separator ) , separator , args... ); } template inline basic_ostream& operator<<( basic_ostream& os , const vector& arg ) { auto begin = arg.begin() , end = arg.end(); auto itr = begin; while( itr != end ){ ( itr == begin ? os : os << " " ) << *itr; itr++; } return os; } template inline basic_ostream& operator<<( basic_ostream& os , const pair& arg ) { return os << arg.first << " " << arg.second; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg& arg ) { return os << arg; } template inline basic_ostream& VariadicCout( basic_ostream& os , const Arg1& arg1 , const Arg2& arg2 , const ARGS&... args ) { return VariadicCout( os << arg1 << " " , arg2 , args... ); } // データ構造用 template inline T Multiplication( const T& t0 , const T& t1 ) { return t0 * t1; } template inline T Id( const T& v ) { return v; } // デバッグ用 #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_MAIN