#ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE // #define USE_GETLINE #endif #ifdef INCLUDE_MAIN inline void Solve() { CIN( int , K , N , M ); CIN_A( int , A , K ); CIN_A( int , B , N ); Map A_hind{}; FOR( k , 0 , K ){ A_hind[A[k]]++; } using path_type = tuple; gE.resize( N + 2 ); FOR_ITR( A_hind ){ gE[0].push_back( { itr->first , 0 , itr->second } ); } FOREQ( i , 1 , N ){ gE[i].push_back( { N + 1 , 0 , B[i-1] } ); } FOR( j , 0 , M ){ CIN( ll , uj , vj , wj ); gE[uj].push_back( { vj , wj , K } ); gE[vj].push_back( { uj , wj , K } ); } Graph graph{ N + 2 , Get( gE ) }; // MinimumCostFlow mcf{ graph , 1LL , 1LL<<62 }; AbstractMinimumCostFlow mcf{ graph , Ring( 1LL ) , 1LL<<62 }; auto [answer,flow] = mcf.GetFlow( 0 , N + 1 , K ); RETURN( answer ); } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_SUB // グラフ用 TE Map gF; TE VE gA; TE VE> gE; TE TY V> IN auto Get( CO V& a ) { return [&]( CRI i = 0 ){ RE a[i]; }; } // COMPAREに使用。圧縮時は削除する。 ll Naive( int N , int M , int K ) { ll answer = N + M + K; return answer; } // COMPAREに使用。圧縮時は削除する。 ll Answer( ll N , ll M , ll K ) { // START_WATCH; ll answer = N + M + K; // // TLに準じる乱択や全探索。デフォルトの猶予は100.0[ms]。 // CEXPR( double , TL , 2000.0 ); // while( CHECK_WATCH( TL ) ){ // } return answer; } // 圧縮時は中身だけ削除する。 inline void Experiment() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COUT( N , M , K , ":" , Naive( N , M , K ) ); // } // } // // cout << Naive( N ) << ",\n"[N==bound]; // } } // 圧縮時は中身だけ削除する。 inline void SmallTest() { // CEXPR( int , bound , 10 ); // FOREQ( N , 0 , bound ){ // FOREQ( M , 0 , bound ){ // FOREQ( K , 0 , bound ){ // COMPARE( N , M , K ); // } // } // // COMPARE( N ); // } } #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_SUB #ifdef INCLUDE_LIBRARY /* C-x 3 C-x o C-x C-fによるファイル操作用 BFS: c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/BreadthFirstSearch/compress.txt CoordinateCompress: c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/CoordinateCompress/compress.txt DFSOnTree c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/DepthFirstSearch/Tree/a.hpp Divisor: c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Prime/Divisor/compress.txt Polynomial c:/Users/user/Documents/Programming/Mathematics/Polynomial/compress.txt UnionFind c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/UnionFindForest/compress.txt */ // VVV 常設でないライブラリは以下に挿入する。 template class VirtualSemirng { public: virtual U Sum( const U& u0 , const U& u1 ) = 0; virtual const U& Zero() const noexcept = 0; virtual U Product( const U& u0 , const U& u1 ) = 0; virtual MONOID& AdditiveMonoid() noexcept = 0; virtual SEMIGROUP& MultiplicativeSemigroup() noexcept = 0; using type = U; }; template class AbstractSemirng : virtual public VirtualSemirng { protected: MONOID m_R0; SEMIGROUP m_R1; public: inline AbstractSemirng( MONOID R0 , SEMIGROUP R1 ); inline U Sum( const U& u0 , const U& u1 ); inline const U& Zero() const noexcept; inline U Product( const U& u0 , const U& u1 ); inline MONOID& AdditiveMonoid() noexcept; inline SEMIGROUP& MultiplicativeSemigroup() noexcept; }; template class Semirng : public AbstractSemirng,MultiplicativeMagma> { public: inline Semirng(); }; template inline AbstractSemirng::AbstractSemirng( MONOID R0 , SEMIGROUP R1 ) : m_R0( move( R0 ) ) , m_R1( move( R1 ) ) {} template inline Semirng::Semirng() : AbstractSemirng,MultiplicativeMagma>( AdditiveMonoid() , MultiplicativeMagma() ) {} template inline U AbstractSemirng::Sum( const U& u0 , const U& u1 ) { return m_R0.Sum( u0 , u1 ); } template inline const U& AbstractSemirng::Zero() const noexcept { return m_R0.Zero(); } template inline U AbstractSemirng::Product( const U& u0 , const U& u1 ) { return m_R1.Product( u0 , u1 ); } template inline MONOID& AbstractSemirng::AdditiveMonoid() noexcept { return m_R0; } template inline SEMIGROUP& AbstractSemirng::MultiplicativeSemigroup() noexcept { return m_R1; } template class VirtualRing : virtual public VirtualSemirng { public: virtual U Inverse( const U& u ) = 0; virtual const U& One() const noexcept = 0; inline GROUP& AdditiveGroup() noexcept; inline MONOID& MultiplicativeMonoid() noexcept; }; template class AbstractRing : virtual public VirtualRing , public AbstractSemirng { public: inline AbstractRing( GROUP R0 , MONOID R1 ); inline U Inverse( const U& u ); inline const U& One() const noexcept; }; template class Ring : virtual public AbstractRing,MultiplicativeMonoid> { public: inline Ring( const U& one_U ); }; template inline AbstractRing::AbstractRing( GROUP R0 , MONOID R1 ) : AbstractSemirng( move( R0 ) , move( R1 ) ) {} template inline Ring::Ring( const U& one_U ) :AbstractRing,MultiplicativeMonoid>( AdditiveGroup() , MultiplicativeMonoid( one_U ) ) {} template inline U AbstractRing::Inverse( const U& u ) { return this->m_R0.Inverse( u ); } template inline const U& AbstractRing::One() const noexcept { return this->m_R1.One(); } template inline GROUP& VirtualRing::AdditiveGroup() noexcept { return this->AdditiveMonoid(); } template inline MONOID& VirtualRing::MultiplicativeMonoid() noexcept { return this->MultiplicativeSemigroup(); } #define BELLMAN_FORD_BODY(INITIALISE_PREV,SET_PREV)CO U& zero = m_M.Zero();CO U& infty = TH->Infty();AS(zero < infty);CRI SZ = m_G.SZ();auto&& i_start = m_G.Enumeration_inv(t_start);AS(0 <= i_start && i_start < SZ);VE found(SZ);VE weight(SZ,infty);found[i_start]= true;weight[i_start]= 0;INITIALISE_PREV;for(int LE = 0;LE < SZ;LE++){for(int i = 0;i < SZ;i++){if(found[i]){CO U& weight_i = weight[i];AS(weight_i != infty);auto&& edge_i = m_G.Edge(m_G.Enumeration(i));for(auto IT = edge_i.BE(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);CO U& edge_ij = IT->second;U temp = m_M.Sum(weight_i,edge_ij);U& weight_j = weight[j];if(weight_j > temp){found[j]= true;weight_j = MO(temp);SET_PREV;}}}}}bool valid = true;for(int i = 0;i < SZ && valid;i++){if(found[i]){CO U& weight_i = weight[i];auto&& edge_i = m_G.Edge(m_G.Enumeration(i));for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);CO U& edge_ij = IT->second;U& weight_j = weight[j];CO U temp = m_M.Sum(weight_i,edge_ij);if(weight_j > temp){valid = false;break;}}}} TE CL AbstractBellmanFord:PU PointedSet{PU:GRAPH& m_G;MONOID m_M;IN AbstractBellmanFord(GRAPH& G,MONOID M,CO U& infty);tuple> GetDistance(CO inner_t& t_start);TE TY V> tuple,VE>>> GetPath(CO inner_t& t_start,CO V>& t_finals);tuple,VE>>> GetPath(CO inner_t& t_start);};TE CL BellmanFord:PU AbstractBellmanFord,ll>{PU:IN BellmanFord(GRAPH& G);}; TE IN AbstractBellmanFord::AbstractBellmanFord(GRAPH& G,MONOID M,CO U& infty):PointedSet(infty),m_G(G),m_M(MO(M)){ST_AS(! is_same_v);}TE IN BellmanFord::BellmanFord(GRAPH& G):AbstractBellmanFord,ll>(G,AdditiveMonoid<>(),4611686018427387904){}TE tuple> AbstractBellmanFord::GetDistance(CO inner_t& t_start){BELLMAN_FORD_BODY(,);m_G.Reset();RE{valid,MO(weight)};}TE TE TY V>tuple,VE>>> AbstractBellmanFord::GetPath(CO inner_t& t_start,CO V>& t_finals){BELLMAN_FORD_BODY(VE prev(SZ),prev[j]= i);VE>> path{};if(valid){CO int path_SZ = t_finals.SZ();path.reserve(path_SZ);for(auto IT = t_finals.begin(),EN = t_finals.EN();IT != EN;IT++){LI> path_j{};CO inner_t& t_final = *IT;path_j.push_back(t_final);int i = m_G.Enumeration_inv(t_final);if(found[i]){WH(i != i_start){i = prev[i];path_j.push_front(m_G.Enumeration(i));}}path.push_back(path_j);}}m_G.Reset();RE{valid,MO(weight),MO(path)};}TE tuple,VE>>> AbstractBellmanFord::GetPath(CO inner_t& t_start){CRI SZ = m_G.SZ();VE> t_finals(SZ);for(int i = 0;i < SZ;i++){t_finals[i]= i;}RE GetPath(t_start,t_finals);} #define DIJKSTRA_BODY(INITIALISE_PREV,CHECK_FINAL,SET_PREV)CO U& zero = m_M.Zero();CO U& infty = TH->Infty();AS(zero < infty);CRI SZ = m_G.SZ();auto&& i_start = m_G.Enumeration_inv(t_start);AS(0 <= i_start && i_start < SZ);set> vertex{};VE found(SZ);VE weight(SZ,infty);vertex.insert(pair(weight[i_start]= zero,i_start));INITIALISE_PREV;WH(! vertex.empty()){auto begin = vertex.begin();auto[weight_i,i]= *begin;CHECK_FINAL;found[i]= true;vertex.erase(begin);auto&& edge_i = m_G.Edge(m_G.Enumeration(i));LI> changed_vertex{};for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){auto&& j = m_G.Enumeration_inv(IT->first);if(!found[j]){CO U& edge_ij = IT->second;U temp = m_M.Sum(weight_i,edge_ij);AS(!(temp < edge_ij)&& temp < infty);U& weight_j = weight[j];if(weight_j > temp){if(weight_j != infty){vertex.erase(pair(weight_j,j));}SET_PREV;changed_vertex.push_back(pair(weight_j = MO(temp),j));}}}for(auto IT_changed = changed_vertex.begin(),EN_changed = changed_vertex.EN();IT_changed != EN_changed;IT_changed++){vertex.insert(*IT_changed);}} TE CL AbstractDijkstra:PU PointedSet{PU:GRAPH& m_G;MONOID m_M;IN AbstractDijkstra(GRAPH& G,MONOID M,CO U& infty);U GetDistance(CO inner_t& t_start,CO inner_t& t_final);VE GetDistance(CO inner_t& t_start);pair>> GetPath(CO inner_t& t_start,CO inner_t& t_final);TE TY V> pair,VE>>> GetPath(CO inner_t& t_start,CO V>& t_finals);pair,VE>>> GetPath(CO inner_t& t_start);};TE CL Dijkstra:PU AbstractDijkstra,ll>{PU:IN Dijkstra(GRAPH& G);}; TE IN AbstractDijkstra::AbstractDijkstra(GRAPH& G,MONOID M,CO U& infty):PointedSet(infty),m_G(G),m_M(MO(M)){ST_AS(! is_same_v);}TE IN Dijkstra::Dijkstra(GRAPH& G):AbstractDijkstra,ll>(G,AdditiveMonoid<>(),4611686018427387904){}TE U AbstractDijkstra::GetDistance(CO inner_t& t_start,CO inner_t& t_final){auto&& i_final = m_G.Enumeration_inv(t_final);DIJKSTRA_BODY(,if(i == i_final){break;},);U AN{MO(weight[i_final])};m_G.Reset();RE AN;}TE VE AbstractDijkstra::GetDistance(CO inner_t& t_start){DIJKSTRA_BODY(,,);m_G.Reset();RE weight;}TE pair>> AbstractDijkstra::GetPath(CO inner_t& t_start,CO inner_t& t_final){auto&& i_final = m_G.Enumeration_inv(t_final);DIJKSTRA_BODY(VE prev(SZ),if(i == i_final){break;},prev[j]= i);int i = i_final;LI> path{};path.push_back(t_final);if(found[i]){WH(i != i_start){i = prev[i];path.push_front(m_G.Enumeration(i));}}U AN{MO(weight[i_final])};m_G.Reset();RE{MO(AN),MO(path)};}TE TE TY V>pair,VE>>> AbstractDijkstra::GetPath(CO inner_t& t_start,CO V>& t_finals){DIJKSTRA_BODY(VE prev(SZ),,prev[j]= i);CO int path_SZ = t_finals.SZ();VE>> path;path.reserve(path_SZ);for(auto IT = t_finals.begin(),EN = t_finals.EN();IT != EN;IT++){LI> path_j{};CO inner_t& t_final = *IT;path_j.push_back(t_final);int i = m_G.Enumeration_inv(t_final);if(found[i]){WH(i != i_start){i = prev[i];path_j.push_front(m_G.Enumeration(i));}}path.push_back(path_j);}m_G.Reset();RE{MO(weight),MO(path)};}TE pair,VE>>> AbstractDijkstra::GetPath(CO inner_t& t_start){CRI SZ = m_G.SZ();VE> t_finals(SZ);for(int i = 0;i < SZ;i++){t_finals[i]= i;}RE GetPath(t_start,t_finals);} #define POTENTIALISED_DIJKSTRA_BODY(GET,WEIGHT,...)CO U& infty = TH->Infty();if(m_valid){CO U& zero = m_M.Zero();auto edge =[&](CO T& t){CO U& potential_i = m_potential[m_G.Enumeration_inv(t)];AS(potential_i < infty);auto edge_i = m_G.Edge(t);LI> AN{};for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){auto& e = *IT;if(m_on(e)){CO auto& v_j = get<0>(e);U& w_j = get<1>(e);CO U& potential_j = m_potential[m_G.Enumeration_inv(v_j)];AS(w_j < infty && potential_j < infty);CO U potential_j_inv = m_M.Inverse(potential_j);w_j = m_M.Sum(m_M.Sum(w_j,potential_i),potential_j_inv);AS(!(w_j < zero)&& w_j < infty);AN.push_back({v_j,MO(w_j)});}}RE AN;};auto G = m_G.GetGraph(MO(edge));AbstractDijkstra d{G,m_M,infty};auto value = d.GET;CRI SZ = m_G.SZ();for(int i = 0;i < SZ;i++){auto& weight_i = WEIGHT[i];if(weight_i != infty){weight_i = m_M.Sum(weight_i,m_potential[i]);}}RE{m_valid,__VA_ARGS__};}auto edge =[&](CO T& t){auto&& edge_i = m_G.Edge(t);LI> AN{};for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){if(m_on(*IT)){AN.push_back({get<0>(*IT),get<1>(*IT)});}}RE AN;};auto G = m_G.GetGraph(MO(edge));AbstractBellmanFord d{G,m_M,infty};RE d.GET; TE CL AbstractPotentialisedDijkstra:PU PointedSet{PU:GRAPH& m_G;GROUP m_M;T m_t_start;bool m_valid;VE m_potential;On m_on;IN AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& negative = true);IN AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,CO bool& valid,VE potential,On on);IN CO bool& Valid()CO NE;IN CO VE& Potential()CO NE;IN VO SetPotential(CO bool& valid,VE potential);tuple> GetDistance();TE tuple,VE>> GetPath(CO Args&... args);};TE CL PotentialisedDijkstra:PU AbstractPotentialisedDijkstra,ll,On>{PU:TE IN PotentialisedDijkstra(GRAPH& G,CO T& t_start,Args&&... args);}; TE IN AbstractPotentialisedDijkstra::AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,On on,CO bool& negative):AbstractPotentialisedDijkstra(G,MO(M),t_start,infty,true,VE(),MO(on)){if(negative){auto edge =[&](CRI t){auto&& edge_i = m_G.Edge(t);LI> AN{};for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){CO auto& e = *IT;AN.push_back({get<0>(e),get<1>(e)});}RE AN;};auto G_full = m_G.GetGraph(MO(edge));AbstractBellmanFord bf{G_full,m_M,infty};auto[valid,potential]= bf.GetDistance(m_t_start);m_valid = valid;m_potential = MO(potential);}else{m_potential = VE(m_G.SZ(),m_M.Zero());}}TE IN AbstractPotentialisedDijkstra::AbstractPotentialisedDijkstra(GRAPH& G,GROUP M,CO T& t_start,CO U& infty,CO bool& valid,VE potential,On on):PointedSet(infty),m_G(G),m_M(MO(M)),m_t_start(t_start),m_valid(valid),m_potential(potential),m_on(MO(on)){ST_AS(is_invocable_r_v().Edge(declval()).back())>);}TE TE IN PotentialisedDijkstra::PotentialisedDijkstra(GRAPH& G,CO T& t_start,Args&&... args):AbstractPotentialisedDijkstra,ll,On>(G,AdditiveGroup<>(),t_start,4611686018427387904,forward>(args)...){}TE IN CO bool& AbstractPotentialisedDijkstra::Valid()CO NE{RE m_valid;}TE IN CO VE& AbstractPotentialisedDijkstra::Potential()CO NE{RE m_potential;}TE IN VO AbstractPotentialisedDijkstra::SetPotential(CO bool& valid,VE potential){AS(int(potential.SZ())== m_G.SZ());m_valid = valid;m_potential = MO(potential);}TE tuple> AbstractPotentialisedDijkstra::GetDistance(){POTENTIALISED_DIJKSTRA_BODY(GetDistance(m_t_start),value,MO(value));}TE TE tuple,VE>> AbstractPotentialisedDijkstra::GetPath(CO Args&... args){POTENTIALISED_DIJKSTRA_BODY(GetPath(m_t_start,args...),get<0>(value),MO(get<0>(value)),MO(get<1>(value)));} TE CL AbstractMinimumCostFlow:PU PointedSet{PU:GRAPH& m_G;RING m_R;IN AbstractMinimumCostFlow(GRAPH& G,RING R,CO U& infty);pair,U>>>> GetFlow(CO inner_t& t_start,CO inner_t& t_final,U f);};TE CL MinimumCostFlow:PU AbstractMinimumCostFlow,U>{PU:IN MinimumCostFlow(GRAPH& G,CO U& one_U,CO U& infty);}; TE IN AbstractMinimumCostFlow::AbstractMinimumCostFlow(GRAPH& G,RING R,CO U& infty):PointedSet(infty),m_G(G),m_R(MO(R)){}TE IN MinimumCostFlow::MinimumCostFlow(GRAPH& G,CO U& one_U,CO U& infty):AbstractMinimumCostFlow,U>(G,Ring(one_U),infty){}TE pair,U>>>> AbstractMinimumCostFlow::GetFlow(CO inner_t& t_start,CO inner_t& t_final,U f){US T = inner_t;CO U& zero = m_R.Zero();CO U& infty = TH->Infty();CRI SZ = m_G.SZ();VE>> rest(SZ);VE>> flow(SZ);int edge_num = 0;for(int i = 0;i < SZ;i++){auto&& ui = m_G.Enumeration(i);auto&& edge_i = m_G.Edge(ui);for(auto IT = edge_i.begin(),EN = edge_i.EN();IT != EN;IT++){CO auto&[vj,wj,fj]= *IT;AS(ui != vj && !(wj < zero)&& wj < infty && !(fj < zero)&& fj < infty);auto&& j = m_G.Enumeration_inv(vj);rest[i].push_back({j,wj,fj,false,edge_num});rest[j].push_back({i,m_R.Inverse(wj),zero,true,edge_num});flow[i].push_back({vj,0});edge_num++;}}for(int i = 0;i < SZ;i++){auto& rest_i = rest[i];sort(rest_i.begin(),rest_i.EN());}VE> edge_pair(edge_num,{-1,-1,-1,-1});for(int i = 0;i < SZ;i++){CO auto& rest_i = rest[i];CO int SZ_i = rest_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto& rest_ij = rest_i[j];auto&[i_0,j_0,i_1,j_1]= edge_pair[get<4>(rest_ij)];if(i_0 == -1){i_0 = i;j_0 = j;}else{i_1 = i;j_1 = j;}}}auto edge =[&](CO T& t)-> CO VE>&{RE rest[m_G.Enumeration_inv(t)];};auto on =[&](CO tuple& e){RE zero < get<2>(e);};auto G = m_G.GetGraph(MO(edge));AbstractPotentialisedDijkstra pd{G,m_R.AdditiveGroup(),t_start,infty,MO(on),false};auto&& i_start = m_G.Enumeration_inv(t_start);LI t_finals ={t_final};U w = zero;WH(zero < f){auto[valid,weight,paths]= pd.GetPath(t_finals);AS(valid);pd.SetPotential(valid,MO(weight));auto& path = paths.front();auto IT_path = path.begin(),IT_path_prev = IT_path,EN_path = path.EN();AS(IT_path != EN_path);int i = i_start;LI> flow_num{};U f_min = f;WH(++IT_path != EN_path){T t = *IT_path;flow_num.push_back({i,m_G.Enumeration_inv(t),-1,-1});auto&[i_curr,i_next,j_1,j_2]= flow_num.back();CO auto& rest_i = rest[i_curr];int SZ_i = rest_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto&[vj,wj,fj,rj,numj]= rest_i[j];if(zero < fj && vj == t){j_1 = j;fj < f_min?f_min = fj:f_min;if(rj){i_curr = i_next;t = *IT_path_prev;}break;}}AS(j_1 != -1);auto& flow_i = flow[i_curr];SZ_i = flow_i.SZ();for(int j = 0;j < SZ_i;j++){CO auto&[vj,fj]= flow_i[j];if(vj == t){j_2 = j;break;}}AS(j_2 != -1);i_curr = i;i = i_next;IT_path_prev = IT_path;}CO U f_min_minus = m_R.Inverse(f_min);U w_diff = zero;for(auto IT = flow_num.begin(),EN = flow_num.EN();IT != EN;IT++){CO auto&[i_curr,i_next,j_1,j_2]= *IT;auto&[vj,wj,fj,rj,numj]= rest[i_curr][j_1];CO auto& edge_pair_i = edge_pair[numj];CRI j_3 = get<0>(edge_pair_i)== i_curr?get<3>(edge_pair_i):get<1>(edge_pair_i);auto& fj_inv = get<2>(rest[i_next][j_3]);auto& f_curr = get<1>(flow[rj?i_next:i_curr][j_2]);w_diff = m_R.Sum(w_diff,wj);fj = m_R.Sum(fj,f_min_minus);fj_inv = m_R.Sum(fj_inv,f_min);f_curr = m_R.Sum(f_curr,f_min);}f = m_R.Sum(f,f_min_minus);w = m_R.Sum(w,m_R.Product(f_min,w_diff));}RE{MO(w),MO(flow)};} // AAA 常設でないライブラリは以上に挿入する。 #define INCLUDE_SUB #include __FILE__ #else // INCLUDE_LIBRARY #ifdef DEBUG #define _GLIBCXX_DEBUG #define REPEAT_MAIN( BOUND ) START_MAIN; signal( SIGABRT , &AlertAbort ); AutoCheck( exec_mode , use_getline ); if( exec_mode == sample_debug_mode || exec_mode == submission_debug_mode || exec_mode == library_search_mode ){ RE 0; } else if( exec_mode == experiment_mode ){ Experiment(); RE 0; } else if( exec_mode == small_test_mode ){ SmallTest(); RE 0; }; DEXPR( int , bound_test_case_num , BOUND , min( BOUND , 100 ) ); int test_case_num = 1; if( exec_mode == solve_mode ){ if CE( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } } else if( exec_mode == random_test_mode ){ CERR( "ランダムテストを行う回数を指定してください。" ); SET_LL( test_case_num ); } FINISH_MAIN #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 ) ); AS( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) if( exec_mode == solve_mode ){ SET_LL( A ); ASSERT( A , MIN , MAX ); } else if( exec_mode == random_test_mode ){ CERR( #A , " = " , ( A = GetRand( MIN , MAX ) ) ); } else { AS( false ); } #define SOLVE_ONLY ST_AS( __FUNCTION__[0] == 'S' ) #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 REPEAT_MAIN( BOUND ) START_MAIN; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if CE( bound_test_case_num > 1 ){ SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } FINISH_MAIN #define DEXPR( LL , BOUND , VALUE , DEBUG_VALUE ) CEXPR( LL , BOUND , VALUE ) #define ASSERT( A , MIN , MAX ) AS( ( MIN ) <= A && A <= ( MAX ) ) #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX ) #define SOLVE_ONLY #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 , ... ) SOLVE_ONLY; string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) SOLVE_ONLY; GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #else #define SET_LL( A ) cin >> A #define CIN( LL , ... ) SOLVE_ONLY; LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define SET_A( A , N ) SOLVE_ONLY; FOR( VARIABLE_FOR_CIN_A , 0 , N ){ cin >> A[VARIABLE_FOR_CIN_A]; } #define CIN_A( LL , A , N ) VE A( N ); SET_A( A , N ); #endif #include using namespace std; #define ATT __attribute__( ( target( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) ) ) #define START_MAIN int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ) #define FINISH_MAIN REPEAT( test_case_num ){ if CE( 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 ) CE LL BOUND = VALUE #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 .BE() , end_ ## ARRAY = ARRAY .EN() #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.BE() , EN_FOR_OUTPUT_ITR = A.EN(); bool VARIABLE_FOR_OUTPUT_ITR = ITERATOR_FOR_COUT_ITR != END_FOR_COUT_ITR; WH( 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( ... ) SOLVE_ONLY; COUT( __VA_ARGS__ ); RE #define COMPARE( ... ) auto naive = Naive( __VA_ARGS__ ); auto answer = Answer( __VA_ARGS__ ); bool match = naive == answer; COUT( "(" , #__VA_ARGS__ , ") == (" , __VA_ARGS__ , ") : Naive == " , naive , match ? "==" : "!=" , answer , "== Answer" ); if( !match ){ RE; } // 圧縮用 #define TE template #define TY typename #define US using #define ST static #define AS assert #define IN inline #define CL class #define PU public #define OP operator #define CE constexpr #define CO const #define NE noexcept #define RE return #define WH while #define VO void #define VE vector #define LI list #define BE begin #define EN end #define SZ size #define LE length #define PW Power #define MO move #define TH this #define CRI CO int& #define CRUI CO uint& #define CRL CO ll& #define ST_AS static_assert #define reMO_CO remove_const #define is_COructible_v is_constructible_v #define rBE rbegin #define reSZ resize // 型のエイリアス #define decldecay_t( VAR ) decay_t TE US ret_t = decltype( declval()( declval()... ) ); TE US inner_t = TY T::type; US uint = unsigned int; US ll = long long; US ull = unsigned long long; US ld = long double; US lld = __float128; TE US T2 = pair; TE US T3 = tuple; TE US T4 = tuple; US path = pair; // 入出力用 TE IN basic_istream& VariadicCin( basic_istream& is ) { RE is; } TE IN basic_istream& VariadicCin( basic_istream& is , Arg& arg , ARGS&... args ) { RE VariadicCin( is >> arg , args... ); } TE IN basic_istream& VariadicGetline( basic_istream& is , CO char& separator ) { RE is; } TE IN basic_istream& VariadicGetline( basic_istream& is , CO char& separator , Arg& arg , ARGS&... args ) { RE VariadicGetline( getline( is , arg , separator ) , separator , args... ); } TE IN basic_ostream& operator<<( basic_ostream& os , CO VE& arg ) { auto BE = arg.BE() , EN = arg.EN(); auto itr = BE; WH( itr != EN ){ ( itr == BE ? os : os << " " ) << *itr; itr++; } RE os; } TE IN basic_ostream& VariadicCout( basic_ostream& os , CO Arg& arg ) { RE os << arg; } TE IN basic_ostream& VariadicCout( basic_ostream& os , CO Arg1& arg1 , CO Arg2& arg2 , CO ARGS&... args ) { RE VariadicCout( os << arg1 << " " , arg2 , args... ); } // デバッグ用 #ifdef DEBUG IN VO AlertAbort( int n ) { CERR( "abort関数が呼ばれました。assertマクロのメッセージが出力されていない場合はオーバーフローの有無を確認をしてください。" ); } VO AutoCheck( int& exec_mode , CO bool& use_getline ); IN VO Solve(); IN VO Experiment(); IN VO SmallTest(); IN VO RandomTest(); ll GetRand( CRL Rand_min , CRL Rand_max ); IN VO BreakPoint( CRI LINE ) {} int exec_mode; CEXPR( int , solve_mode , 0 ); CEXPR( int , sample_debug_mode , 1 ); CEXPR( int , submission_debug_mode , 2 ); CEXPR( int , library_search_mode , 3 ); CEXPR( int , experiment_mode , 4 ); CEXPR( int , small_test_mode , 5 ); CEXPR( int , random_test_mode , 6 ); #ifdef USE_GETLINE CEXPR( bool , use_getline , true ); #else CEXPR( bool , use_getline , false ); #endif #else ll GetRand( CRL Rand_min , CRL Rand_max ) { ll answer = time( NULL ); RE answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; } #endif // VVV 常設ライブラリは以下に挿入する。 // Map // c:/Users/user/Documents/Programming/Mathematics/Function/Map CL is_ordered{PU:is_ordered()= delete;TE ST CE auto Check(CO T& t)-> decltype(t < t,true_type());ST CE false_type Check(...);TE ST CE CO bool value = is_same_v< decltype(Check(declval())),true_type >;}; TE US Map = conditional_t>,unordered_map,conditional_t,map,void>>; // Algebra // c:/Users/user/Documents/Programming/Mathematics/Algebra/compress.txt #define DC_OF_CPOINT(POINT)IN CO U& POINT()CO NE #define DC_OF_POINT(POINT)IN U& POINT() NE #define DF_OF_CPOINT(POINT)TE IN CO U& VirtualPointedSet::POINT()CO NE{RE Point();} #define DF_OF_POINT(POINT)TE IN U& VirtualPointedSet::POINT()NE{RE Point();} TE CL UnderlyingSet{PU:US type = U;};TE CL VirtualPointedSet:virtual PU UnderlyingSet{PU:virtual CO U& Point()CO NE = 0;virtual U& Point() NE = 0;DC_OF_CPOINT(Unit);DC_OF_CPOINT(Zero);DC_OF_CPOINT(One);DC_OF_CPOINT(Infty);DC_OF_CPOINT(size);DC_OF_POINT(init);DC_OF_POINT(root);};TE CL PointedSet:virtual PU VirtualPointedSet{PU:U m_b_U;IN PointedSet(CO U& b_u = U());IN CO U& Point()CO NE;IN U& Point() NE;};TE CL VirtualNSet:virtual PU UnderlyingSet{PU:virtual U Transfer(CO U& u)= 0;IN U Inverse(CO U& u);};TE CL AbstractNSet:virtual PU VirtualNSet{PU:F_U& m_f_U;IN AbstractNSet(F_U& f_U);IN U Transfer(CO U& u);};TE CL VirtualMagma:virtual PU UnderlyingSet{PU:virtual U Product(CO U& u0,CO U& u1)= 0;IN U Sum(CO U& u0,CO U& u1);};TE CL AdditiveMagma:virtual PU VirtualMagma{PU:IN U Product(CO U& u0,CO U& u1);};TE CL MultiplicativeMagma:virtual PU VirtualMagma{PU:IN U Product(CO U& u0,CO U& u1);};TE CL AbstractMagma:virtual PU VirtualMagma{PU:M_U& m_m_U;IN AbstractMagma(M_U& m_U);IN U Product(CO U& u0,CO U& u1);}; TE IN PointedSet::PointedSet(CO U& b_U):m_b_U(b_U){}TE IN CO U& PointedSet::Point()CO NE{RE m_b_U;}TE IN U& PointedSet::Point()NE{RE m_b_U;}DF_OF_CPOINT(Unit);DF_OF_CPOINT(Zero);DF_OF_CPOINT(One);DF_OF_CPOINT(Infty);DF_OF_CPOINT(size);DF_OF_POINT(init);DF_OF_POINT(root);TE IN AbstractNSet::AbstractNSet(F_U& f_U):m_f_U(f_U){ST_AS(is_invocable_r_v);}TE IN U AbstractNSet::Transfer(CO U& u){RE m_f_U(u);}TE IN U VirtualNSet::Inverse(CO U& u){RE Transfer(u);}TE IN AbstractMagma::AbstractMagma(M_U& m_U):m_m_U(m_U){ST_AS(is_invocable_r_v);}TE IN U AdditiveMagma::Product(CO U& u0,CO U& u1){RE u0 + u1;}TE IN U MultiplicativeMagma::Product(CO U& u0,CO U& u1){RE u0 * u1;}TE IN U AbstractMagma::Product(CO U& u0,CO U& u1){RE m_m_U(u0,u1);}TE IN U VirtualMagma::Sum(CO U& u0,CO U& u1){RE Product(u0,u1);} TE CL VirtualMonoid:virtual PU VirtualMagma,virtual PU VirtualPointedSet{};TE CL AdditiveMonoid:virtual PU VirtualMonoid,PU AdditiveMagma,PU PointedSet{};TE CL MultiplicativeMonoid:virtual PU VirtualMonoid,PU MultiplicativeMagma,PU PointedSet{PU:IN MultiplicativeMonoid(CO U& e_U);};TE CL AbstractMonoid:virtual PU VirtualMonoid,PU AbstractMagma,PU PointedSet{PU:IN AbstractMonoid(M_U& m_U,CO U& e_U);}; TE IN MultiplicativeMonoid::MultiplicativeMonoid(CO U& e_U):PointedSet(e_U){}TE IN AbstractMonoid::AbstractMonoid(M_U& m_U,CO U& e_U):AbstractMagma(m_U),PointedSet(e_U){} TE CL VirtualGroup:virtual PU VirtualMonoid,virtual PU VirtualPointedSet,virtual PU VirtualNSet{};TE CL AdditiveGroup:virtual PU VirtualGroup,PU AdditiveMonoid{PU:IN U Transfer(CO U& u);};TE CL AbstractGroup:virtual PU VirtualGroup,PU AbstractMonoid,PU AbstractNSet{PU:IN AbstractGroup(M_U& m_U,CO U& e_U,I_U& i_U);IN U Transfer(CO U& u);};TE IN AbstractGroup::AbstractGroup(M_U& m_U,CO U& e_U,I_U& i_U):AbstractMonoid(m_U,e_U),AbstractNSet(i_U){}TE IN U AbstractGroup::Transfer(CO U& u){RE m_i_U(u);}TE IN U AdditiveGroup::Transfer(CO U& u){RE -u;} // Graph // c:/Users/user/Documents/Programming/Mathematics/Geometry/Graph/compress.txt #define SFINAE_FOR_GRAPH TY T,TY E,enable_if_t,void*> PTR TE CL VirtualGraph:PU UnderlyingSet{PU:int m_SZ;E m_edge;IN VirtualGraph(CRI SZ,E edge);virtual R1 Enumeration(CRI i)= 0;virtual R2 Enumeration_inv(CO T& t)= 0;IN VO Reset();IN CRI SZ()CO NE;IN E& edge()NE;IN ret_t Edge(CO T& t);US type = T;};TE CL Graph:virtual PU VirtualGraph{PU:IN Graph(CRI SZ,E edge);IN CRI Enumeration(CRI i);IN CRI Enumeration_inv(CRI t);TE IN Graph GetGraph(F edge)CO;};TE CL EnumerationGraph:virtual PU VirtualGraph,ret_t,E>{PU:Enum_T& m_enum_T;Enum_T_inv& m_enum_T_inv;IN EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge);IN ret_t Enumeration(CRI i);IN ret_t Enumeration_inv(CO T& t);TE IN EnumerationGraph GetGraph(F edge)CO;};TE EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge)-> EnumerationGraph()(0)),Enum_T,Enum_T_inv,E>;TE CL MemorisationGraph:virtual PU VirtualGraph{PU:int m_LE;VE m_memory;Map m_memory_inv;IN MemorisationGraph(CRI SZ,E edge);IN T Enumeration(CRI i);IN CRI Enumeration_inv(CO T& t);IN VO Reset();TE IN MemorisationGraph GetGraph(F edge)CO;};TE MemorisationGraph(CRI SZ,E edge)-> MemorisationGraph()().back()),E>;TE MemorisationGraph(CRI SZ,E edge)-> MemorisationGraph(declval()().back())),E>; TE IN VirtualGraph::VirtualGraph(CRI SZ,E edge):m_SZ(SZ),m_edge(MO(edge)){ST_AS(is_COructible_v && is_COructible_v && is_invocable_v);}TE IN Graph::Graph(CRI SZ,E edge):VirtualGraph(SZ,MO(edge)){}TE IN EnumerationGraph::EnumerationGraph(CRI SZ,Enum_T& enum_T,Enum_T_inv& enum_T_inv,E edge):VirtualGraph,ret_t,E>(SZ,MO(edge)),m_enum_T(enum_T),m_enum_T_inv(enum_T_inv){}TE IN MemorisationGraph::MemorisationGraph(CRI SZ,E edge):VirtualGraph(SZ,MO(edge)),m_LE(),m_memory(),m_memory_inv(){}TE IN CRI Graph::Enumeration(CRI i){RE i;}TE IN ret_t EnumerationGraph::Enumeration(CRI i){RE m_enum_T(i);}TE IN T MemorisationGraph::Enumeration(CRI i){AS(0 <= i && i < m_LE);RE m_memory[i];}TE IN CRI Graph::Enumeration_inv(CRI i){RE i;}TE IN ret_t EnumerationGraph::Enumeration_inv(CO T& t){RE m_enum_T_inv(t);}TE IN CRI MemorisationGraph::Enumeration_inv(CO T& t){if(m_memory_inv.count(t)== 0){AS(m_LE < TH->SZ());m_memory.push_back(t);RE m_memory_inv[t]= m_LE++;}RE m_memory_inv[t];}TE VO VirtualGraph::Reset(){}TE IN VO MemorisationGraph::Reset(){m_LE = 0;m_memory.clear();m_memory_inv.clear();}TE IN CRI VirtualGraph::SZ()CO NE{RE m_SZ;}TE IN E& VirtualGraph::edge()NE{RE m_edge;}TE IN ret_t VirtualGraph::Edge(CO T& t){RE m_edge(t);}TE TE IN Graph Graph::GetGraph(F edge)CO{RE Graph(TH->SZ(),MO(edge));}TE TE IN EnumerationGraph EnumerationGraph::GetGraph(F edge)CO{RE EnumerationGraph(TH->SZ(),m_enum_T,m_enum_T_inv,MO(edge));}TE TE IN MemorisationGraph MemorisationGraph::GetGraph(F edge)CO{RE MemorisationGraph(TH->SZ(),MO(edge));} // ConstexprModulo // c:/Users/user/Documents/Programming/Mathematics/Arithmetic/Mod/ConstexprModulo/a.hpp CEXPR(uint,P,998244353);TE CE INT& RS(INT& n)NE{RE n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n %= M;}TE CE uint& RS(uint& n)NE{RE n %= M;}TE CE ull& RS(ull& n)NE{RE n %= M;}TE CE INT& RSP(INT& n)NE{CE CO uint trunc =(1 << 23)- 1;INT n_u = n >> 23;n &= trunc;INT n_uq =(n_u / 7)/ 17;n_u -= n_uq * 119;n += n_u << 23;RE n < n_uq?n += P - n_uq:n -= n_uq;}TE <> CE ull& RS(ull& n)NE{CE CO ull Pull = P;CE CO ull Pull2 =(Pull - 1)*(Pull - 1);RE RSP(n > Pull2?n -= Pull2:n);}TE CE INT RS(INT&& n)NE{RE MO(RS(n));}TE CE INT RS(CO INT& n)NE{RE RS(INT(n));} #define SFINAE_FOR_MOD(DEFAULT)TY T,enable_if_t >::value>* DEFAULT #define DC_OF_CM_FOR_MOD(FUNC)CE bool OP FUNC(CO Mod& n)CO NE #define DC_OF_AR_FOR_MOD(FUNC)CE Mod OP FUNC(CO Mod& n)CO NE;TE CE Mod OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MOD(FUNC)TE CE bool Mod::OP FUNC(CO Mod& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MOD(FUNC,FORMULA)TE CE Mod Mod::OP FUNC(CO Mod& n)CO NE{RE MO(Mod(*TH)FUNC ## = n);}TE TE CE Mod Mod::OP FUNC(T&& n)CO NE{RE FORMULA;}TE CE Mod OP FUNC(T&& n0,CO Mod& n1)NE{RE MO(Mod(forward(n0))FUNC ## = n1);} TE CL Mod{PU:uint m_n;CE Mod()NE;CE Mod(CO Mod& n)NE;CE Mod(Mod& n)NE;CE Mod(Mod&& n)NE;TE CE Mod(CO T& n)NE;TE CE Mod(T& n)NE;TE CE Mod(T&& n)NE;CE Mod& OP=(CO Mod& n)NE;CE Mod& OP=(Mod&& n)NE;CE Mod& OP+=(CO Mod& n)NE;CE Mod& OP-=(CO Mod& n)NE;CE Mod& OP*=(CO Mod& n)NE;IN Mod& OP/=(CO Mod& n);CE Mod& OP<<=(int n)NE;CE Mod& OP>>=(int n)NE;CE Mod& OP++()NE;CE Mod OP++(int)NE;CE Mod& OP--()NE;CE Mod OP--(int)NE;DC_OF_CM_FOR_MOD(==);DC_OF_CM_FOR_MOD(!=);DC_OF_CM_FOR_MOD(<);DC_OF_CM_FOR_MOD(<=);DC_OF_CM_FOR_MOD(>);DC_OF_CM_FOR_MOD(>=);DC_OF_AR_FOR_MOD(+);DC_OF_AR_FOR_MOD(-);DC_OF_AR_FOR_MOD(*);DC_OF_AR_FOR_MOD(/);CE Mod OP<<(int n)CO NE;CE Mod OP>>(int n)CO NE;CE Mod OP-()CO NE;CE Mod& SignInvert()NE;CE Mod& Double()NE;CE Mod& Halve()NE;IN Mod& Invert();TE CE Mod& PositivePW(T&& EX)NE;TE CE Mod& NonNegativePW(T&& EX)NE;TE CE Mod& PW(T&& EX);CE VO swap(Mod& n)NE;CE CRUI RP()CO NE;ST CE Mod DeRP(CRUI n)NE;ST CE uint& Normalise(uint& n)NE;ST IN CO Mod& Inverse(CRUI n)NE;ST IN CO Mod& Factorial(CRUI n)NE;ST IN CO Mod& FactorialInverse(CRUI n)NE;ST IN Mod Combination(CRUI n,CRUI i)NE;ST IN CO Mod& zero()NE;ST IN CO Mod& one()NE;TE CE Mod& Ref(T&& n)NE;}; #define SFINAE_FOR_MN(DEFAULT)TY T,enable_if_t,decay_t >::value>* DEFAULT #define DC_OF_AR_FOR_MN(FUNC)IN MN OP FUNC(CO MN& n)CO NE;TE IN MN OP FUNC(T&& n)CO NE; #define DF_OF_CM_FOR_MN(FUNC)TE IN bool MN::OP FUNC(CO MN& n)CO NE{RE m_n FUNC n.m_n;} #define DF_OF_AR_FOR_MN(FUNC,FORMULA)TE IN MN MN::OP FUNC(CO MN& n)CO NE{RE MO(MN(*TH)FUNC ## = n);}TE TE IN MN MN::OP FUNC(T&& n)CO NE{RE FORMULA;}TE IN MN OP FUNC(T&& n0,CO MN& n1)NE{RE MO(MN(forward(n0))FUNC ## = n1);} TE CL MN:PU Mod{PU:CE MN()NE;CE MN(CO MN& n)NE;CE MN(MN& n)NE;CE MN(MN&& n)NE;TE CE MN(CO T& n)NE;TE CE MN(T&& n)NE;CE MN& OP=(CO MN& n)NE;CE MN& OP=(MN&& n)NE;CE MN& OP+=(CO MN& n)NE;CE MN& OP-=(CO MN& n)NE;CE MN& OP*=(CO MN& n)NE;IN MN& OP/=(CO MN& n);CE MN& OP<<=(int n)NE;CE MN& OP>>=(int n)NE;CE MN& OP++()NE;CE MN OP++(int)NE;CE MN& OP--()NE;CE MN OP--(int)NE;DC_OF_AR_FOR_MN(+);DC_OF_AR_FOR_MN(-);DC_OF_AR_FOR_MN(*);DC_OF_AR_FOR_MN(/);CE MN OP<<(int n)CO NE;CE MN OP>>(int n)CO NE;CE MN OP-()CO NE;CE MN& SignInvert()NE;CE MN& Double()NE;CE MN& Halve()NE;CE MN& Invert();TE CE MN& PositivePW(T&& EX)NE;TE CE MN& NonNegativePW(T&& EX)NE;TE CE MN& PW(T&& EX);CE uint RP()CO NE;CE Mod Reduce()CO NE;ST CE MN DeRP(CRUI n)NE;ST IN CO MN& Formise(CRUI n)NE;ST IN CO MN& Inverse(CRUI n)NE;ST IN CO MN& Factorial(CRUI n)NE;ST IN CO MN& FactorialInverse(CRUI n)NE;ST IN MN Combination(CRUI n,CRUI i)NE;ST IN CO MN& zero()NE;ST IN CO MN& one()NE;ST CE uint Form(CRUI n)NE;ST CE ull& Reduction(ull& n)NE;ST CE ull& ReducedMU(ull& n,CRUI m)NE;ST CE uint MU(CRUI n0,CRUI n1)NE;ST CE uint BaseSquareTruncation(uint& n)NE;TE CE MN& Ref(T&& n)NE;};TE CE MN Twice(CO MN& n)NE;TE CE MN Half(CO MN& n)NE;TE CE MN Inverse(CO MN& n);TE CE MN PW(MN n,T EX);TE CE MN<2> PW(CO MN<2>& n,CO T& p);TE CE T Square(CO T& t);TE <> CE MN<2> Square >(CO MN<2>& t);TE CE VO swap(MN& n0,MN& n1)NE;TE IN string to_string(CO MN& n)NE;TE IN basic_istream& OP>>(basic_istream& is,MN& n);TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n); TE CL COantsForMod{PU:COantsForMod()= delete;ST CE CO bool g_even =((M & 1)== 0);ST CE CO uint g_memory_bound = 1000000;ST CE CO uint g_memory_LE = M < g_memory_bound?M:g_memory_bound;ST CE ull MNBasePW(ull&& EX)NE;ST CE uint g_M_minus = M - 1;ST CE uint g_M_minus_2 = M - 2;ST CE uint g_M_minus_2_neg = 2 - M;ST CE CO int g_MN_digit = 32;ST CE CO ull g_MN_base = ull(1)<< g_MN_digit;ST CE CO uint g_MN_base_minus = uint(g_MN_base - 1);ST CE CO uint g_MN_digit_half =(g_MN_digit + 1)>> 1;ST CE CO uint g_MN_base_sqrt_minus =(1 << g_MN_digit_half)- 1;ST CE CO uint g_MN_M_neg_inverse = uint((g_MN_base - MNBasePW((ull(1)<<(g_MN_digit - 1))- 1))& g_MN_base_minus);ST CE CO uint g_MN_base_mod = uint(g_MN_base % M);ST CE CO uint g_MN_base_square_mod = uint(((g_MN_base % M)*(g_MN_base % M))% M);};TE CE ull COantsForMod::MNBasePW(ull&& EX)NE{ull prod = 1;ull PW = M;WH(EX != 0){(EX & 1)== 1?(prod *= PW)&= g_MN_base_minus:prod;EX >>= 1;(PW *= PW)&= g_MN_base_minus;}RE prod;} US MP = Mod

