結果
問題 | No.2770 Coupon Optimization |
ユーザー | 👑 p-adic |
提出日時 | 2024-06-01 15:02:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 640 ms / 3,000 ms |
コード長 | 60,017 bytes |
コンパイル時間 | 11,616 ms |
コンパイル使用メモリ | 346,016 KB |
実行使用メモリ | 31,056 KB |
最終ジャッジ日時 | 2024-06-01 15:02:24 |
合計ジャッジ時間 | 22,732 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 574 ms
30,800 KB |
testcase_04 | AC | 559 ms
30,928 KB |
testcase_05 | AC | 18 ms
5,376 KB |
testcase_06 | AC | 622 ms
30,800 KB |
testcase_07 | AC | 572 ms
30,800 KB |
testcase_08 | AC | 613 ms
31,056 KB |
testcase_09 | AC | 146 ms
10,640 KB |
testcase_10 | AC | 314 ms
17,060 KB |
testcase_11 | AC | 273 ms
16,312 KB |
testcase_12 | AC | 306 ms
17,964 KB |
testcase_13 | AC | 291 ms
16,860 KB |
testcase_14 | AC | 638 ms
30,932 KB |
testcase_15 | AC | 611 ms
30,908 KB |
testcase_16 | AC | 612 ms
31,052 KB |
testcase_17 | AC | 640 ms
30,804 KB |
ソースコード
#ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE // #define USE_GETLINE #endif #ifdef INCLUDE_MAIN IN VO Solve() { CIN( ll , N , M ); CIN_A( ll , 0 , N , A ); CIN_A( ll , 0 , M , B ); sort( A ); FOR( i , 0 , N ){ A[i] /= 100; } sort( B , true ); B.resize( N ); FOR( j , 0 , N ){ B[j] = 100 - B[j]; } Polynomial<ll> a{ move( A ) }; Polynomial<ll> b{ move( B ) }; a *= b; FOR( i , 0 , N ){ COUT( a[i] ); } } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_SUB // 圧縮時は中身だけ削除する。 IN VO Experiment() { } // 圧縮時は中身だけ削除する。 IN VO SmallTest() { } // 圧縮時は中身だけ削除する。 IN VO RandomTest() { } #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_SUB #ifdef INCLUDE_LIBRARY // VVV 常設でないライブラリは以下に挿入する。 #ifdef DEBUG #include "c:/Users/user/Documents/Programming/Mathematics/Polynomial/Truncate/NonProth/a_Body.hpp" #else #define PO Polynomial #define TRPO TruncatedPolynomial TE <TY T,int EX_lim>CL PW3PW_CE{PU:T m_val[EX_lim];CE PW3PW_CE(CO T& t);CE CO T& OP[](CRI i)CO;CE CO T(&Get()CO)[EX_lim];}; TE <TY T,int EX_lim> CE PW3PW_CE<T,EX_lim>::PW3PW_CE(CO T& t):m_val(){T PW{t};for(uint EX = EX_lim - 1;EX + 1 > 0;EX--){m_val[EX]= -PW;m_val[EX]*= PW *= PW;}}TE <TY T,int EX_lim> CE CO T& PW3PW_CE<T,EX_lim>::OP[](CRI i)CO{AS(i < EX_lim);RE m_val[i];}TE <TY T,int EX_lim> CE CO T(&PW3PW_CE<T,EX_lim>::Get()CO)[EX_lim]{RE m_val;} #define PS_FOR_FFT(MOD,LE,BORDER,PR,IPR,MINT)ST_AS((MINT<MOD>::DeRP(PR)*= MINT<MOD>::DeRP(IPR))== MINT<MOD>::DeRP(1));TE <> CE CO uint LimitOfPWForFFT<MINT<MOD> > = LE - 1;TE <> CE CO uint BorderForFFT<MINT<MOD> > = BORDER;TE <> IN CO MINT<MOD>(&PrimitiveRootOfTwoForFFT()NE)[LimitOfPWForFFT<MINT<MOD> >]{ST CE PW3PW_CE<MINT<MOD>,LimitOfPWForFFT<MINT<MOD> > > PRT{PR};ST_AS(PRT.m_val[0]== MINT<MOD>::DeRP(1));RE PRT.Get();}TE <> IN CO MINT<MOD>(&InversePrimitiveRootOfTwoForFFT()NE)[LimitOfPWForFFT<MINT<MOD> >]{ST CE PW3PW_CE<MINT<MOD>,LimitOfPWForFFT<MINT<MOD> > > IPRT{IPR};ST_AS(IPRT.m_val[0]== MINT<MOD>::DeRP(1)&&(MINT<MOD>::DeRP(PR)*= MINT<MOD>::DeRP(IPR))== MINT<MOD>::DeRP(1));RE IPRT.Get();} TE <TY T> CE CO uint LimitOfPWForFFT{};TE <TY T> CE CO uint BorderForFFT{};TE <TY T> IN CO T(&PrimitiveRootOfTwoForFFT()NE)[LimitOfPWForFFT<T>];TE <TY T> IN CO T(&InversePrimitiveRootOfTwoForFFT()NE)[LimitOfPWForFFT<T>]; PS_FOR_FFT(998244353,24,4,31,128805723,Mod);PS_FOR_FFT(167772161,26,4,17,29606852,Mod);PS_FOR_FFT(469762049,27,4,30,15658735,Mod);PS_FOR_FFT(754974721,25,4,362,415027540,Mod); TE <TY T> VO CooleyTukey(VE<T>& f,CRUI N_input_start,CRUI N_input_lim,CRUI N_output_start,CRUI N_output_lim,CRUI two_PW,CRUI EX,CO T(&PRT)[LimitOfPWForFFT<T>]){CO uint LE = two_PW + N_input_start + N_output_start;f.reserve(LE);WH(f.SZ()< LE){f.push_back(0);}ST VE<uint> bit_reverse[32]={VE<uint>(1)};ST uint e_next = 1;ST uint two_PW_next = 1;ST uint two_PW_next2 = 2;ST VE<uint>* p_bit_reverse_prev = bit_reverse;ST VE<uint>* p_bit_reverse_curr = p_bit_reverse_prev + 1;WH(e_next <= EX){*p_bit_reverse_curr = VE<uint>(two_PW_next2);uint* p_bit_reverse_curr_i = &((*p_bit_reverse_curr)[0]);uint* p_bit_reverse_curr_i_plus = p_bit_reverse_curr_i + two_PW_next;uint* p_bit_reverse_prev_i = &((*p_bit_reverse_prev)[0]);for(uint i = 0;i < two_PW_next;i++){(*(p_bit_reverse_curr_i_plus++)= *(p_bit_reverse_curr_i++)= *(p_bit_reverse_prev_i++)* 2)+= 1;}e_next++;swap(two_PW_next,two_PW_next2);two_PW_next2 *= 4;p_bit_reverse_prev++;p_bit_reverse_curr++;}CO VE<uint>& bit_reverse_EX = bit_reverse[EX];uint bit_num = 0;CO uint* p_bit_num_reverse = &(bit_reverse_EX[bit_num]);WH(bit_num < two_PW){if(*p_bit_num_reverse < bit_num){swap(f[*p_bit_num_reverse + N_input_start],f[bit_num + N_input_start]);}bit_num++;p_bit_num_reverse++;}CO T& one = PRT[0];T zeta,diff;uint i,j,j_lim,two_PW_curr = 1,two_PW_curr_2 = 2;WH(two_PW_curr < two_PW){CO uint N_input_start_plus = N_input_start + two_PW_curr;bit_num = i = 0;zeta = one;WH(i < two_PW){j = i;j_lim = i + two_PW_curr;WH(j < j_lim){diff = f[j + N_input_start] - f[j + N_input_start_plus];f[j + N_input_start] += f[j + N_input_start_plus];f[j + N_input_start_plus] = zeta * diff;j++;}bit_num++;i += two_PW_curr_2;j = 0;WH(true){if(((bit_num >> j)& 1)== 1){zeta *= PRT[j+1];break;}j++;}}two_PW_curr <<= 1;two_PW_curr_2 <<= 1;}CO uint LE_fixed = N_output_lim + N_input_start;WH(f.SZ()> LE_fixed){f.pop_back();}for(uint i = 0;i < N_output_start;i++){f[N_input_start + i]= 0;}RE;} TE <TY T> IN VO FFT(VE<T>& f,CRUI N_input_start,CRUI N_input_lim,CRUI two_PW,CRUI EX){CooleyTukey<T>(f,N_input_start,N_input_lim,0,two_PW,two_PW,EX,PrimitiveRootOfTwoForFFT<T>());}TE <TY T> IN VO FFT(VE<T>& f,CRUI N_input_start,CRUI N_input_lim,CRUI N_output_start,CRUI N_output_lim,CRUI two_PW,CRUI EX){CooleyTukey<T>(f,N_input_start,N_input_lim,N_output_start,N_output_lim,two_PW,EX,PrimitiveRootOfTwoForFFT<T>());}TE <TY T> IN VO IFFT(VE<T>& f,CRUI N_input_start,CRUI N_input_lim,CRUI two_PW,CO T& two_PW_inv,CRUI EX){CooleyTukey<T>(f,N_input_start,N_input_lim,0,two_PW,two_PW,EX,InversePrimitiveRootOfTwoForFFT<T>());CO uint SZ = two_PW + N_input_start;for(uint i = N_input_start;i < SZ;i++){f[i]*= two_PW_inv;}}TE <TY T> IN VO IFFT(VE<T>& f,CRUI N_input_start,CRUI N_input_lim,CRUI N_output_start,CRUI N_output_lim,CRUI two_PW,CO T& two_PW_inv,CRUI EX){CooleyTukey<T>(f,N_input_start,N_input_lim,N_output_start,N_output_lim,two_PW,EX,InversePrimitiveRootOfTwoForFFT<T>());CO uint SZ = N_output_lim + N_input_start;for(uint i = N_output_start + N_input_start;i < SZ;i++){f[i]*= two_PW_inv;}} #define SFINAE_FOR_PO(DEFAULT)TY Arg,enable_if_t<is_COructible_v<T,decay_t<Arg>>>* DEFAULT #define DC_OF_AR_FOR_PO(FUNC)IN PO<T> OP FUNC(PO<T> f)CO;IN PO<T> OP FUNC(T t)CO #define DF_OF_AR_FOR_PO(FUNC,DEF)TE <TY T> IN PO<T> PO<T>::OP FUNC(PO<T> f)CO{RE MO(DEF);};TE <TY T> IN PO<T> PO<T>::OP FUNC(T t)CO{RE *TH FUNC PO<T>(MO(t));} TE <TY T>CL TRPO;TE <TY T>CL PO{PU:VE<T> m_f;uint m_SZ;IN PO();IN PO(CO PO<T>& f);IN PO(PO<T>&& f);IN PO(TRPO<T> f);IN PO(VE<T> f);IN PO(T t);TE <SFINAE_FOR_PO(= nullptr)> IN PO(Arg n);IN PO(CRUI i,T t);TE <SFINAE_FOR_PO(= nullptr)> IN PO(CRUI i,Arg n);TE <SFINAE_FOR_PO(= nullptr)> IN PO<T>& OP=(Arg n);IN PO<T>& OP=(PO<T> f);IN PO<T>& OP=(VE<T> f);IN CO T& OP[](CRUI i)CO;IN T& OP[](CRUI i);IN T OP()(CO T& t)CO;PO<T>& OP+=(CO PO<T>& f);PO<T>& OP-=(CO PO<T>& f);PO<T>& OP*=(CO PO<T>& f);PO<T>& OP*=(PO<T>&& f);IN PO<T>& OP/=(CO PO<T>& f);PO<T>& OP/=(CO T& t);PO<T>& OP%=(CO PO<T>& f);PO<T>& OP%=(CO T& t);bool OP==(CO PO<T>& f)CO;bool OP==(CO T& t)CO;TE <TY P> IN bool OP!=(CO P& f)CO;DC_OF_AR_FOR_PO(+);IN PO<T> OP-()CO;DC_OF_AR_FOR_PO(-);DC_OF_AR_FOR_PO(*);IN PO<T> OP/(CO PO<T>& f)CO;IN PO<T> OP/(CO T& t)CO;IN PO<T> OP%(CO PO<T>& f)CO;IN PO<T> OP%(CO T& t)CO;IN CO VE<T>& GetCoefficient()CO NE;IN CRUI SZ()CO NE;IN VO reSZ(CRUI deg_plus)NE;IN VO swap(PO<T>& f);IN VO swap(VE<T>& f);VO ReMORedundantZero();IN string Display()CO NE;ST PO<T> Quotient(CO PO<T>& f0,CO PO<T>& f1);ST PO<T> TP(CO PO<T>& f,CRUI f_TP_SZ);ST IN CO PO<T>& zero();ST IN CO PO<T>& one();ST IN CO T& c_zero();ST IN CO T& c_one();ST IN CO T& c_minus_one();IN PO<T>& SignInvert();}; #define DF_BODY_OF_PS_OF_MU_OF_PO_PROTH_MOD(TYPE,ARG,RHS)TE <> PO<TYPE>& PO<TYPE>::OP*=(ARG f){if(m_SZ != 0){VE<TYPE> v{};v.swap(m_f);TRPO<TYPE> TH_copy{m_SZ + f.m_SZ - 1,MO(v)};TH_copy *= RHS;m_f = MO(TH_copy.PO<TYPE>::m_f);m_SZ = m_f.SZ();}RE *TH;} #define RE_ZERO_FOR_MU_FOR_TR_PO_IF(CONDITION)if(CONDITION){RE OP=(zero);} #define RE_ZERO_FOR_TR_MU_CO_FOR_TR_PO_IF(CONDITION)if(CONDITION){RE TRPO<T>(m_N);} #define RE_ZERO_FOR__FOR_TR_PO_IF(MU,CONDITION)RE_ZERO_FOR_ ## MU ## _FOR_TR_PO_IF(CONDITION) #define SET_VE_FOR_AN_OF_MU_FOR_TR_PO(N_OUTPUT_LIM)if(PO<T>::m_SZ < N_OUTPUT_LIM){for(uint i = PO<T>::m_SZ;i < N_OUTPUT_LIM;i++){PO<T>::m_f.push_back(0);}PO<T>::m_SZ = N_OUTPUT_LIM;} #define SET_VE_FOR_AN_OF_TR_MU_CO_FOR_TR_PO(N_OUTPUT_LIM)VE<T> AN(N_OUTPUT_LIM) #define SET_VE_FOR_AN_OF__FOR_TR_PO(MU,N_OUTPUT_LIM)SET_VE_FOR_AN_OF_ ## MU ## _FOR_TR_PO(N_OUTPUT_LIM) #define SET_SUM_OF_MU_FOR_TR_PO PO<T>::m_f[i]= sum #define SET_SUM_OF_TR_MU_CO_FOR_TR_PO AN[i]= sum #define SET_SUM_OF__FOR_TR_PO(MU)SET_SUM_OF_ ## MU ## _FOR_TR_PO #define SET_N_INPUT_START_FOR_MU_FOR_TR_PO(F,SZ,N_INPUT_START_NUM)uint N_INPUT_START_NUM{};for(uint i = 0;i < SZ && searching;i++){if(F[i]!= zero){N_INPUT_START_NUM = i;searching = false;}} #define SET_N_INPUT_MAX_FOR_MU_FOR_TR_PO(F,SZ,N_INPUT_MAX_NUM)uint N_INPUT_MAX_NUM{};searching = true;for(uint i =(SZ)- 1;searching;i--){if(F[i]!= zero){N_INPUT_MAX_NUM = i;searching = false;}} #define CN_FOR_MU_FOR_TR_PO(J_MIN)CO uint j_max = i < N_input_max_0_start_1?i - N_input_start_1:N_input_max_0;T sum{zero};for(uint j = J_MIN;j <= j_max;j++){sum += PO<T>::m_f[j]* f.PO<T>::m_f[i - j];}PO<T>::m_f[i]= sum; #define CN_FOR_TR_MU_CO_FOR_TR_PO(J_MIN)CO uint j_max = i < N_input_max_0_start_1?i - N_input_start_1:N_input_max_0;T& m_fi = AN[i];for(uint j = J_MIN;j <= j_max;j++){m_fi += PO<T>::m_f[j]* f.PO<T>::m_f[i - j];} #define CN_FOR__FOR_TR_PO(MU,J_MIN)CN_FOR_ ## MU ## _FOR_TR_PO(J_MIN) #define ZEROIFICATION_FOR_MU_FOR_TR_PO for(uint i = 0;i < N_input_start_0_start_1;i++){PO<T>::m_f[i]= 0;} #define ZEROIFICATION_FOR_TR_MU_CO_FOR_TR_PO CRUI N_output_start_fixed = N_output_start < N_input_start_0_start_1?N_output_start:N_input_start_0_start_1;for(uint i = 0;i < N_output_start_fixed;i++){AN[i]= 0;} #define ZEROIFICATION_FOR__FOR_TR_PO(MU)ZEROIFICATION_FOR_ ## MU ## _FOR_TR_PO #define DF_0_OF__FOR_TR_PO(MU,ACCESS_ENTRY,N_OUTPUT_START)RE_ZERO_FOR__FOR_TR_PO_IF(MU,PO<T>::m_SZ == 0);uint N_output_max = PO<T>::m_SZ + f.PO<T>::m_SZ - 2;if(N_output_max >= m_N){N_output_max = m_N - 1;}CO uint N_output_lim = N_output_max + 1;SET_VE_FOR_AN_OF__FOR_TR_PO(MU,N_output_lim);for(uint i = N_output_max;searching;i--){T sum{zero};for(uint j = 0;j <= i;j++){sum += ACCESS_ENTRY * f.PO<T>::OP[](i - j);}SET_SUM_OF__FOR_TR_PO(MU);searching = i > N_OUTPUT_START;} #define DF_1_OF__FOR_TR_PO(MU)SET_N_INPUT_START_FOR_MU_FOR_TR_PO(PO<T>::m_f,PO<T>::m_SZ,N_input_start_0);RE_ZERO_FOR__FOR_TR_PO_IF(MU,searching);searching = true;SET_N_INPUT_START_FOR_MU_FOR_TR_PO(f,f.PO<T>::m_SZ,N_input_start_1); #define SET_N_INPUT_RANGE SET_N_INPUT_MAX_FOR_MU_FOR_TR_PO(PO<T>::m_f,PO<T>::m_SZ,N_input_max_0);SET_N_INPUT_MAX_FOR_MU_FOR_TR_PO(f,f.PO<T>::m_SZ < m_N?f.PO<T>::m_SZ:m_N,N_input_max_1);CO uint N_input_max_0_max_1 = N_input_max_0 + N_input_max_1;CO uint N_input_start_0_start_1 = N_input_start_0 + N_input_start_1;uint N_output_lim_fixed = N_input_max_0_max_1 < m_N?N_input_max_0_max_1 + 1:m_N; #define DF_3_OF__FOR_TR_PO(MU)CO uint N_input_start_0_max_1 = N_input_start_0 + N_input_max_1;CO uint N_input_max_0_start_1 = N_input_max_0 + N_input_start_1;CO uint N_output_max_fixed = N_output_lim_fixed - 1;SET_VE_FOR_AN_OF__FOR_TR_PO(MU,N_output_lim_fixed);for(uint i = N_output_max_fixed;i > N_input_start_0_max_1;i--){CN_FOR__FOR_TR_PO(MU,i - N_input_max_1);}searching = true;for(uint i = N_input_start_0_max_1 < N_output_max_fixed?N_input_start_0_max_1:N_output_max_fixed;searching;i--){CN_FOR__FOR_TR_PO(MU,N_input_start_0);searching = i > N_input_start_0_start_1;}ZEROIFICATION_FOR__FOR_TR_PO(MU); #define SET_SHIFTED_VE_FOR_MU(V,F,I_START,I_MAX,I_SHIFT)VE<T> V(product_LE);for(uint i = I_START;i <= I_MAX;i++){V[I_SHIFT + i]= F[i];} #define DF_OF_MU_FOR_TR_PO(RE_LINE_0,RE_LINE_1,RE_LINE_2,RE_LINE_3,RE_LINE_4,MU,ACCESS_ENTRY,N_OUTPUT_START,FIX_N_OUTPUT_LIM)CE CRUI border_0 = FFT_MU_border_0<T>;CO T& zero = PO<T>::c_zero();bool searching = true;if(PO<T>::m_SZ < border_0 && f.PO<T>::m_SZ < border_0){RE_LINE_0;DF_0_OF__FOR_TR_PO(MU,ACCESS_ENTRY,N_OUTPUT_START);RE_LINE_1;}DF_1_OF__FOR_TR_PO(MU);RE_LINE_2;SET_N_INPUT_RANGE;FIX_N_OUTPUT_LIM;RE_LINE_3;DF_3_OF__FOR_TR_PO(MU);RE_LINE_4; #define DF_OF_FFT_MU_FOR_TR_PO(RE_LINE_0,RE_LINE_1,RE_LINE_2,RE_LINE_3,RE_LINE_4,RE_LINE_5,MU,ACCESS_ENTRY,N_OUTPUT_START,N_OUTPUT_START_SHIFTED,FIX_N_OUTPUT_LIM,DC_OF_F0,N_INPUT_START_0,N_INPUT_LIM_0,DC_OF_F1,N_INPUT_START_1,N_INPUT_LIM_1,VE_FOR_IFFT,RESZ_VE_FOR_IFFT,I_START,MU_FORMULA,SET_AN)CE CRUI border_0 = FFT_MU_border_0<T>;CO T& zero = PO<T>::c_zero();bool searching = true;if(PO<T>::m_SZ < border_0 && f.PO<T>::m_SZ < border_0){RE_LINE_0;DF_0_OF__FOR_TR_PO(MU,ACCESS_ENTRY,N_OUTPUT_START);RE_LINE_1;}DF_1_OF__FOR_TR_PO(MU);RE_LINE_2;SET_N_INPUT_RANGE;FIX_N_OUTPUT_LIM;RE_LINE_3;CO uint N_input_TR_deg_0_deg_1 = N_input_max_0 - N_input_start_0 + N_input_max_1 - N_input_start_1;CE CRUI border_1 = FFT_MU_border_1<T>;if(N_input_TR_deg_0_deg_1 < border_1){DF_3_OF__FOR_TR_PO(MU);RE_LINE_4;}uint two_PW = FFT_MU_border_1_2<T>;uint EX = FFT_MU_border_1_2_EX<T>;T two_PW_inv{FFT_MU_border_1_2_inv<T>};WH(N_input_TR_deg_0_deg_1 >= two_PW){two_PW *= 2;two_PW_inv /= 2;EX++;}CO uint product_LE = N_input_start_0_start_1 + two_PW;DC_OF_F0;DC_OF_F1;FFT<T>(f0,N_INPUT_START_0,N_INPUT_LIM_0,two_PW,EX);FFT<T>(f1,N_INPUT_START_1,N_INPUT_LIM_1,two_PW,EX);RESZ_VE_FOR_IFFT;for(uint i = I_START + two_PW - 1;i + 1 > I_START;i--){MU_FORMULA;}CO uint N_output_lim_shifted = N_output_lim_fixed - N_input_start_0_start_1;CO uint N_output_start_shifted = min(N_output_lim_shifted,uint(N_OUTPUT_START_SHIFTED));IFFT<T>(VE_FOR_IFFT,N_input_start_0_start_1,product_LE,N_output_start_shifted,N_output_lim_shifted,two_PW,two_PW_inv,EX);SET_AN;RE_LINE_5; #define DF_OF_INVERSE_FOR_TR_PO(TYPE,RECURSION)CRUI N = f.GetTruncation();uint PW;uint PW_2 = 1;TRPO< TYPE > f_inv{PW_2,PO< TYPE >::c_one()/ f[0]};WH(PW_2 < N){PW = PW_2;PW_2 *= 2;f_inv.SetTruncation(PW_2);RECURSION;}f_inv.SetTruncation(N);RE f_inv #define DF_OF_EXP_FOR_TR_PO(TYPE,RECURSION)assert(f[0]== PO< TYPE >::c_zero());CRUI N = f.GetTruncation();uint PW;uint PW_2 = 1;TRPO< TYPE > f_exp{PW_2,PO< TYPE >::c_one()};WH(PW_2 < N){PW = PW_2;PW_2 *= 2;f_exp.SetTruncation(PW_2);RECURSION;}f_exp.SetTruncation(N);RE f_exp #define DF_OF_PS_OF_MU_OF_TR_PO(TYPE,BORDER_0,BORDER_1,BORDER_1_2,BORDER_1_2_EX,BORDER_1_2_INV)TE <> CE CO uint FFT_MU_border_0< TYPE > = BORDER_0;TE <> CE CO uint FFT_MU_border_1< TYPE > = BORDER_1;static_assert(FFT_MU_border_0< TYPE > <= FFT_MU_border_1< TYPE >);TE <> CE CO uint FFT_MU_border_1_2< TYPE > = BORDER_1_2;static_assert(FFT_MU_border_1< TYPE > < FFT_MU_border_1_2< TYPE > && FFT_MU_border_1_2< TYPE > <= FFT_MU_border_1< TYPE > * 2 );TE <> CE CO uint FFT_MU_border_1_2_EX< TYPE > = BORDER_1_2_EX;static_assert(FFT_MU_border_1_2< TYPE > == 1 << FFT_MU_border_1_2_EX< TYPE > );TE <> CE CO uint FFT_MU_border_1_2_inv< TYPE > = BORDER_1_2_INV;static_assert((TYPE::DeRP(FFT_MU_border_1_2< TYPE >)*= TYPE::DeRP(FFT_MU_border_1_2_inv< TYPE >))== TYPE::DeRP(1));TE <> IN TRPO< TYPE >& TRPO< TYPE >::OP*=(CO PO< TYPE >& f){RE TRPO< TYPE >::FFT_MU(f);}TE <> IN TRPO< TYPE >& TRPO< TYPE >::OP*=(PO< TYPE >&& f){RE TRPO< TYPE >::FFT_MU(MO(f));}TE <> TRPO< TYPE > Inverse(CO TRPO< TYPE >& f){DF_OF_INVERSE_FOR_TR_PO(TYPE,f_inv.TRMinus(f_inv.FFT_TRMU_CO(f,PW,PW_2).FFT_TRMU(f_inv,PW,PW_2),PW,PW_2));}TE <> TRPO< TYPE > Exp(CO TRPO< TYPE >& f){DF_OF_EXP_FOR_TR_PO(TYPE,f_exp.TRMinus((TRIntegral(Differential(f_exp).FFT_TRMU_CO(Inverse(f_exp),PW - 1,PW_2),PW).TRMinus(f,PW,PW_2)).FFT_TRMU(f_exp,PW,PW_2),PW,PW_2));} #define DF_OF_PS_OF_MU_OF_PO_PROTH_MOD(MOD,BORDER_1_2_INV,MINT)DF_OF_PS_OF_MU_OF_TR_PO(MINT<MOD>,17,512,1024,10,BORDER_1_2_INV);DF_BODY_OF_PS_OF_MU_OF_PO_PROTH_MOD(MINT<MOD>,CO PO<MINT<MOD> >&,TH == &f?TH_copy:f);DF_BODY_OF_PS_OF_MU_OF_PO_PROTH_MOD(MINT<MOD>,PO<MINT<MOD> >&&,MO(f)); TE <TY T>CL TRPO :PU PO<T>{PU:uint m_N;PU:IN TRPO(CRUI N = 0);IN TRPO(CO TRPO<T>& f);IN TRPO(TRPO<T>&& f);IN TRPO(CRUI N,T t);IN TRPO(CRUI N,CO PO<T>& f);IN TRPO(CRUI N,PO<T>&& f);IN TRPO(CRUI N,VE<T>&& f);IN TRPO(CRUI N,CRUI i,T t);TE <SFINAE_FOR_PO(= nullptr)> IN TRPO(CRUI N,CRUI i,CO Arg& t);IN TRPO<T>& OP=(TRPO<T> f);TE <SFINAE_FOR_PO(= nullptr)> IN TRPO<T>& OP=(Arg n);IN TRPO<T>& OP=(PO<T> f);IN TRPO<T>& OP+=(CO T& t);IN TRPO<T>& OP+=(CO PO<T>& f);IN TRPO<T>& OP+=(CO TRPO<T>& f);TRPO<T>& TRPlus(CO PO<T>& f,CRUI N_input_start,CRUI N_input_limit);IN TRPO<T>& OP-=(CO T& t);IN TRPO<T>& OP-=(CO PO<T>& f);IN TRPO<T>& OP-=(CO TRPO<T>& f);TRPO<T>& TRMinus(CO PO<T>& f,CRUI N_input_start,CRUI N_input_limit);IN TRPO<T>& OP*=(CO T& t);TRPO<T>& OP*=(CO PO<T>& f);IN TRPO<T>& OP*=(PO<T>&& f);TRPO<T>& FFT_MU(CO PO<T>& f);TRPO<T>& TRMU(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim);TRPO<T>& FFT_TRMU(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim);TRPO<T>& FFT_TRMU(PO<T>&& f,CRUI N_output_start,CRUI N_output_lim);TRPO<T> TRMU_CO(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim)CO;TRPO<T> FFT_TRMU_CO(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim)CO;TRPO<T> FFT_TRMU_CO(PO<T>&& f,CRUI N_output_start,CRUI N_output_lim)CO;IN TRPO<T>& OP/=(CO T& t);IN TRPO<T>& OP/=(CO TRPO<T>& t);IN TRPO<T>& OP%=(CO T& t);IN TRPO<T> OP-()CO;IN VO SetTruncation(CRUI N)NE;IN CRUI GetTruncation()CO NE;IN TRPO<T>& TruncateInitial(CRUI N)NE;IN TRPO<T>& TruncateFinal(CRUI N)NE;};TE <TY T> CE CO uint FFT_MU_border_0 = 17;TE <TY T> CE CO uint FFT_MU_border_1{};TE <TY T> CE CO uint FFT_MU_border_1_2{};TE <TY T> CE CO uint FFT_MU_border_1_2_EX{};TE <TY T> CE CO uint FFT_MU_border_1_2_inv{}; TE <TY T> IN TRPO<T>::TRPO(CRUI N):PO<T>(),m_N(N){}TE <TY T> IN TRPO<T>::TRPO(CO TRPO<T>& f):PO<T>(f),m_N(f.m_N){}TE <TY T> IN TRPO<T>::TRPO(TRPO<T>&& f):PO<T>(MO(f.m_f)),m_N(f.m_N){}TE <TY T> IN TRPO<T>::TRPO(CRUI N,T t):PO<T>(MO(t)),m_N(N){}TE <TY T> IN TRPO<T>::TRPO(CRUI N,CO PO<T>& f):PO<T>(),m_N(N){PO<T>::m_SZ = f.PO<T>::m_SZ < m_N?f.PO<T>::m_SZ:m_N;PO<T>::m_f = VE<T>(PO<T>::m_SZ);for(uint i = 0;i < PO<T>::m_SZ;i++){PO<T>::m_f[i]= f.PO<T>::m_f[i];}}TE <TY T> IN TRPO<T>::TRPO(CRUI N,PO<T>&& f):PO<T>(),m_N(N){if(f.PO<T>::m_SZ < m_N * 2){PO<T>::OP=(MO(f));if(f.PO<T>::m_SZ > m_N){TruncateFinal(m_N);}}else{PO<T>::m_f = VE<T>(m_N);for(uint i = 0;i < m_N;i++){PO<T>::m_f[i]= MO(f.PO<T>::m_f[i]);}PO<T>::m_SZ = m_N;}}TE <TY T> IN TRPO<T>::TRPO(CRUI N,VE<T>&& f):PO<T>(),m_N(N){CO uint f_SZ = f.SZ();if(f_SZ < m_N * 2){PO<T>::OP=(MO(f));if(f_SZ > m_N){TruncateFinal(m_N);}}else{PO<T>::m_f = VE<T>(m_N);for(uint i = 0;i < m_N;i++){PO<T>::m_f[i]= MO(f[i]);}}}TE <TY T> IN TRPO<T>::TRPO(CRUI N,CRUI i,T t):PO<T>(),m_N(N){if(i < m_N?t != PO<T>::c_zero():false){PO<T>::OP[](i)= MO(t);}}TE <TY T> TE <SFINAE_FOR_PO()> IN TRPO<T>::TRPO(CRUI N,CRUI i,CO Arg& n):TRPO(N,i,T(n)){}TE <TY T> IN TRPO<T>& TRPO<T>::OP=(TRPO<T> f){PO<T>::OP=(MO(f.m_f));m_N = f.m_N;RE *TH;}TE <TY T> TE <SFINAE_FOR_PO()> IN TRPO<T>& TRPO<T>::OP=(Arg n){PO<T>::OP=(MO(n));RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP=(PO<T> f){RE OP=(TRPO<T>(m_N,MO(f)));}TE <TY T> IN TRPO<T>& TRPO<T>::OP+=(CO T& t){PO<T>::OP+=(t);RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP+=(CO PO<T>& f){RE TRPlus(f,0,f.m_SZ);}TE <TY T> IN TRPO<T>& TRPO<T>::OP+=(CO TRPO<T>& f){RE m_N == 0?OP=(f):TRPlus(f,0,f.PO<T>::m_SZ);}TE <TY T>TRPO<T>& TRPO<T>::TRPlus(CO PO<T>& f,CRUI N_input_start,CRUI N_input_lim){CRUI SZ = N_input_lim < m_N?N_input_lim < f.PO<T>::m_SZ?N_input_lim:f.PO<T>::m_SZ:m_N < f.PO<T>::m_SZ?m_N:f.PO<T>::m_SZ;if(PO<T>::m_SZ < SZ){PO<T>::m_f.reserve(SZ);for(uint i = N_input_start;i < PO<T>::m_SZ;i++){PO<T>::m_f[i]+= f.PO<T>::m_f[i];}for(uint i = PO<T>::m_SZ;i < SZ;i++){PO<T>::m_f.push_back(f.PO<T>::m_f[i]);}PO<T>::m_SZ = SZ;}else{for(uint i = N_input_start;i < SZ;i++){PO<T>::m_f[i]+= f.PO<T>::m_f[i];}}RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP-=(CO T& t){PO<T>::OP-=(t);RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP-=(CO PO<T>& f){RE TRMinus(f,0,f.m_SZ);}TE <TY T> IN TRPO<T>& TRPO<T>::OP-=(CO TRPO<T>& f){RE m_N == 0?OP=(-f):TRMinus(f,0,f.PO<T>::m_SZ);}TE <TY T>TRPO<T>& TRPO<T>::TRMinus(CO PO<T>& f,CRUI N_input_start,CRUI N_input_lim){CRUI SZ = N_input_lim < m_N?N_input_lim < f.PO<T>::m_SZ?N_input_lim:f.PO<T>::m_SZ:m_N < f.PO<T>::m_SZ?m_N:f.PO<T>::m_SZ;if(PO<T>::m_SZ < SZ){PO<T>::m_f.reserve(SZ);for(uint i = N_input_start;i < PO<T>::m_SZ;i++){PO<T>::m_f[i]-= f.PO<T>::m_f[i];}for(uint i = PO<T>::m_SZ;i < SZ;i++){PO<T>::m_f.push_back(- f.PO<T>::m_f[i]);}PO<T>::m_SZ = SZ;}else{for(uint i = N_input_start;i < SZ;i++){PO<T>::m_f[i]-= f.PO<T>::m_f[i];}}RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP*=(CO T& t){PO<T>::OP*=(t);RE *TH;}TE <TY T>TRPO<T>& TRPO<T>::OP*=(CO PO<T>& f){DF_OF_MU_FOR_TR_PO(RE_ZERO_FOR_MU_FOR_TR_PO_IF(f.PO<T>::m_SZ == 0),RE *TH,RE_ZERO_FOR_MU_FOR_TR_PO_IF(searching),RE_ZERO_FOR_MU_FOR_TR_PO_IF(N_input_start_0_start_1 >= m_N),RE *TH,MU,PO<T>::m_f[j],0,);}TE <TY T> IN TRPO<T>& TRPO<T>::OP*=(PO<T>&& f){RE OP*=(f);}TE <TY T>TRPO<T>& TRPO<T>::FFT_MU(CO PO<T>& f){DF_OF_FFT_MU_FOR_TR_PO(RE_ZERO_FOR_MU_FOR_TR_PO_IF(f.PO<T>::m_SZ == 0),RE *TH,RE_ZERO_FOR_MU_FOR_TR_PO_IF(searching),RE_ZERO_FOR_MU_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE *TH,RE *TH,MU,PO<T>::m_f[j],0,0,,VE<T>& f0 = PO<T>::m_f,N_input_start_0,N_input_max_0 + 1,SET_SHIFTED_VE_FOR_MU(f1,f.PO<T>::m_f,N_input_start_1,N_input_max_1,N_input_start_0),N_input_start_0_start_1,N_input_start_0 + N_input_max_1 + 1,f1,,N_input_start_0,f1[N_input_start_1 + i]*= f0[i],OP=(TRPO<T>(m_N,MO(f1))));}TE <TY T>TRPO<T>& TRPO<T>::TRMU(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim){DF_OF_MU_FOR_TR_PO(,RE *TH,,RE_ZERO_FOR_MU_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE *TH,MU,PO<T>::m_f[j],N_output_start,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;});}TE <TY T>TRPO<T>& TRPO<T>::FFT_TRMU(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim){DF_OF_FFT_MU_FOR_TR_PO(,RE *TH,,RE_ZERO_FOR_MU_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE *TH,RE *TH,MU,PO<T>::m_f[j],N_output_start,N_output_start < N_input_start_0_start_1?0:N_output_start - N_input_start_0_start_1,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;},VE<T>& f0 = PO<T>::m_f,N_input_start_0,N_input_max_0 + 1,SET_SHIFTED_VE_FOR_MU(f1,f.PO<T>::m_f,N_input_start_1,N_input_max_1,N_input_start_0),N_input_start_0_start_1,N_input_start_0 + N_input_max_1 + 1,f1,,N_input_start_0,f1[N_input_start_1 + i]*= f0[i],OP=(TRPO<T>(m_N,MO(f1))));}TE <TY T>TRPO<T>& TRPO<T>::FFT_TRMU(PO<T>&& f,CRUI N_output_start,CRUI N_output_lim){DF_OF_FFT_MU_FOR_TR_PO(,RE *TH,,RE_ZERO_FOR_MU_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE *TH,RE *TH,MU,PO<T>::m_f[j],N_output_start,N_output_start < N_input_start_0_start_1?0:N_output_start - N_input_start_0_start_1,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;},VE<T>& f0 = PO<T>::m_f,N_input_start_0,N_input_max_0 + 1,VE<T>&& f1 = MO(f.PO<T>::m_f),N_input_start_1,N_input_max_1 + 1,f0,f0.reserve(product_LE),0,f1[N_input_start_0_start_1 + i]= f0[N_input_start_0 + i]* f1[N_input_start_1 + i],for(uint i = N_input_start_0;i < N_input_start_0_start_1;i++){f0[i]= 0;}PO<T>::m_SZ = f0.SZ();SetTruncation(m_N););}TE <TY T>TRPO<T> TRPO<T>::TRMU_CO(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim)CO{DF_OF_MU_FOR_TR_PO(,RE TRPO<T>(m_N,MO(AN)),,RE_ZERO_FOR_TR_MU_CO_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE TRPO<T>(m_N,MO(AN)),TR_MU_CO,PO<T>::OP[](j),N_output_start,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;});}TE <TY T>TRPO<T> TRPO<T>::FFT_TRMU_CO(CO PO<T>& f,CRUI N_output_start,CRUI N_output_lim)CO{DF_OF_FFT_MU_FOR_TR_PO(,RE TRPO<T>(m_N,MO(AN)),,RE_ZERO_FOR_TR_MU_CO_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE TRPO<T>(m_N,MO(AN)),RE TRPO<T>(m_N,MO(f0)),TR_MU_CO,PO<T>::OP[](j),N_output_start,N_output_start < N_input_start_0_start_1?0:N_output_start - N_input_start_0_start_1,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;},SET_SHIFTED_VE_FOR_MU(f0,PO<T>::m_f,N_input_start_0,N_input_max_0,N_input_start_1),N_input_start_0_start_1,N_input_start_1 + N_input_max_0 + 1,VE<T> f1 = f.PO<T>::m_f,N_input_start_1,N_input_max_1 + 1,f0,,N_input_start_1,f0[N_input_start_0 + i]*= f1[i],);}TE <TY T>TRPO<T> TRPO<T>::FFT_TRMU_CO(PO<T>&& f,CRUI N_output_start,CRUI N_output_lim)CO{DF_OF_FFT_MU_FOR_TR_PO(,RE TRPO<T>(m_N,MO(AN)),,RE_ZERO_FOR_TR_MU_CO_FOR_TR_PO_IF(N_input_start_0_start_1 >= N_output_lim_fixed),RE TRPO<T>(m_N,MO(AN)),RE TRPO<T>(m_N,MO(f0)),TR_MU_CO,PO<T>::OP[](j),N_output_start,N_output_start < N_input_start_0_start_1?0:N_output_start - N_input_start_0_start_1,if(N_output_lim_fixed > N_output_lim){N_output_lim_fixed = N_output_lim;},SET_SHIFTED_VE_FOR_MU(f0,PO<T>::m_f,N_input_start_0,N_input_max_0,N_input_start_1),N_input_start_0_start_1,N_input_start_1 + N_input_max_0 + 1,VE<T>&& f1 = MO(f.PO<T>::m_f),N_input_start_1,N_input_max_1 + 1,f0,,N_input_start_1,f0[N_input_start_0 + i]*= f1[i],);}TE <TY T> IN TRPO<T>& TRPO<T>::OP/=(CO T& t){PO<T>::OP/=(t);RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::OP/=(CO TRPO<T>& f){RE OP*=(Inverse(m_N <= f.m_N?f:TRPO<T>(m_N,f)));}TE <TY T> IN TRPO<T>& TRPO<T>::OP%=(CO T& t){PO<T>::OP%=(t);RE *TH;}TE <TY T> IN TRPO<T> TRPO<T>::OP-()CO{RE MO(TRPO<T>(m_N)-= *TH);}TE <TY T> IN VO TRPO<T>::SetTruncation(CRUI N)NE{if(N < m_N){TruncateFinal(N);}m_N = N;}TE <TY T> IN CRUI TRPO<T>::GetTruncation()CO NE{RE m_N;}TE <TY T> IN TRPO<T>& TRPO<T>::TruncateInitial(CRUI N)NE{CRUI SZ = N < PO<T>::m_SZ?N:PO<T>::m_SZ;for(uint i = 0;i < SZ;i++){PO<T>::m_f[i]= 0;}RE *TH;}TE <TY T> IN TRPO<T>& TRPO<T>::TruncateFinal(CRUI N)NE{WH(PO<T>::m_SZ > N){PO<T>::m_f.pop_back();PO<T>::m_SZ--;}RE *TH;}TE <TY T,TY P> IN TRPO<T> OP+(CO TRPO<T>& f0,CO P& f1){RE MO(TRPO<T>(f0)+= f1);}TE <TY T,TY P> IN TRPO<T> OP-(CO TRPO<T>& f){RE MO(TRPO<T>(f.GetTurncation())-= f);}TE <TY T,TY P> IN TRPO<T> OP-(CO TRPO<T>& f0,CO P& f1){RE MO(TRPO<T>(f0)-= f1);}TE <TY T,TY P> IN TRPO<T> OP*(CO TRPO<T>& f0,CO P& f1){RE MO(TRPO<T>(f0)*= f1);}TE <TY T,TY P> IN TRPO<T> OP/(CO TRPO<T>& f0,CO P& f1){RE MO(TRPO<T>(f0)/= f1);}TE <TY T> IN TRPO<T> OP%(CO TRPO<T>& f0,CO T& t1){RE MO(TRPO<T>(f0)%= t1);}TE <TY T>TRPO<T> Differential(CRUI n,CO TRPO<T>& f){if(f.PO<T>::m_SZ < n){RE TRPO<T>(f.m_N - n,PO<T>::zero());}VE<T> df(f.PO<T>::m_SZ - n);T coef = T::Factorial(n);uint i = n;WH(i < f.PO<T>::m_SZ){df[i - n]= f[i]* coef;i++;(coef *= i)/=(i - n);}RE TRPO<T>(f.m_N - n,MO(df));}TE <TY T>TRPO<T> TRDifferential(CO TRPO<T>& f,CRUI N_output_start_plus_one){assert(f.m_N > 0);TRPO<T> f_dif{f.m_N - 1};if(N_output_start_plus_one < f.PO<T>::m_SZ){CO uint SZ = f.PO<T>::m_SZ - 1;f_dif.PO<T>::m_f = VE<T>(SZ);for(uint i = N_output_start_plus_one;i < f.PO<T>::m_SZ;i++){f_dif.PO<T>::m_f[i-1]= f.PO<T>::m_f[i]* i;}f_dif.PO<T>::m_SZ = SZ;}RE f_dif;}TE <TY T> IN TRPO<T> Differential(CO TRPO<T>& f){RE TRDifferential<T>(f,1);}TE <TY T>TRPO<T> TRIntegral(CO TRPO<T>& f,CRUI N_output_start){TRPO<T> f_int{f.m_N + 1};if(N_output_start <= f.PO<T>::m_SZ){CO uint SZ = f.PO<T>::m_SZ + 1;f_int.PO<T>::m_f = VE<T>(SZ);for(uint i = N_output_start;i <= f.PO<T>::m_SZ;i++){f_int.PO<T>::m_f[i]= f.PO<T>::m_f[i - 1]/ T(i);}f_int.PO<T>::m_SZ = SZ;}RE f_int;}TE <TY T> IN TRPO<T> Integral(CO TRPO<T>& f){RE TRIntegral<T>(f,1);}TE <TY T>TRPO<T> Inverse(CO TRPO<T>& f){DF_OF_INVERSE_FOR_TR_PO(T,f_inv.TRMinus(f_inv.TRMU_CO(f,PW,PW_2).TRMU(f_inv,PW,PW_2),PW,PW_2));}TE <TY T>TRPO<T> Exp(CO TRPO<T>& f){DF_OF_EXP_FOR_TR_PO(T,f_exp.TRMinus((TRIntegral(Differential(f_exp).TRMU_CO(Inverse(f_exp),PW - 1,PW_2),PW).TRMinus(f,PW,PW_2)).TRMU(f_exp,PW),PW,PW_2));}TE <TY T> IN TRPO<T> Log(CO TRPO<T>& f){assert(f[0]== PO<T>::c_one());RE Integral<T>(Differential<T>(f)/= f);} DF_OF_PS_OF_MU_OF_PO_PROTH_MOD(P,997269505,Mod); // ファイル容量が厳しい場合は以下を削除する。(プロス素数以外を法とする畳み込み) DF_OF_PS_OF_MU_OF_PO_PROTH_MOD(167772161,167608321,Mod);DF_OF_PS_OF_MU_OF_PO_PROTH_MOD(469762049,469303297,Mod);DF_OF_PS_OF_MU_OF_PO_PROTH_MOD(754974721,754237441,Mod); #define DF_BODY_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(TYPE,ARG,MINT,COEF)TE <> PO<TYPE>& PO<TYPE>::OP*=(ARG f){if(m_SZ != 0){if(f.m_SZ == 0){m_f.clear();m_SZ = 0;}else{CE uint P0 = 167772161;CE uint P1 = 469762049;CE uint P2 = P;US M0 = MINT<P0>;US M1 = MINT<P1>;US M2 = MINT<P2>;VE<M0> v0{};VE<M1> v1{};VE<M2> v2{};v0.reserve(m_SZ);v1.reserve(m_SZ);v2.reserve(m_SZ);for(uint d = 0;d < m_SZ;d++){auto&& f_d = COEF;v0.push_back(f_d);v1.push_back(f_d);v2.push_back(f_d);}VE<M0> w0{};VE<M1> w1{};VE<M2> w2{};w0.reserve(f.m_SZ);w1.reserve(f.m_SZ);w2.reserve(f.m_SZ);for(uint d = 0;d < f.m_SZ;d++){auto&& f_d = f.COEF;w0.push_back(f_d);w1.push_back(f_d);w2.push_back(f_d);}m_SZ += f.m_SZ - 1;TRPO<M0> TH_copy0{m_SZ,MO(v0)};TRPO<M1> TH_copy1{m_SZ,MO(v1)};TRPO<M2> TH_copy2{m_SZ,MO(v2)};TRPO<M0> f_copy0{f.m_SZ,MO(w0)};TRPO<M1> f_copy1{f.m_SZ,MO(w1)};TRPO<M2> f_copy2{f.m_SZ,MO(w2)};TH_copy0 *= f_copy0;TH_copy1 *= f_copy1;TH_copy2 *= f_copy2;m_f.clear();m_f.reserve(m_SZ);CE TYPE P0_mod_M = TYPE(P0);CE TYPE P01_mod_M = TYPE(P1)* P0_mod_M;CE M1 P0_mod_P1_inv = M1::DeRP(104391568);CE M2 P0_mod_P2 = M2::DeRP(P0);CE M2 P01_mod_P2_inv = M2::DeRP(575867115);ST_AS((M1::DeRP(P0)*= P0_mod_P1_inv)== M1::DeRP(1));ST_AS((M2::DeRP(P0)*= M2::DeRP(P1)*= P01_mod_P2_inv)== M2::DeRP(1));for(uint d = 0;d < m_SZ;d++){auto&& c0 = TH_copy0[d].RP();auto&& c1 =((TH_copy1[d]-= c0)*= P0_mod_P1_inv).RP();auto&& c2 =((TH_copy2[d]-= P0_mod_P2 * c1 + c0)*= P01_mod_P2_inv).RP();m_f.push_back(P01_mod_M * c2 + P0_mod_M * c1 + c0);}ReMORedundantZero();}}RE *TH;} #define DF_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(MOD,MINT)DF_BODY_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(MINT<MOD>,CO PO<MINT<MOD> >&,MINT,m_f[d].RP());DF_BODY_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(MINT<MOD>,PO<MINT<MOD> >&&,MINT,m_f[d].RP()); #define DF_OF_PS_OF_MU_OF_PO_NON_MOD(INT)DF_BODY_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(INT,CO PO<INT>&,Mod,m_f[d]);DF_BODY_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(INT,PO<INT>&&,Mod,m_f[d]); DF_OF_PS_OF_MU_OF_PO_ARBITRARY_MOD(1000000007,Mod);DF_OF_PS_OF_MU_OF_PO_NON_MOD(ll);DF_OF_PS_OF_MU_OF_PO_NON_MOD(ull); // ここまで。 TE <TY T> IN PO<T>::PO():m_f(),m_SZ(0){}TE <TY T> IN PO<T>::PO(CO PO<T>& f):m_f(f.m_f),m_SZ(f.m_SZ){}TE <TY T> IN PO<T>::PO(PO<T>&& f):m_f(MO(f.m_f)),m_SZ(f.m_SZ){}TE <TY T> IN PO<T>::PO(TRPO<T> f):m_f(MO(f.m_f)),m_SZ(f.m_SZ){}TE <TY T> IN PO<T>::PO(VE<T> f):m_f(MO(f)),m_SZ(m_f.SZ()){}TE <TY T> IN PO<T>::PO(T t):PO(){if(t != c_zero()){OP[](0)= MO(t);}}TE <TY T> TE <SFINAE_FOR_PO()> IN PO<T>::PO(Arg n):PO(T(MO(n))){}TE <TY T> IN PO<T>::PO(CRUI i,T t):PO(){if(t != c_zero()){OP[](i)= MO(t);}}TE <TY T> TE <SFINAE_FOR_PO()> IN PO<T>::PO(CRUI i,Arg n):PO(i,T(MO(n))){}TE <TY T> TE <SFINAE_FOR_PO()> IN PO<T>& PO<T>::OP=(Arg n){m_f.clear();m_SZ = 0;OP[](0)= MO(n);RE *TH;}TE <TY T> IN PO<T>& PO<T>::OP=(PO<T> f){m_f = MO(f.m_f);m_SZ = f.m_SZ;RE *TH;}TE <TY T> IN PO<T>& PO<T>::OP=(VE<T> f){m_f = MO(f);m_SZ = m_f.SZ();RE *TH;}TE <TY T> IN CO T& PO<T>::OP[](CRUI i)CO{RE m_SZ <= i?c_zero():m_f[i];}TE <TY T> IN T& PO<T>::OP[](CRUI i){if(m_SZ <= i){CO T& z = c_zero();WH(m_SZ <= i){m_f.push_back(z);m_SZ++;}}RE m_f[i];}TE <TY T> IN T PO<T>::OP()(CO T& t)CO{RE MO((*TH %(PO<T>(1,c_one())- t))[0]);}TE <TY T>PO<T>& PO<T>::OP+=(CO PO<T>& f){if(m_SZ < f.m_SZ){m_f.reserve(f.m_SZ);for(uint i = 0;i < m_SZ;i++){m_f[i]+= f.m_f[i];}for(uint i = m_SZ;i < f.m_SZ;i++){m_f.push_back(f.m_f[i]);}m_SZ = f.m_SZ;}else{for(uint i = 0;i < f.m_SZ;i++){m_f[i]+= f.m_f[i];}}RE *TH;}TE <TY T>PO<T>& PO<T>::OP-=(CO PO<T>& f){if(m_SZ < f.m_SZ){m_f.reserve(f.m_SZ);for(uint i = 0;i < m_SZ;i++){m_f[i]-= f.m_f[i];}for(uint i = m_SZ;i < f.m_SZ;i++){m_f.push_back(- f.m_f[i]);}m_SZ = f.m_SZ;}else{for(uint i = 0;i < f.m_SZ;i++){m_f[i]-= f.m_f[i];}}RE *TH;}TE <TY T>PO<T>& PO<T>::OP*=(CO PO<T>& f){if(m_SZ == 0){RE *TH;}if(f.m_SZ == 0){m_f.clear();m_SZ = 0;RE *TH;}CO uint SZ = m_SZ + f.m_SZ - 1;PO<T> product{};for(uint i = 0;i < SZ;i++){T& product_i = product[i];CO uint j_min = m_SZ > i?0:i - m_SZ + 1;CO uint j_lim = i < f.m_SZ?i + 1:f.m_SZ;for(uint j = j_min;j < j_lim;j++){product_i += m_f[i - j]* f.m_f[j];}}RE *TH = MO(product);}TE <TY T> IN PO<T>& PO<T>::OP*=(PO<T>&& f){RE *TH *= f;};TE <TY T>PO<T>& PO<T>::OP/=(CO T& t){if(t == c_one()){RE *TH;}CO T t_inv{c_one()/ t};for(uint i = 0;i < m_SZ;i++){OP[](i)*= t_inv;}RE *TH;}TE <TY T>PO<T> PO<T>::TP(CO PO<T>& f,CRUI f_TP_SZ){VE<T> f_TP(f_TP_SZ);for(uint d = 0;d < f_TP_SZ;d++){f_TP[d]= f.m_f[f.m_SZ - 1 - d];}RE PO<T>(MO(f_TP));}TE <TY T>PO<T>& PO<T>::OP%=(CO T& t){if(t == c_one()){RE *TH = zero();}for(uint i = 0;i < m_SZ;i++){m_f[i]%= t;}RE *TH;}TE <TY T>bool PO<T>::OP==(CO PO<T>& f)CO{CRUI SZ0 = SZ();CRUI SZ1 = f.SZ();CRUI SZ_max = SZ0 < SZ1?SZ1:SZ0;for(uint i = 0;i < SZ_max;i++){if(OP[](i)!= f[i]){RE false;}}RE true;}TE <TY T>bool PO<T>::OP==(CO T& t)CO{CRUI SZ_max = SZ();CO T& zero = PO<T>::c_zero();for(uint i = 1;i < SZ_max;i++){if(m_f[i]!= zero){RE false;}}RE OP[](0)== t;}TE <TY T> TE<TY P> IN bool PO<T>::OP!=(CO P& f)CO{RE !(*TH == f);}DF_OF_AR_FOR_PO(+,f += *TH);TE <TY T> IN PO<T>& PO<T>::SignInvert(){ReMORedundantZero();for(auto& fi:m_f){fi = -fi;}RE *TH;}TE <TY T> IN PO<T> PO<T>::OP-()CO{RE MO(PO<T>(*TH).SignInvert());}DF_OF_AR_FOR_PO(-,f.SignInvert()+= *TH);DF_OF_AR_FOR_PO(*,f *= *TH);TE <TY T> IN PO<T> PO<T>::OP/(CO T& t)CO{RE MO(PO<T>(*TH)/= t);}TE <TY T> IN PO<T> PO<T>::OP%(CO T& t)CO{RE MO(PO<T>(*TH)%= t);}TE <TY T> IN CO VE<T>& PO<T>::GetCoefficient()CO NE{RE m_f;}TE <TY T> IN CRUI PO<T>::SZ()CO NE{RE m_SZ;}TE <TY T> IN VO PO<T>::reSZ(CRUI deg_plus)NE{m_f.reSZ(m_SZ = deg_plus);}TE <TY T> IN VO PO<T>::swap(PO<T>& f){m_f.swap(f.m_f);swap(m_SZ,f.m_SZ);}TE <TY T> IN VO PO<T>::swap(VE<T>& f){m_f.swap(f);m_SZ = m_f.SZ();}TE <TY T>VO PO<T>::ReMORedundantZero(){CO T& z = c_zero();WH(m_SZ > 0?m_f[m_SZ - 1]== z:false){m_f.pop_back();m_SZ--;}RE;}TE <TY T>string PO<T>::Display()CO NE{string s = "(";if(m_SZ > 0){s += to_string(m_f[0]);for(uint i = 1;i < m_SZ;i++){s += "," + to_string(m_f[i]);}}s += ")";RE s;}TE <TY T> IN CO PO<T>& PO<T>::zero(){ST CO PO<T> z{};RE z;}TE <TY T> IN CO PO<T>& PO<T>::one(){ST CO PO<T> o{c_one()};RE o;}TE <TY T> IN CO T& PO<T>::c_zero(){ST CO T z{0};RE z;}TE <TY T> IN CO T& PO<T>::c_one(){ST CO T o{1};RE o;}TE <TY T> IN CO T& PO<T>::c_minus_one(){ST CO T m{-1};RE m;}TE <TY T>PO<T> Differential(CRUI n,CO PO<T>& f){CRUI SZ = f.SZ();if(SZ < n){RE PO<T>::zero();}VE<T> df(SZ - n);T coef = T::Factorial(n);uint i = n;WH(i < SZ){df[i - n]= f[i]* coef;i++;(coef *= i)/=(i - n);}RE PO<T>(MO(df));} TE <TY T> IN PO<T>& PO<T>::OP/=(CO PO<T>& f){RE m_SZ < f.m_SZ?*TH:*TH = Quotient(*TH,f);}TE <TY T>PO<T> PO<T>::Quotient(CO PO<T>& f0,CO PO<T>& f1){if(f0.m_SZ < f1.m_SZ){RE PO<T>::zero();}AS(f1.m_SZ != 0);CO uint f0_TP_SZ = f0.m_SZ - f1.m_SZ + 1;CO uint f1_TP_SZ = f0_TP_SZ < f1.m_SZ?f0_TP_SZ:f1.m_SZ;CO TRPO<T> f1_TP_inverse = Inverse(TRPO<T>(f0_TP_SZ,TP(f1,f1_TP_SZ)));TRPO<T> f0_TP{f0_TP_SZ,TP(f0,f0_TP_SZ)};f0_TP *= f1_TP_inverse;for(uint d0 =(f0_TP_SZ + 1)/ 2;d0 < f0_TP_SZ;d0++){::swap(f0_TP[d0],f0_TP[f0_TP_SZ - 1 - d0]);}RE f0_TP;}TE <TY T>PO<T>& PO<T>::OP%=(CO PO<T>& f){if(m_SZ >= f.m_SZ){*TH -=(*TH / f)* f;ReMORedundantZero();}RE *TH;}TE <TY T> IN PO<T> PO<T>::OP/(CO PO<T>& f)CO{RE PO<T>::Quotient(*TH,f);}TE <TY T> IN PO<T> PO<T>::OP%(CO PO<T>& f)CO{RE MO(PO<T>(*TH)%= f);} #endif // AAA 常設でないライブラリは以上に挿入する。 #define INCLUDE_SUB #include __FILE__ #else // INCLUDE_LIBRARY #ifndef DEBUG #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 , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #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( I , N , ... ) SOLVE_ONLY; VariadicResize( N + I , __VA_ARGS__ ); FOR( VARIABLE_FOR_SET_A , 0 , N ){ VariadicSet( cin , VARIABLE_FOR_SET_A + I , __VA_ARGS__ ); } #define CIN_A( LL , I , N , ... ) VE<LL> __VA_ARGS__; SET_A( I , N , __VA_ARGS__ ) #endif #include <bits/stdc++.h> 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<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 ) CE LL BOUND = VALUE #define SET_A_ASSERT( I , N , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A + I] , MIN , MAX ); } #define CIN_ASSERT( A , MIN , MAX ) decldecay_t( MAX ) A; SET_ASSERT( A , MIN , MAX ) #define CIN_A_ASSERT( I , N , A , MIN , MAX ) vector<decldecay_t( MAX )> A( N + I ); SET_A_ASSERT( I , N , 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 ITR( ARRAY ) auto begin_ ## ARRAY = ARRAY .BE() , itr_ ## ARRAY = begin_ ## ARRAY , end_ ## ARRAY = ARRAY .EN() #define FOR_ITR( ARRAY ) for( ITR( ARRAY ) , itr = itr_ ## ARRAY ; itr_ ## ARRAY != end_ ## ARRAY ; itr_ ## ARRAY ++ , itr++ ) #define RUN( VAR , ... ) for( auto&& VAR : __VA_ARGS__ ) #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 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 VI virtual #define IS basic_istream<char,Traits> #define OS basic_ostream<char,Traits> #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<decltype(VAR)> TE <TY F,TY...Args> US ret_t = decltype(declval<F>()(declval<Args>()...)); TE <TY T> 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 <TY INT> US T2 = pair<INT,INT>; TE <TY INT> US T3 = tuple<INT,INT,INT>; TE <TY INT> US T4 = tuple<INT,INT,INT,INT>; US path = pair<int,ll>; // 二分探索用 // EXPRESSIONがANSWERの広義単調関数の時、EXPRESSION >= CO_TARGETの整数解を格納。 #define BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , DESIRED_INEQUALITY , CO_TARGET , INEQUALITY_FOR_CHECK , UPDATE_U , UPDATE_L , UPDATE_ANSWER ) \ ST_AS( ! is_same<decldecay_t( CO_TARGET ),uint>::value && ! is_same<decldecay_t( CO_TARGET ),ull>::value ); \ ll ANSWER = MINIMUM; \ { \ ll L_BS = MINIMUM; \ ll U_BS = MAXIMUM; \ ANSWER = UPDATE_ANSWER; \ ll EXPRESSION_BS; \ CO ll CO_TARGET_BS = ( CO_TARGET ); \ ll DIFFERENCE_BS; \ WH( L_BS < U_BS ){ \ DIFFERENCE_BS = ( EXPRESSION_BS = ( EXPRESSION ) ) - CO_TARGET_BS; \ CERR( "二分探索中:" , "L_BS =" , L_BS , "<=" , #ANSWER , "=" , ANSWER , "<=" , U_BS , "= U_BS : (" , #EXPRESSION , ") =" , EXPRESSION_BS , DIFFERENCE_BS > 0 ? ">" : DIFFERENCE_BS < 0 ? "<" : "=" , CO_TARGET_BS , "= (" , #CO_TARGET , ")" ); \ if( DIFFERENCE_BS INEQUALITY_FOR_CHECK 0 ){ \ U_BS = UPDATE_U; \ } else { \ L_BS = UPDATE_L; \ } \ ANSWER = UPDATE_ANSWER; \ } \ if( L_BS > U_BS ){ \ CERR( "二分探索失敗:" , "L_BS =" , L_BS , ">" , U_BS , "= U_BS :" , #ANSWER , ":= (" , #MAXIMUM , ") + 1 =" , MAXIMUM + 1 ); \ CERR( "二分探索マクロにミスがある可能性があります。変更前の版に戻してください。" ); \ ANSWER = MAXIMUM + 1; \ } else { \ CERR( "二分探索終了:" , "L_BS =" , L_BS , "<=" , #ANSWER , "=" , ANSWER , "<=" , U_BS , "= U_BS" ); \ CERR( "二分探索が成功したかを確認するために" , #EXPRESSION , "を計算します。" ); \ CERR( "成功判定が不要な場合はこの計算を削除しても構いません。" ); \ EXPRESSION_BS = ( EXPRESSION ); \ CERR( "二分探索結果: (" , #EXPRESSION , ") =" , EXPRESSION_BS , ( EXPRESSION_BS > CO_TARGET_BS ? ">" : EXPRESSION_BS < CO_TARGET_BS ? "<" : "=" ) , CO_TARGET_BS ); \ if( EXPRESSION_BS DESIRED_INEQUALITY CO_TARGET_BS ){ \ CERR( "二分探索成功:" , #ANSWER , ":=" , ANSWER ); \ } else { \ CERR( "二分探索失敗:" , #ANSWER , ":= (" , #MAXIMUM , ") + 1 =" , MAXIMUM + 1 ); \ CERR( "単調でないか、単調増加性と単調減少性を逆にしてしまったか、探索範囲内に解が存在しません。" ); \ ANSWER = MAXIMUM + 1; \ } \ } \ } \ // 単調増加の時にEXPRESSION >= CO_TARGETの最小解を格納。 #define BS1( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CO_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CO_TARGET , >= , ANSWER , ANSWER + 1 , ( L_BS + U_BS ) / 2 ) // 単調増加の時にEXPRESSION <= CO_TARGETの最大解を格納。 #define BS2( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CO_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CO_TARGET , > , ANSWER - 1 , ANSWER , ( L_BS + 1 + U_BS ) / 2 ) // 単調減少の時にEXPRESSION >= CO_TARGETの最大解を格納。 #define BS3( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CO_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , >= , CO_TARGET , < , ANSWER - 1 , ANSWER , ( L_BS + 1 + U_BS ) / 2 ) // 単調減少の時にEXPRESSION <= CO_TARGETの最小解を格納。 #define BS4( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , CO_TARGET ) BS( ANSWER , MINIMUM , MAXIMUM , EXPRESSION , <= , CO_TARGET , <= , ANSWER , ANSWER + 1 , ( L_BS + U_BS ) / 2 ) // 尺取り法用 // VAR_TPA_LとVAR_TPA_RをINITで初期化し、VAR_TPA_RがCONTINUE_CONDITIONを満たす限り、 // 閉区間[VAR_TPA_L,VAR_TPA_R]が条件ON_CONDITIONを満たすか否かを判定し、 // trueになるかVAR_TAR_LがVAR_TAR_Rに追い付くまでVAR_TPA_Lの更新操作UPDATE_Lを繰り返し、 // その後VAR_TPA_Rの更新操作UPDATE_Rを行う。 // ON_CONDITIONがtrueとなる極大閉区間とその時点でのINFOをANSWERに格納する。 #define TPA( ANSWER , VAR_TPA , INIT , CONTINUE_CONDITION , UPDATE_L , UPDATE_R , ON_CONDITION , INFO ) \ VE<tuple<decldecay_t( INIT ),decldecay_t( INIT ),decldecay_t( INFO )>> ANSWER{}; \ { \ auto init_TPA = INIT; \ decldecay_t( ANSWER.front() ) ANSWER ## _temp = { init_TPA , init_TPA , INFO }; \ auto ANSWER ## _prev = ANSWER ## _temp; \ auto& VAR_TPA ## _L = get<0>( ANSWER ## _temp ); \ auto& VAR_TPA ## _R = get<1>( ANSWER ## _temp ); \ auto& VAR_TPA ## _info = get<2>( ANSWER ## _temp ); \ bool on_TPA_prev = false; \ WH( true ){ \ bool continuing = CONTINUE_CONDITION; \ bool on_TPA = continuing && ( ON_CONDITION ); \ CERR( continuing ? "尺取り中" : "尺取り終了" , ": [L,R] = [" , VAR_TPA ## _L , "," , VAR_TPA ## _R , "] ," , on_TPA_prev ? "on" : "off" , "->" , on_TPA ? "on" : "off" , ", info =" , VAR_TPA ## _info ); \ if( on_TPA_prev && ! on_TPA ){ \ ANSWER.push_back( ANSWER ## _prev ); \ } \ if( continuing ){ \ if( on_TPA || VAR_TPA ## _L == VAR_TPA ## _R ){ \ ANSWER ## _prev = ANSWER ## _temp; \ UPDATE_R; \ } else { \ UPDATE_L; \ } \ } else { \ break; \ } \ on_TPA_prev = on_TPA; \ } \ } \ // データ構造用 TE <TY T> IN T Addition(CO T& t0,CO T& t1){RE t0 + t1;} TE <TY T> IN T Xor(CO T& t0,CO T& t1){RE t0 ^ t1;} TE <TY T> IN T MU(CO T& t0,CO T& t1){RE t0 * t1;} TE <TY T> IN CO T& Zero(){ST CO T z{};RE z;} TE <TY T> IN CO T& One(){ST CO T o = 1;RE o;}TE <TY T> IN T AdditionInv(CO T& t){RE -t;} TE <TY T> IN T Id(CO T& v){RE v;} TE <TY T> IN T Min(CO T& a,CO T& b){RE a < b?a:b;} TE <TY T> IN T Max(CO T& a,CO T& b){RE a < b?b:a;} // VVV 常設ライブラリは以下に挿入する。 #ifdef DEBUG #include "C:/Users/user/Documents/Programming/Contest/Template/include/a_Body.hpp" #else // Random(1KB) ll GetRand(CRI Rand_min,CRI Rand_max){AS(Rand_min <= Rand_max);ll AN = time(NULL);RE AN * rand()%(Rand_max + 1 - Rand_min)+ Rand_min;} // Map (2KB) #define DC_OF_HASH(...)struct hash<__VA_ARGS__>{IN size_t OP()(CO __VA_ARGS__& n)CO;}; CL is_ordered{PU:is_ordered()= delete;TE <TY T> ST CE auto Check(CO T& t)-> decltype(t < t,true_type());ST CE false_type Check(...);TE <TY T> ST CE CO bool value = is_same_v< decltype(Check(declval<T>())),true_type >;}; TE <TY T>US Set = conditional_t<is_COructible_v<unordered_set<T>>,unordered_set<T>,conditional_t<is_ordered::value<T>,set<T>,VO>>; #define DF_OF_AR_FOR_MAP(MAP,OPR)TE <TY T,TY U> IN MAP<T,U>& OP OPR ## =(MAP<T,U>& a,CO pair<T,U>& v){a[v.first]OPR ## = v.second;RE a;}TE <TY T,TY U> IN MAP<T,U>& OP OPR ## =(MAP<T,U>& a0,CO MAP<T,U>& a1){for(auto&[t,u]:a1){a0[t]OPR ## = u;}RE a0;}TE <TY T,TY U,TY ARG> IN MAP<T,U> OP OPR(MAP<T,U> a,CO ARG& arg){RE MO(a OPR ## = arg);} #define DF_OF_ARS_FOR_MAP(MAP)DF_OF_AR_FOR_MAP(MAP,+);DF_OF_AR_FOR_MAP(MAP,-);DF_OF_AR_FOR_MAP(MAP,*);DF_OF_AR_FOR_MAP(MAP,/);DF_OF_AR_FOR_MAP(MAP,%); TE <TY T,TY U>US Map = conditional_t<is_COructible_v<unordered_map<T,int>>,unordered_map<T,U>,conditional_t<is_ordered::value<T>,map<T,U>,VO>>; DF_OF_ARS_FOR_MAP(map);DF_OF_ARS_FOR_MAP(unordered_map); // Tuple(3KB) #define DF_OF_AR_FOR_TUPLE(OPR)TE <TY T,TY U,TE <TY...> TY V> IN auto OP OPR ## =(V<T,U>& t0,CO V<T,U>& t1)-> decltype((get<0>(t0),t0))&{get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);RE t0;}TE <TY T,TY U,TY V> IN tuple<T,U,V>& OP OPR ## =(tuple<T,U,V>& t0,CO tuple<T,U,V>& t1){get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);get<2>(t0)OPR ## = get<2>(t1);RE t0;}TE <TY T,TY U,TY V,TY W> IN tuple<T,U,V,W>& OP OPR ## =(tuple<T,U,V,W>& t0,CO tuple<T,U,V,W>& t1){get<0>(t0)OPR ## = get<0>(t1);get<1>(t0)OPR ## = get<1>(t1);get<2>(t0)OPR ## = get<2>(t1);get<3>(t0)OPR ## = get<3>(t1);RE t0;}TE <TE <TY...> TY V,TY...ARGS> IN auto OP OPR(CO V<ARGS...>& t0,CO V<ARGS...>& t1)-> decldecay_t((get<0>(t0),t0)){auto t = t0;RE MO(t OPR ## = t1);} #define DF_OF_HASH_FOR_TUPLE(PAIR)TE <TY T,TY U> IN size_t hash<PAIR<T,U>>::OP()(CO PAIR<T,U>& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash<T> h0;ST CO hash<U> h1;RE(h0(get<0>(n))* seed)^ h1(get<1>(n));} #define DF_OF_INCREMENT_FOR_TUPLE(INCR)TE <TY T,TY U,TE <TY...> TY V> IN auto OP INCR(V<T,U>& t)-> decldecay_t((get<0>(t),t))&{INCR get<0>(t);INCR get<1>(t);RE t;}TE <TY T,TY U,TY V> IN tuple<T,U,V>& OP INCR(tuple<T,U,V>& t){INCR get<0>(t);INCR get<1>(t);INCR get<2>(t);RE t;}TE <TY T,TY U,TY V,TY W> IN tuple<T,U,V,W>& OP INCR(tuple<T,U,V,W>& t){INCR get<0>(t);INCR get<1>(t);INCR get<2>(t);INCR get<3>(t);RE t;} TE <CL Traits,TY T,TY U,TE <TY...> TY V> IN auto OP>>(IS& is,V<T,U>& arg)-> decltype((get<0>(arg),is))&{RE is >> get<0>(arg)>> get<1>(arg);}TE <CL Traits,TY T,TY U,TY V> IN IS& OP>>(IS& is,tuple<T,U,V>& arg){RE is >> get<0>(arg)>> get<1>(arg)>> get<2>(arg);}TE <CL Traits,TY T,TY U,TY V,TY W> IN IS& OP>>(IS& is,tuple<T,U,V,W>& arg){RE is >> get<0>(arg)>> get<1>(arg)>> get<2>(arg)>> get<3>(arg);}TE <CL Traits,TY T,TY U,TE <TY...> TY V> IN auto OP<<(OS& os,CO V<T,U>& arg)-> decltype((get<0>(arg),os))&{RE os << get<0>(arg)<< " " << get<1>(arg);}TE <CL Traits,TY T,TY U,TY V> IN OS& OP<<(OS& os,CO tuple<T,U,V>& arg){RE os << get<0>(arg)<< " " << get<1>(arg)<< " " << get<2>(arg);}TE <CL Traits,TY T,TY U,TY V,TY W> IN OS& OP<<(OS& os,CO tuple<T,U,V,W>& arg){RE os << get<0>(arg)<< " " << get<1>(arg)<< " " << get<2>(arg)<< " " << get<3>(arg);}DF_OF_AR_FOR_TUPLE(+);DF_OF_AR_FOR_TUPLE(-);DF_OF_AR_FOR_TUPLE(*);DF_OF_AR_FOR_TUPLE(/);DF_OF_AR_FOR_TUPLE(%);DF_OF_INCREMENT_FOR_TUPLE(++);DF_OF_INCREMENT_FOR_TUPLE(--); TE <TY T,TY U> DC_OF_HASH(pair<T,U>);TE <TY T,TY U> DC_OF_HASH(tuple<T,U>);TE <TY T,TY U,TY V> DC_OF_HASH(tuple<T,U,V>);TE <TY T,TY U,TY V,TY W> DC_OF_HASH(tuple<T,U,V,W>); DF_OF_HASH_FOR_TUPLE(pair);DF_OF_HASH_FOR_TUPLE(tuple);TE <TY T,TY U,TY V> IN size_t hash<tuple<T,U,V>>::OP()(CO tuple<T,U,V>& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash<pair<T,U>> h01;ST CO hash<V> h2;RE(h01({get<0>(n),get<1>(n)})* seed)^ h2(get<2>(n));}TE <TY T,TY U,TY V,TY W> IN size_t hash<tuple<T,U,V,W>>::OP()(CO tuple<T,U,V,W>& n)CO{ST CO size_t seed =(GetRand(1e3,1e8)<< 1)| 1;ST CO hash<pair<T,U>> h01;ST CO hash<pair<V,W>> h23;RE(h01({get<0>(n),get<1>(n)})* seed)^ h23({get<2>(n),get<3>(n)});} // Vector(2KB) #define DF_OF_COUT_FOR_VE(V)TE <CL Traits,TY Arg> IN OS& OP<<(OS& os,CO V<Arg>& arg){auto BE = arg.BE(),EN = arg.EN();auto IT = BE;WH(IT != EN){(IT == BE?os:os << " ")<< *IT;IT++;}RE os;} #define DF_OF_AR_FOR_VE(V,OPR)TE <TY T> IN V<T>& OP OPR ## =(V<T>& a,CO T& t){for(auto& s:a){s OPR ## = t;}RE a;}TE <TY T> IN V<T>& OP OPR ## =(V<T>& a0,CO V<T>& a1){AS(a0.SZ()<= a1.SZ());auto IT0 = a0.BE(),EN0 = a0.EN();auto IT1 = a1.BE();WH(IT0 != EN0){*(IT0++)OPR ## = *(IT1++);}RE a0;}TE <TY T,TY U> IN V<T> OP OPR(V<T> a,CO U& u){RE MO(a OPR ## = u);} #define DF_OF_INCREMENT_FOR_VE(V,INCR)TE <TY T> IN V<T>& OP INCR(V<T>& a){for(auto& i:a){INCR i;}RE a;} #define DF_OF_ARS_FOR_VE(V)DF_OF_AR_FOR_VE(V,+);DF_OF_AR_FOR_VE(V,-);DF_OF_AR_FOR_VE(V,*);DF_OF_AR_FOR_VE(V,/);DF_OF_AR_FOR_VE(V,%);DF_OF_INCREMENT_FOR_VE(V,++);DF_OF_INCREMENT_FOR_VE(V,--) DF_OF_COUT_FOR_VE(VE);DF_OF_COUT_FOR_VE(LI);DF_OF_COUT_FOR_VE(set);DF_OF_COUT_FOR_VE(unordered_set);DF_OF_ARS_FOR_VE(VE);DF_OF_ARS_FOR_VE(LI);IN VO VariadicResize(CRI SZ){}TE <TY Arg,TY... ARGS> IN VO VariadicResize(CRI SZ,Arg& arg,ARGS&... args){arg.resize(SZ);VariadicResize(SZ,args...);}TE <TY T> VO sort(VE<T>& a,CO bool& reversed = false){if(reversed){ST auto comp =[](CO T& t0,CO T& t1){RE t1 < t0;};sort(a.BE(),a.EN(),comp);}else{sort(a.BE(),a.EN());}}TE <TY V> IN auto Get(V& a){RE[&](CRI i = 0)-> CO decldecay_t(a[0])&{RE a[i];};}TE <TY T = int> IN VE<T> id(CRI SZ){VE<T> AN(SZ);FOR(i,0,SZ){AN[i]= i;}RE AN;} // StdStream(1KB) TE <CL Traits> IN IS& VariadicCin(IS& is){RE is;}TE <CL Traits,TY Arg,TY... ARGS> IN IS& VariadicCin(IS& is,Arg& arg,ARGS&... args){RE VariadicCin(is >> arg,args...);}TE <CL Traits> IN IS& VariadicSet(IS& is,CRI i){RE is;}TE <CL Traits,TY Arg,TY... ARGS> IN IS& VariadicSet(IS& is,CRI i,Arg& arg,ARGS&... args){RE VariadicSet(is >> arg[i],i,args...);}TE <CL Traits> IN IS& VariadicGetline(IS& is,CO char& separator){RE is;}TE <CL Traits,TY Arg,TY... ARGS> IN IS& VariadicGetline(IS& is,CO char& separator,Arg& arg,ARGS&... args){RE VariadicGetline(getline(is,arg,separator),separator,args...);}TE <CL Traits,TY Arg> IN OS& VariadicCout(OS& os,CO Arg& arg){RE os << arg;}TE <CL Traits,TY Arg1,TY Arg2,TY... ARGS> IN OS& VariadicCout(OS& os,CO Arg1& arg1,CO Arg2& arg2,CO ARGS&... args){RE VariadicCout(os << arg1 << " ",arg2,args...);} // ConstexprModulo (7KB) CEXPR(uint,P,998244353); #define RP Represent #define DeRP Derepresent TE <uint M,TY INT> CE INT RS(INT n)NE{RE MO(n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n < INT(M)?n:n %= M);}TE <TY INT> 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 <uint M> CL Mod;TE <uint M>CL COantsForMod{PU:COantsForMod()= delete;ST CE CO uint g_memory_bound = 1e6;ST CE CO uint g_memory_LE = M < g_memory_bound?M:g_memory_bound;ST CE uint g_M_minus = M - 1;ST CE int g_order_minus_1 = M - 2;ST CE int g_order_minus_1_neg = -g_order_minus_1;}; #define DC_OF_CM_FOR_MOD(OPR)CE bool OP OPR(CO Mod<M>& n)CO NE #define DC_OF_AR_FOR_MOD(OPR,EX)CE Mod<M> OP OPR(Mod<M> n)CO EX; #define DF_OF_CM_FOR_MOD(OPR)TE <uint M> CE bool Mod<M>::OP OPR(CO Mod<M>& n)CO NE{RE m_n OPR n.m_n;} #define DF_OF_AR_FOR_MOD(OPR,EX,LEFT,OPR2)TE <uint M> CE Mod<M> Mod<M>::OP OPR(Mod<M> n)CO EX{RE MO(LEFT OPR2 ## = *TH);}TE <uint M,TY T> CE Mod<M> OP OPR(T n0,CO Mod<M>& n1)EX{RE MO(Mod<M>(MO(n0))OPR ## = n1);} TE <uint M>CL Mod{PU:uint m_n;CE Mod()NE;CE Mod(CO Mod<M>& n)NE;CE Mod(Mod<M>&& n)NE;TE <TY T> CE Mod(T n)NE;CE Mod<M>& OP=(Mod<M> n)NE;CE Mod<M>& OP+=(CO Mod<M>& n)NE;CE Mod<M>& OP-=(CO Mod<M>& n)NE;CE Mod<M>& OP*=(CO Mod<M>& n)NE;IN Mod<M>& OP/=(Mod<M> n);TE <TY INT> CE Mod<M>& OP<<=(INT n);TE <TY INT> CE Mod<M>& OP>>=(INT n);CE Mod<M>& OP++()NE;CE Mod<M> OP++(int)NE;CE Mod<M>& OP--()NE;CE Mod<M> 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(+,NE);DC_OF_AR_FOR_MOD(-,NE);DC_OF_AR_FOR_MOD(*,NE);DC_OF_AR_FOR_MOD(/,);TE <TY INT> CE Mod<M> OP^(INT EX)CO;TE <TY INT> CE Mod<M> OP<<(INT n)CO;TE <TY INT> CE Mod<M> OP>>(INT n)CO;CE Mod<M> OP-()CO NE;CE Mod<M>& SignInvert()NE;IN Mod<M>& Invert();TE <TY INT> CE Mod<M>& PW(INT EX);CE VO swap(Mod<M>& n)NE;CE CRUI RP()CO NE;ST CE Mod<M> DeRP(uint n)NE;ST IN CO Mod<M>& Inverse(CRUI n);ST IN CO Mod<M>& Factorial(CRUI n);ST IN CO Mod<M>& FactorialInverse(CRUI n);ST IN Mod<M> Combination(CRUI n,CRUI i);ST IN CO Mod<M>& zero()NE;ST IN CO Mod<M>& one()NE;TE <TY INT> CE Mod<M>& PositivePW(INT EX)NE;TE <TY INT> CE Mod<M>& NonNegativePW(INT EX)NE;US COants = COantsForMod<M>;}; US MP = Mod<P>; TE <uint M> CE Mod<M>::Mod()NE:m_n(){}TE <uint M> CE Mod<M>::Mod(CO Mod<M>& n)NE:m_n(n.m_n){}TE <uint M> CE Mod<M>::Mod(Mod<M>&& n)NE:m_n(MO(n.m_n)){}TE <uint M> TE <TY T> CE Mod<M>::Mod(T n)NE:m_n(RS<M>(MO(n))){ST_AS(is_COructible_v<uint,decay_t<T> >);}TE <uint M> CE Mod<M>& Mod<M>::OP=(Mod<M> n)NE{m_n = MO(n.m_n);RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP+=(CO Mod<M>& n)NE{(m_n += n.m_n)< M?m_n:m_n -= M;RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP-=(CO Mod<M>& n)NE{m_n < n.m_n?(m_n += M)-= n.m_n:m_n -= n.m_n;RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP*=(CO Mod<M>& n)NE{m_n = MO(ull(m_n)* n.m_n)% M;RE *TH;}TE <> CE MP& MP::OP*=(CO MP& n)NE{ull m_n_copy = m_n;m_n = MO((m_n_copy *= n.m_n)< P?m_n_copy:RSP(m_n_copy));RE *TH;}TE <uint M> IN Mod<M>& Mod<M>::OP/=(Mod<M> n){RE OP*=(n.Invert());}TE <uint M> TE <TY INT> CE Mod<M>& Mod<M>::OP<<=(INT n){AS(n >= 0);RE *TH *= Mod<M>(2).NonNegativePW(MO(n));}TE <uint M> TE <TY INT> CE Mod<M>& Mod<M>::OP>>=(INT n){AS(n >=0);WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += M)>>= 1;}RE *TH;}TE <uint M> CE Mod<M>& Mod<M>::OP++()NE{m_n < COants::g_M_minus?++m_n:m_n = 0;RE *TH;}TE <uint M> CE Mod<M> Mod<M>::OP++(int)NE{Mod<M> n{*TH};OP++();RE n;}TE <uint M> CE Mod<M>& Mod<M>::OP--()NE{m_n == 0?m_n = COants::g_M_minus:--m_n;RE *TH;}TE <uint M> CE Mod<M> Mod<M>::OP--(int)NE{Mod<M> 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(+,NE,n,+);DF_OF_AR_FOR_MOD(-,NE,n.SignInvert(),+);DF_OF_AR_FOR_MOD(*,NE,n,*);DF_OF_AR_FOR_MOD(/,,n.Invert(),*);TE <uint M> TE <TY INT> CE Mod<M> Mod<M>::OP^(INT EX)CO{RE MO(Mod<M>(*TH).PW(MO(EX)));}TE <uint M> TE <TY INT> CE Mod<M> Mod<M>::OP<<(INT n)CO{RE MO(Mod<M>(*TH)<<= MO(n));}TE <uint M> TE <TY INT> CE Mod<M> Mod<M>::OP>>(INT n)CO{RE MO(Mod<M>(*TH)>>= MO(n));}TE <uint M> CE Mod<M> Mod<M>::OP-()CO NE{RE MO(Mod<M>(*TH).SignInvert());}TE <uint M> CE Mod<M>& Mod<M>::SignInvert()NE{m_n > 0?m_n = M - m_n:m_n;RE *TH;}TE <uint M> IN Mod<M>& Mod<M>::Invert(){AS(m_n != 0);uint m_n_neg;RE m_n < COants::g_memory_LE?(m_n = Inverse(m_n).m_n,*TH):((m_n_neg = M - m_n)< COants::g_memory_LE)?(m_n = M - Inverse(m_n_neg).m_n,*TH):NonNegativePW(COants::g_order_minus_1);}TE <uint M> TE <TY INT> CE Mod<M>& Mod<M>::PositivePW(INT EX)NE{Mod<M> PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?*TH *= PW:*TH;EX >>= 1;PW *= PW;}RE *TH;}TE <uint M> TE <TY INT> CE Mod<M>& Mod<M>::NonNegativePW(INT EX)NE{RE EX == 0?(m_n = 1,*TH):PositivePW(MO(EX));}TE <uint M> TE <TY INT> CE Mod<M>& Mod<M>::PW(INT EX){bool neg = EX < 0;AS(!(neg && m_n == 0));RE neg?PositivePW(MO(EX *= COants::g_order_minus_1_neg)):NonNegativePW(MO(EX));}TE <uint M> CE VO Mod<M>::swap(Mod<M>& n)NE{std::swap(m_n,n.m_n);}TE <uint M> IN CO Mod<M>& Mod<M>::Inverse(CRUI n){AS(n < COants::g_memory_LE);ST Mod<M> memory[COants::g_memory_LE]={zero(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory[LE_curr].m_n = M - memory[M % LE_curr].m_n * ull(M / LE_curr)% M;LE_curr++;}RE memory[n];}TE <uint M> IN CO Mod<M>& Mod<M>::Factorial(CRUI n){if(M <= n){RE zero();}AS(n < COants::g_memory_LE);ST Mod<M> memory[COants::g_memory_LE]={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){(memory[LE_curr]= memory[LE_curr - 1])*= LE_curr;LE_curr++;}RE memory[n];}TE <uint M> IN CO Mod<M>& Mod<M>::FactorialInverse(CRUI n){ST Mod<M> memory[COants::g_memory_LE]={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){(memory[LE_curr]= memory[LE_curr - 1])*= Inverse(LE_curr);LE_curr++;}RE memory[n];}TE <uint M> IN Mod<M> Mod<M>::Combination(CRUI n,CRUI i){RE i <= n?Factorial(n)* FactorialInverse(i)* FactorialInverse(n - i):zero();}TE <uint M> CE CRUI Mod<M>::RP()CO NE{RE m_n;}TE <uint M> CE Mod<M> Mod<M>::DeRP(uint n)NE{Mod<M> n_copy{};n_copy.m_n = MO(n);RE n_copy;}TE <uint M> IN CO Mod<M>& Mod<M>::zero()NE{ST CE CO Mod<M> z{};RE z;}TE <uint M> IN CO Mod<M>& Mod<M>::one()NE{ST CE CO Mod<M> o{1};RE o;}TE <uint M> IN Mod<M> Inverse(CO Mod<M>& n){RE MO(Mod<M>(n).Invert());}TE <uint M,TY INT> CE Mod<M> PW(Mod<M> n,INT EX){RE MO(n.PW(MO(EX)));}TE <uint M> CE VO swap(Mod<M>& n0,Mod<M>& n1)NE{n0.swap(n1);}TE <uint M> IN string to_string(CO Mod<M>& n)NE{RE to_string(n.RP())+ " + " + to_string(M)+ "Z";}TE <uint M,CL Traits> IN basic_istream<char,Traits>& OP>>(basic_istream<char,Traits>& is,Mod<M>& n){ll m;is >> m;n = m;RE is;}TE <uint M,CL Traits> IN basic_ostream<char,Traits>& OP<<(basic_ostream<char,Traits>& os,CO Mod<M>& n){RE os << n.RP();} #define DF_OF_HASH_FOR_MOD(MOD)IN size_t hash<MOD>::OP()(CO MOD& n)CO{ST CO hash<decldecay_t(n.RP())> h;RE h(n.RP());} TE <uint M> DC_OF_HASH(Mod<M>); TE <uint M> DF_OF_HASH_FOR_MOD(Mod<M>); #endif // AAA 常設ライブラリは以上に挿入する。 #define INCLUDE_LIBRARY #include __FILE__ #endif // INCLUDE_LIBRARY #endif // INCLUDE_SUB #endif // INCLUDE_MAIN