;US MNP = MN

;TE CE uint MN::Form(CRUI n)NE{ull n_copy = n;RE uint(MO(Reduction(n_copy *= COantsForMod::g_MN_base_square_mod)));}TE CE ull& MN::Reduction(ull& n)NE{ull n_sub = n & COantsForMod::g_MN_base_minus;RE((n +=((n_sub *= COantsForMod::g_MN_M_neg_inverse)&= COantsForMod::g_MN_base_minus)*= M)>>= COantsForMod::g_MN_digit)< M?n:n -= M;}TE CE ull& MN::ReducedMU(ull& n,CRUI m)NE{RE Reduction(n *= m);}TE CE uint MN::MU(CRUI n0,CRUI n1)NE{ull n0_copy = n0;RE uint(MO(ReducedMU(ReducedMU(n0_copy,n1),COantsForMod::g_MN_base_square_mod)));}TE CE uint MN::BaseSquareTruncation(uint& n)NE{CO uint n_u = n >> COantsForMod::g_MN_digit_half;n &= COantsForMod::g_MN_base_sqrt_minus;RE n_u;}TE CE MN::MN()NE:Mod(){static_assert(! COantsForMod::g_even);}TE CE MN::MN(CO MN& n)NE:Mod(n){}TE CE MN::MN(MN& n)NE:Mod(n){}TE CE MN::MN(MN&& n)NE:Mod(MO(n)){}TE TE CE MN::MN(CO T& n)NE:Mod(n){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE TE CE MN::MN(T&& n)NE:Mod(forward(n)){static_assert(! COantsForMod::g_even);Mod::m_n = Form(Mod::m_n);}TE CE MN& MN::OP=(CO MN& n)NE{RE Ref(Mod::OP=(n));}TE CE MN& MN::OP=(MN&& n)NE{RE Ref(Mod::OP=(MO(n)));}TE CE MN& MN::OP+=(CO MN& n)NE{RE Ref(Mod::OP+=(n));}TE CE MN& MN::OP-=(CO MN& n)NE{RE Ref(Mod::OP-=(n));}TE CE MN& MN::OP*=(CO MN& n)NE{ull m_n_copy = Mod::m_n;RE Ref(Mod::m_n = MO(ReducedMU(m_n_copy,n.m_n)));}TE IN MN& MN::OP/=(CO MN& n){RE OP*=(MN(n).Invert());}TE CE MN& MN::OP<<=(int n)NE{RE Ref(Mod::OP<<=(n));}TE CE MN& MN::OP>>=(int n)NE{RE Ref(Mod::OP>>=(n));}TE CE MN& MN::OP++()NE{RE Ref(Mod::Normalise(Mod::m_n += COantsForMod::g_MN_base_mod));}TE CE MN MN::OP++(int)NE{MN n{*TH};OP++();RE n;}TE CE MN& MN::OP--()NE{RE Ref(Mod::m_n < COantsForMod::g_MN_base_mod?((Mod::m_n += M)-= COantsForMod::g_MN_base_mod):Mod::m_n -= COantsForMod::g_MN_base_mod);}TE CE MN MN::OP--(int)NE{MN n{*TH};OP--();RE n;}DF_OF_AR_FOR_MN(+,MN(forward(n))+= *TH);DF_OF_AR_FOR_MN(-,MN(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MN(*,MN(forward(n))*= *TH);DF_OF_AR_FOR_MN(/,MN(forward(n)).Invert()*= *TH);TE CE MN MN::OP<<(int n)CO NE{RE MO(MN(*TH)<<= n);}TE CE MN MN::OP>>(int n)CO NE{RE MO(MN(*TH)>>= n);}TE CE MN MN::OP-()CO NE{RE MO(MN(*TH).SignInvert());}TE CE MN& MN::SignInvert()NE{RE Ref(Mod::m_n > 0?Mod::m_n = M - Mod::m_n:Mod::m_n);}TE CE MN& MN::Double()NE{RE Ref(Mod::Double());}TE CE MN& MN::Halve()NE{RE Ref(Mod::Halve());}TE CE MN& MN::Invert(){assert(Mod::m_n > 0);RE PositivePW(uint(COantsForMod::g_M_minus_2));}TE TE CE MN& MN::PositivePW(T&& EX)NE{MN PW{*TH};(--EX)%= COantsForMod::g_M_minus_2;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE TE CE MN& MN::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(Mod::m_n = COantsForMod::g_MN_base_mod):PositivePW(forward(EX));}TE TE CE MN& MN::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod::m_n == 0));RE neg?PositivePW(forward(EX *= COantsForMod::g_M_minus_2_neg)):NonNegativePW(forward(EX));}TE CE uint MN::RP()CO NE{ull m_n_copy = Mod::m_n;RE MO(Reduction(m_n_copy));}TE CE Mod MN::Reduce()CO NE{ull m_n_copy = Mod::m_n;RE Mod::DeRP(MO(Reduction(m_n_copy)));}TE CE MN MN::DeRP(CRUI n)NE{RE MN(Mod::DeRP(n));}TE IN CO MN& MN::Formise(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = DeRP(LE_curr);LE_curr++;}RE memory[n];}TE IN CO MN& MN::Inverse(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN(Mod::Inverse(LE_curr));LE_curr++;}RE memory[n];}TE IN CO MN& MN::Factorial(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};ST MN val_last{one()};WH(LE_curr <= n){memory[LE_curr++] = val_curr *= ++val_last;}RE memory[n];}TE IN CO MN& MN::FactorialInverse(CRUI n)NE{ST MN memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;ST MN val_curr{one()};ST MN val_last{one()};WH(LE_curr <= n){memory[LE_curr] = val_curr *= Inverse(LE_curr);LE_curr++;}RE memory[n];}TE IN MN MN::Combination(CRUI n,CRUI i)NE{RE i <= n?Factorial(n)*FactorialInverse(i)*FactorialInverse(n - i):zero();}TE IN CO MN& MN::zero()NE{ST CE CO MN z{};RE z;}TE IN CO MN& MN::one()NE{ST CE CO MN o{DeRP(1)};RE o;}TE TE CE MN& MN::Ref(T&& n)NE{RE *TH;}TE CE MN Twice(CO MN& n)NE{RE MO(MN(n).Double());}TE CE MN Half(CO MN& n)NE{RE MO(MN(n).Halve());}TE CE MN Inverse(CO MN& n){RE MO(MN(n).Invert());}TE CE MN PW(MN n,T EX){RE MO(n.PW(EX));}TE CE VO swap(MN& n0,MN& n1)NE{n0.swap(n1);}TE IN string to_string(CO MN& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_istream& OP>>(basic_istream& is,MN& n){ll m;is >> m;n = m;RE is;}TE IN basic_ostream& OP<<(basic_ostream& os,CO MN& n){RE os << n.RP();} TE CE Mod::Mod()NE:m_n(){}TE CE Mod::Mod(CO Mod& n)NE:m_n(n.m_n){}TE CE Mod::Mod(Mod& n)NE:m_n(n.m_n){}TE CE Mod::Mod(Mod&& n)NE:m_n(MO(n.m_n)){}TE TE CE Mod::Mod(CO T& n)NE:m_n(RS(n)){}TE TE CE Mod::Mod(T& n)NE:m_n(RS(decay_t(n))){}TE TE CE Mod::Mod(T&& n)NE:m_n(RS(forward(n))){}TE CE Mod& Mod::OP=(CO Mod& n)NE{RE Ref(m_n = n.m_n);}TE CE Mod& Mod::OP=(Mod&& n)NE{RE Ref(m_n = MO(n.m_n));}TE CE Mod& Mod::OP+=(CO Mod& n)NE{RE Ref(Normalise(m_n += n.m_n));}TE CE Mod& Mod::OP-=(CO Mod& n)NE{RE Ref(m_n < n.m_n?(m_n += M)-= n.m_n:m_n -= n.m_n);}TE CE Mod& Mod::OP*=(CO Mod& n)NE{RE Ref(m_n = COantsForMod::g_even?RS(ull(m_n)* n.m_n):MN::MU(m_n,n.m_n));}TE <> CE MP& MP::OP*=(CO MP& n)NE{ull m_n_copy = m_n;RE Ref(m_n = MO((m_n_copy *= n.m_n)< P?m_n_copy:RSP(m_n_copy)));}TE IN Mod& Mod::OP/=(CO Mod& n){RE OP*=(Mod(n).Invert());}TE CE Mod& Mod::OP<<=(int n)NE{WH(n-- > 0){Normalise(m_n <<= 1);}RE *TH;}TE CE Mod& Mod::OP>>=(int n)NE{WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += M)>>= 1;}RE *TH;}TE CE Mod& Mod::OP++()NE{RE Ref(m_n < COantsForMod::g_M_minus?++m_n:m_n = 0);}TE CE Mod Mod::OP++(int)NE{Mod n{*TH};OP++();RE n;}TE CE Mod& Mod::OP--()NE{RE Ref(m_n == 0?m_n = COantsForMod::g_M_minus:--m_n);}TE CE Mod Mod::OP--(int)NE{Mod n{*TH};OP--();RE n;}DF_OF_CM_FOR_MOD(==);DF_OF_CM_FOR_MOD(!=);DF_OF_CM_FOR_MOD(>);DF_OF_CM_FOR_MOD(>=);DF_OF_CM_FOR_MOD(<);DF_OF_CM_FOR_MOD(<=);DF_OF_AR_FOR_MOD(+,Mod(forward(n))+= *TH);DF_OF_AR_FOR_MOD(-,Mod(forward(n)).SignInvert()+= *TH);DF_OF_AR_FOR_MOD(*,Mod(forward(n))*= *TH);DF_OF_AR_FOR_MOD(/,Mod(forward(n)).Invert()*= *TH);TE CE Mod Mod::OP<<(int n)CO NE{RE MO(Mod(*TH)<<= n);}TE CE Mod Mod::OP>>(int n)CO NE{RE MO(Mod(*TH)>>= n);}TE CE Mod Mod::OP-()CO NE{RE MO(Mod(*TH).SignInvert());}TE CE Mod& Mod::SignInvert()NE{RE Ref(m_n > 0?m_n = M - m_n:m_n);}TE CE Mod& Mod::Double()NE{RE Ref(Normalise(m_n <<= 1));}TE CE Mod& Mod::Halve()NE{RE Ref(((m_n & 1)== 0?m_n:m_n += M)>>= 1);}TE IN Mod& Mod::Invert(){assert(m_n > 0);uint m_n_neg;RE m_n < COantsForMod::g_memory_LE?Ref(m_n = Inverse(m_n).m_n):((m_n_neg = M - m_n)< COantsForMod::g_memory_LE)?Ref(m_n = M - Inverse(m_n_neg).m_n):PositivePW(uint(COantsForMod::g_M_minus_2));}TE <> IN Mod<2>& Mod<2>::Invert(){assert(m_n > 0);RE *TH;}TE TE CE Mod& Mod::PositivePW(T&& EX)NE{Mod PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?OP*=(PW):*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <> TE CE Mod<2>& Mod<2>::PositivePW(T&& EX)NE{RE *TH;}TE TE CE Mod& Mod::NonNegativePW(T&& EX)NE{RE EX == 0?Ref(m_n = 1):Ref(PositivePW(forward(EX)));}TE TE CE Mod& Mod::PW(T&& EX){bool neg = EX < 0;assert(!(neg && Mod::m_n == 0));RE neg?PositivePW(forward(EX *= COantsForMod::g_M_minus_2_neg)):NonNegativePW(forward(EX));}TE IN CO Mod& Mod::Inverse(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr].m_n = M - MN::MU(memory[M % LE_curr].m_n,M / LE_curr);LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::Factorial(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::Factorial(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN CO Mod& Mod::FactorialInverse(CRUI n)NE{ST Mod memory[COantsForMod::g_memory_LE] ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr] = MN::FactorialInverse(LE_curr).Reduce();LE_curr++;}RE memory[n];}TE IN Mod Mod::Combination(CRUI n,CRUI i)NE{RE MN::Combination(n,i).Reduce();}TE CE VO Mod::swap(Mod& n)NE{std::swap(m_n,n.m_n);}TE CE CRUI Mod::RP()CO NE{RE m_n;}TE CE Mod Mod::DeRP(CRUI n)NE{Mod n_copy{};n_copy.m_n = n;RE n_copy;}TE CE uint& Mod::Normalise(uint& n)NE{RE n < M?n:n -= M;}TE IN CO Mod& Mod::zero()NE{ST CE CO Mod z{};RE z;}TE IN CO Mod& Mod::one()NE{ST CE CO Mod o{DeRP(1)};RE o;}TE TE CE Mod& Mod::Ref(T&& n)NE{RE *TH;}TE CE Mod Twice(CO Mod& n)NE{RE MO(Mod(n).Double());}TE CE Mod Half(CO Mod& n)NE{RE MO(Mod(n).Halve());}TE IN Mod Inverse(CO Mod& n){RE MO(Mod(n).Invert());}TE CE Mod Inverse_COrexpr(CRUI n)NE{RE MO(Mod::DeRP(RS(n)).NonNegativePW(M - 2));}TE CE Mod PW(Mod n,T EX){RE MO(n.PW(EX));}TE CE Mod<2> PW(Mod<2> n,const T& p){RE p == 0?Mod<2>::one():move(n);}TE CE VO swap(Mod& n0,Mod& n1)NE{n0.swap(n1);}TE IN string to_string(CO Mod& n)NE{RE to_string(n.RP())+ " + MZ";}TE IN basic_istream& OP>>(basic_istream& is,Mod& n){ll m;is >> m;n = m;RE is;}TE IN basic_ostream& OP<<(basic_ostream& os,CO Mod& n){RE os << n.RP();} // IntervalAddBIT // c:/Users/user/Documents/Programming/Mathematics/SetTheory/DirectProduct/AffineSpace/BIT/IntervalAdd/a.hpp TE CL BIT{PU:int m_SZ;VE m_fenwick;int m_PW;IN BIT(CRI SZ = 0);BIT(CO VE& a);IN BIT& OP=(BIT&& a);IN T Get(CRI i)CO;IN VO Set(CRI i,CO T& n);IN VO Set(CO VE& a);IN VO Initialise(CRI SZ = 0);IN BIT& OP+=(CO VE& a);VO Add(CRI i,CO T& n);IN CO T& LSBSegmentSum(CRI j)CO;T InitialSegmentSum(CRI i_final)CO;IN T IntervalSum(CRI i_start,CRI i_final)CO;int BinarySearch(CO T& n)CO;IN int BinarySearch(CRI i_start,CO T& n)CO;}; TE IN BIT::BIT(CRI SZ):m_SZ(SZ),m_fenwick(m_SZ+1),m_PW(1){static_assert(! is_same::value);WH(m_PW < m_SZ){m_PW <<= 1;}}TE BIT::BIT(CO VE& a):BIT(a.SZ()){for(int j = 1;j <= m_SZ;j++){T& fenwick_j = m_fenwick[j];int i = j - 1;fenwick_j = a[i];int i_lim = j -(j & -j);WH(i > i_lim){fenwick_j += m_fenwick[i];i -=(i & -i);}}}TE IN BIT& BIT::OP=(BIT&& a){m_SZ = a.m_SZ;m_fenwick = MO(a.m_fenwick);m_PW = a.m_PW;RE *TH;}TE IN T BIT::Get(CRI i)CO{RE IntervalSum(i,i);}TE IN VO BIT::Set(CRI i,CO T& n){Add(i,n - IntervalSum(i,i));}TE IN VO BIT::Set(CO VE& a){*TH = BIT{a};}TE IN VO BIT::Initialise(CRI SZ){*TH = BIT(SZ);}TE IN BIT& BIT::OP+=(CO VE&a){ BIT a_copy{ a }; assert(m_SZ == a.m_SZ);for(int i = 1;i <= m_SZ;i++){m_fenwick[i] += a.m_fenwick[i];}RE *TH;}TE VO BIT::Add(CRI i,CO T& n){int j = i + 1;WH(j <= m_SZ){m_fenwick[j] += n;j +=(j & -j);}RE;}TE IN CO T& BIT::LSBSegmentSum(CRI j)CO{assert(0 < j && j <= m_SZ);RE m_fenwick[j];}TE T BIT::InitialSegmentSum(CRI i_final)CO{T sum = 0;int j =(i_final < m_SZ?i_final:m_SZ - 1)+ 1;WH(j > 0){sum += m_fenwick[j];j -= j & -j;}RE sum;}TE IN T BIT::IntervalSum(CRI i_start,CRI i_final)CO{RE InitialSegmentSum(i_final)- InitialSegmentSum(i_start - 1);}TE int BIT::BinarySearch(CO T& n)CO{int PW = m_PW;int j = 0;T sum{};T sum_next{};WH(PW > 0){int j_next = j | PW;if(j_next < m_SZ){sum_next += m_fenwick[j_next];if(sum_next < n){sum = sum_next;j = j_next;}else{sum_next = sum;}}PW >>= 1;}RE j;}TE IN int BIT::BinarySearch(CRI i_start,CO T& n)CO{RE max(i_start,BinarySearch(InitialSegmentSum(i_start)+ n));} TE CL IntervalAddBIT{PU:BIT m_bit_0;BIT m_bit_1;IN IntervalAddBIT(CRI SZ = 0);IN IntervalAddBIT(CO VE& a);IN IntervalAddBIT& OP=(IntervalAddBIT&& a);IN T Get(CRI i)CO;IN VO Set(CRI i,CO T& n);IN VO Set(CO VE& a);IN VO Initialise(CRI SZ = 0);IN IntervalAddBIT& OP+=(CO VE& a);IN VO Add(CRI i,CO T& n);IN VO IntervalAdd(CRI i_start,CRI i_final,CO T& n);IN T InitialSegmentSum(CRI i_final)CO;IN T IntervalSum(CRI i_start,CRI i_final)CO;}; TE IN IntervalAddBIT::IntervalAddBIT(CRI SZ):m_bit_0(SZ),m_bit_1(SZ){}TE IN IntervalAddBIT::IntervalAddBIT(CO VE& a):m_bit_0(),m_bit_1(){CO int SZ = a.SZ();VE diff(SZ);diff[0]= a[0];for(int i = 1;i < SZ;i++){diff[i] = a[i] - a[i-1];}m_bit_0.Set(diff);for(int i = 1;i < SZ;i++){(diff[i]*= 1 - i)-= a[i];}m_bit_1.Set(diff);}TE IN IntervalAddBIT& IntervalAddBIT::OP=(IntervalAddBIT&& a){m_bit_0 = MO(a.m_bit_0);m_bit_1 = MO(a.m_bit_1);}TE IN T IntervalAddBIT::Get(CRI i)CO{RE IntervalSum(i,i);}TE IN VO IntervalAddBIT::Set(CRI i,CO T& n){Add(i,n - IntervalSum(i,i));}TE IN VO IntervalAddBIT::Set(CO VE& a){*TH = IntervalAddBIT(a);}TE IN VO IntervalAddBIT::Initialise(CO int& SZ){m_bit_0.Initialise(SZ);m_bit_1.Initialise(SZ);}TE IN IntervalAddBIT& IntervalAddBIT::OP+=(CO VE& a){IntervalAddBIT a_copy{a};CO int SZ = a.SZ();for(int i = 1;i < SZ;i++){m_bit_0[i] += a_copy.m_bit_0[i];m_bit_1[i] += a_copy.m_bit_1[i];}RE *TH;}TE IN VO IntervalAddBIT::Add(CRI i,CO T& n){IntervalAdd(i,i,n);}TE IN VO IntervalAddBIT::IntervalAdd(CRI i_start,CRI i_final,CO T& n){m_bit_0.Add(i_start,-(i_start - 1)* n);m_bit_0.Add(i_final + 1,i_final * n);m_bit_1.Add(i_start,n);m_bit_1.Add(i_final + 1,- n);}TE IN T IntervalAddBIT::InitialSegmentSum(CRI i_final)CO{RE m_bit_0.InitialSegmentSum(i_final)+ i_final * m_bit_1.InitialSegmentSum(i_final);}TE IN T IntervalAddBIT::IntervalSum(CRI i_start,CRI i_final)CO{RE InitialSegmentSum(i_final)- InitialSegmentSum(i_start - 1);} // AAA 常設ライブラリは以上に挿入する。 #define INCLUDE_LIBRARY #include __FILE__ #endif // INCLUDE_LIBRARY #endif // INCLUDE_SUB #endif // INCLUDE_MAIN