// 入力制約/フォーマットチェック #ifndef INCLUDE_MODE #define INCLUDE_MODE // #define REACTIVE #define USE_GETLINE #endif #ifdef INCLUDE_MAIN void Solve() { CEXPR( int , bound_HW , 25 ); CEXPR( ll , bound_K , 1e18 ); CEXPR( ll , bound_M , ll(1e9) + 7 ); GETLINE_COUNT( HWKM_str , 4 , ' ' ); STOI( HWKM_str , H , 1 , bound_HW ); STOI( HWKM_str , W , 1 , bound_HW ); STOI( HWKM_str , K , 0 , bound_K ); STOI( HWKM_str , M , ll(1e8) , bound_M ); int HW = H * W; assert( HW <= bound_HW ); FOREQ( d , 2 , 31623 ){ assert( M % d != 0 ); } vector count( 6 , vector( 26 , vector() ) ); count[ 1 ][ 1 ] = { 0, 1 }; count[ 1 ][ 2 ] = { 0, 2, 1 }; count[ 1 ][ 3 ] = { 0, 5, 2, 1 }; count[ 1 ][ 4 ] = { 0, 12, 5, 2, 1 }; count[ 1 ][ 5 ] = { 0, 28, 12, 5, 2, 1 }; count[ 1 ][ 6 ] = { 0, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 7 ] = { 0, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 8 ] = { 0, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 9 ] = { 0, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 10 ] = { 0, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 11 ] = { 0, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 12 ] = { 0, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 13 ] = { 0, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 14 ] = { 0, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 15 ] = { 0, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 16 ] = { 0, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 17 ] = { 0, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 18 ] = { 0, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 19 ] = { 0, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 20 ] = { 0, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 21 ] = { 0, 6029312, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 22 ] = { 0, 12582912, 6029312, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 23 ] = { 0, 26214400, 12582912, 6029312, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 24 ] = { 0, 54525952, 26214400, 12582912, 6029312, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 1 ][ 25 ] = { 0, 113246208, 54525952, 26214400, 12582912, 6029312, 2883584, 1376256, 655360, 311296, 147456, 69632, 32768, 15360, 7168, 3328, 1536, 704, 320, 144, 64, 28, 12, 5, 2, 1 }; count[ 2 ][ 2 ] = { 0, 8, 4, 4, 1 }; count[ 2 ][ 3 ] = { 0, 40, 17, 14, 10, 6, 1 }; count[ 2 ][ 4 ] = { 0, 192, 80, 64, 39, 28, 20, 8, 1 }; count[ 2 ][ 5 ] = { 0, 896, 368, 296, 184, 120, 81, 58, 34, 10, 1 }; count[ 2 ][ 6 ] = { 0, 4096, 1664, 1344, 856, 560, 368, 240, 167, 112, 52, 12, 1 }; count[ 2 ][ 7 ] = { 0, 18432, 7424, 6016, 3904, 2568, 1712, 1112, 728, 488, 337, 198, 74, 14, 1 }; count[ 2 ][ 8 ] = { 0, 81920, 32768, 26624, 17536, 11584, 7816, 5136, 3384, 2208, 1456, 992, 647, 324, 100, 16, 1 }; count[ 2 ][ 9 ] = { 0, 360448, 143360, 116736, 77824, 51584, 35136, 23304, 15520, 10232, 6704, 4392, 2936, 1976, 1169, 498, 130, 18, 1 }; count[ 2 ][ 10 ] = { 0, 1572864, 622592, 507904, 342016, 227328, 156032, 104256, 70024, 46640, 30888, 20320, 13304, 8784, 5904, 3792, 1991, 728, 164, 20, 1 }; count[ 2 ][ 11 ] = { 0, 6815744, 2686976, 2195456, 1490944, 993280, 686080, 461184, 311872, 209416, 139968, 93048, 61440, 40328, 26480, 17624, 11672, 6952, 3217, 1022, 202, 22, 1 }; count[ 2 ][ 12 ] = { 0, 29360128, 11534336, 9437184, 6455296, 4308992, 2992128, 2021376, 1374592, 929088, 625544, 419408, 279656, 185376, 122088, 80112, 52888, 35200, 22416, 12160, 4967, 1388, 244, 24, 1 }; count[ 3 ][ 3 ] = { 0, 400, 144, 106, 84, 69, 52, 32, 9, 1 }; count[ 3 ][ 4 ] = { 0, 3840, 1360, 952, 705, 550, 445, 360, 270, 164, 62, 12, 1 }; count[ 3 ][ 5 ] = { 0, 35840, 12544, 8720, 6408, 4897, 3772, 2972, 2371, 1873, 1385, 854, 376, 101, 15, 1 }; count[ 3 ][ 6 ] = { 0, 327680, 113664, 78592, 57936, 44456, 34161, 26334, 20329, 15846, 12510, 9758, 7131, 4478, 2164, 714, 149, 18, 1 }; count[ 3 ][ 7 ] = { 0, 2949120, 1015808, 699392, 516864, 398928, 308296, 238385, 183352, 140476, 108244, 84130, 65926, 50898, 36946, 23558, 12123, 4602, 1205, 206, 21, 1 }; count[ 3 ][ 8 ] = { 0, 26214400, 8978432, 6160384, 4561920, 3537664, 2748496, 2136040, 1652625, 1269794, 973997, 746362, 574842, 446028, 347449, 266040, 192322, 124150, 66909, 28072, 8654, 1876, 272, 24, 1 }; count[ 4 ][ 4 ] = { 0, 73728, 25600, 17152, 11728, 9040, 7504, 6288, 5340, 4436, 3432, 2360, 1296, 492, 116, 16, 1 }; count[ 4 ][ 5 ] = { 0, 1376256, 471040, 314368, 213248, 158976, 128272, 105936, 89480, 75998, 64363, 53486, 42473, 31078, 19942, 10358, 3972, 1054, 186, 20, 1 }; count[ 4 ][ 6 ] = { 0, 25165824, 8519680, 5668864, 3856384, 2863104, 2288896, 1868032, 1553240, 1299184, 1090650, 919360, 774295, 643000, 517972, 394544, 274893, 167464, 83894, 32492, 9316, 1920, 272, 24, 1 }; count[ 5 ][ 5 ] = { 0, 51380224, 17301504, 11501568, 7749632, 5599232, 4361216, 3564416, 3023760, 2603088, 2265120, 1971974, 1703398, 1448460, 1194986, 939036, 687581, 454452, 258314, 119182, 42437, 11295, 2192, 296, 25, 1 }; if( H > W ){ swap( H , W ); } DynamicMod::SetModulo( M ); vector power( HW + 1 ); FOREQ( c , 0 , HW ){ power[c] = Power( DynamicMod::Derepresent( c ) , K ); } DynamicMod power_sum{} , coef{}; FOREQ( c , 1 , HW ){ power_sum += power[c] * count[H][W][c]; } FOREQ( n , 1 , HW ){ coef += DynamicMod::one() / ( HW - n + 1 ); } RETURN( power_sum * coef * Power( DynamicMod::Derepresent( 2 ) , -HW ) ); } REPEAT_MAIN(1); #else // INCLUDE_MAIN #ifdef INCLUDE_LIBRARY // https://github.com/p-adic/cpp // VVV ライブラリは以下に挿入する。redefinitionを避けるため圧縮元はincludeしない。 /* 圧縮用 */ #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 #define OS basic_ostream #define ST_AS static_assert #define reMO_CO remove_const #define is_COructible_v is_constructible_v #define rBE rbegin #define RP Represent #define DeRP Derepresent TE CE INT1 Residue(INT1 n,CO INT2& M)NE{RE MO(n < 0?((((++n)*= -1)%= M)*= -1)+= M - 1:n < M?n:n %= M);} TE CL DynamicMods;TE CL COantsForDynamicMods{PU:COantsForDynamicMods()= delete;ST uint g_M;ST uint g_M_minus;ST int g_order;ST int g_order_minus_1;ST int g_order_minus_1_neg;ST bool g_M_is_prime;}; TE uint COantsForDynamicMods::g_M = 0;TE uint COantsForDynamicMods::g_M_minus = -1;TE int COantsForDynamicMods::g_order = 1;TE int COantsForDynamicMods::g_order_minus_1 = 0;TE int COantsForDynamicMods::g_order_minus_1_neg = 0;TE bool COantsForDynamicMods::g_M_is_prime = false; #define DC_OF_CM_FOR_DYNAMIC_MOD(OPR)IN bool OP OPR(CO DynamicMods& n)CO NE #define DC_OF_AR_FOR_DYNAMIC_MOD(OPR,EX)IN DynamicMods OP OPR(DynamicMods n)CO EX; #define DF_OF_CM_FOR_DYNAMIC_MOD(OPR)TE IN bool DynamicMods::OP OPR(CO DynamicMods& n)CO NE{RE m_n OPR n.m_n;} #define DF_OF_AR_FOR_DYNAMIC_MOD(OPR,EX,LEFT,OPR2)TE IN DynamicMods DynamicMods::OP OPR(DynamicMods n)CO EX{RE MO(LEFT OPR2 ## = *TH);}TE IN DynamicMods OP OPR(T n0,CO DynamicMods& n1)EX{RE MO(DynamicMods(MO(n0))OPR ## = n1);} TE CL DynamicMods{PU:uint m_n;IN DynamicMods()NE;IN DynamicMods(CO DynamicMods& n)NE;IN DynamicMods(DynamicMods&& n)NE;TE IN DynamicMods(T n)NE;IN DynamicMods& OP=(DynamicMods n)NE;IN DynamicMods& OP+=(CO DynamicMods& n)NE;IN DynamicMods& OP-=(CO DynamicMods& n)NE;IN DynamicMods& OP*=(CO DynamicMods& n)NE;IN DynamicMods& OP/=(DynamicMods n);TE IN DynamicMods& OP<<=(INT n);TE IN DynamicMods& OP>>=(INT n);IN DynamicMods& OP++()NE;IN DynamicMods OP++(int)NE;IN DynamicMods& OP--()NE;IN DynamicMods OP--(int)NE;DC_OF_CM_FOR_DYNAMIC_MOD(==);DC_OF_CM_FOR_DYNAMIC_MOD(!=);DC_OF_CM_FOR_DYNAMIC_MOD(<);DC_OF_CM_FOR_DYNAMIC_MOD(<=);DC_OF_CM_FOR_DYNAMIC_MOD(>);DC_OF_CM_FOR_DYNAMIC_MOD(>=);DC_OF_AR_FOR_DYNAMIC_MOD(+,NE);DC_OF_AR_FOR_DYNAMIC_MOD(-,NE);DC_OF_AR_FOR_DYNAMIC_MOD(*,NE);DC_OF_AR_FOR_DYNAMIC_MOD(/,);TE IN DynamicMods OP^(INT EX)CO;TE IN DynamicMods OP<<(INT n)CO;TE IN DynamicMods OP>>(INT n)CO;IN DynamicMods OP-()CO NE;IN DynamicMods& SignInvert()NE;IN DynamicMods& Invert();TE IN DynamicMods& PW(INT EX);IN VO swap(DynamicMods& n)NE;IN CRUI RP()CO NE;ST IN DynamicMods DeRP(uint n)NE;ST IN CO DynamicMods& Inverse(CRUI n);ST IN CO DynamicMods& Factorial(CRUI n);ST IN CO DynamicMods& FactorialInverse(CRUI n);ST IN DynamicMods Combination(CRUI n,CRUI i);ST IN CO DynamicMods& zero()NE;ST IN CO DynamicMods& one()NE;ST IN CRUI GetModulo()NE;ST IN VO SetModulo(CRUI M,CRI order_minus_1 = -1)NE;TE IN DynamicMods& PositivePW(INT EX)NE;TE IN DynamicMods& NonNegativePW(INT EX)NE;US COants = COantsForDynamicMods;}; US DynamicMod = DynamicMods<0>; TE IN DynamicMods::DynamicMods()NE:m_n(){}TE IN DynamicMods::DynamicMods(CO DynamicMods& n)NE:m_n(n.m_n){}TE IN DynamicMods::DynamicMods(DynamicMods&& n)NE:m_n(MO(n.m_n)){}TE TE IN DynamicMods::DynamicMods(T n)NE:m_n(Residue(uint(MO(n)),COants::g_M)){ST_AS(is_COructible_v >);}TE IN DynamicMods& DynamicMods::OP=(DynamicMods n)NE{m_n = MO(n.m_n);RE *TH;}TE IN DynamicMods& DynamicMods::OP+=(CO DynamicMods& n)NE{(m_n += n.m_n)< COants::g_M?m_n:m_n -= COants::g_M;RE *TH;}TE IN DynamicMods& DynamicMods::OP-=(CO DynamicMods& n)NE{m_n < n.m_n?(m_n += COants::g_M)-= n.m_n:m_n -= n.m_n;RE *TH;}TE IN DynamicMods& DynamicMods::OP*=(CO DynamicMods& n)NE{m_n = Residue(MO(ull(m_n)* n.m_n),COants::g_M);RE *TH;}TE IN DynamicMods& DynamicMods::OP/=(DynamicMods n){RE OP*=(n.Invert());}TE TE IN DynamicMods& DynamicMods::OP<<=(INT n){AS(n >= 0);RE *TH *= DynamicMods(2).NonNegativePW(MO(n));}TE TE IN DynamicMods& DynamicMods::OP>>=(INT n){AS(n >= 0);WH(n-- > 0){((m_n & 1)== 0?m_n:m_n += COants::g_M)>>= 1;}RE *TH;}TE IN DynamicMods& DynamicMods::OP++()NE{m_n < COants::g_M_minus?++m_n:m_n = 0;RE *TH;}TE IN DynamicMods DynamicMods::OP++(int)NE{DynamicMods n{*TH};OP++();RE n;}TE IN DynamicMods& DynamicMods::OP--()NE{m_n == 0?m_n = COants::g_M_minus:--m_n;RE *TH;}TE IN DynamicMods DynamicMods::OP--(int)NE{DynamicMods n{*TH};OP--();RE n;}DF_OF_CM_FOR_DYNAMIC_MOD(==);DF_OF_CM_FOR_DYNAMIC_MOD(!=);DF_OF_CM_FOR_DYNAMIC_MOD(>);DF_OF_CM_FOR_DYNAMIC_MOD(>=);DF_OF_CM_FOR_DYNAMIC_MOD(<);DF_OF_CM_FOR_DYNAMIC_MOD(<=);DF_OF_AR_FOR_DYNAMIC_MOD(+,NE,n,+);DF_OF_AR_FOR_DYNAMIC_MOD(-,NE,n.SignInvert(),+);DF_OF_AR_FOR_DYNAMIC_MOD(*,NE,n,*);DF_OF_AR_FOR_DYNAMIC_MOD(/,,n.Invert(),*);TE TE IN DynamicMods DynamicMods::OP^(INT EX)CO{RE MO(DynamicMods(*TH).PW(MO(EX)));}TE TE IN DynamicMods DynamicMods::OP<<(INT n)CO{RE MO(DynamicMods(*TH)<<= MO(n));}TE TE IN DynamicMods DynamicMods::OP>>(INT n)CO{RE MO(DynamicMods(*TH)>>= MO(n));}TE IN DynamicMods DynamicMods::OP-()CO NE{RE MO(DynamicMods(*TH).SignInvert());}TE IN DynamicMods& DynamicMods::SignInvert()NE{m_n > 0?m_n = COants::g_M - m_n:m_n;RE *TH;}TE IN DynamicMods& DynamicMods::Invert(){RE m_n <(COants::g_M_is_prime?1e6:3e4)?*TH = Inverse(m_n):NonNegativePW(COants::g_order_minus_1);}TE TE IN DynamicMods& DynamicMods::PositivePW(INT EX)NE{DynamicMods PW{*TH};EX--;WH(EX != 0){(EX & 1)== 1?*TH *= PW:*TH;EX >>= 1;PW *= PW;}RE *TH;}TE TE IN DynamicMods& DynamicMods::NonNegativePW(INT EX)NE{RE EX == 0?(m_n = 1,*TH):PositivePW(MO(EX));}TE TE IN DynamicMods& DynamicMods::PW(INT EX){bool neg = EX < 0;RE neg?PositivePW(ll(MO(EX %= COants::g_order))* COants::g_order_minus_1_neg % COants::g_order):NonNegativePW(MO(EX));}TE IN VO DynamicMods::swap(DynamicMods& n)NE{std::swap(m_n,n.m_n);}TE IN CO DynamicMods& DynamicMods::Inverse(CRUI n){ST VE> memory ={zero(),one()};ST uint LE_curr = 2;AS(COants::g_M == 1||(0 < n && n < COants::g_M));WH(LE_curr <= n){memory.push_back(COants::g_M_is_prime?DeRP(COants::g_M - memory[COants::g_M % LE_curr].m_n * ull(COants::g_M / LE_curr)% COants::g_M):DeRP(n).NonNegativePW(COants::g_order_minus_1));LE_curr++;}RE memory[n];}TE IN CO DynamicMods& DynamicMods::Factorial(CRUI n){ST VE> memory ={one(),one()};ST uint LE_curr = 2;if(COants::g_M <= n){RE zero();}WH(LE_curr <= n && memory.back().m_n != 0){memory.push_back(memory.back()* DeRP(LE_curr));LE_curr++;}RE LE_curr <= n?memory.back():memory[n];}TE IN CO DynamicMods& DynamicMods::FactorialInverse(CRUI n){ST VE> memory ={one(),one()};ST uint LE_curr = 2;WH(LE_curr <= n){memory.push_back(memory[LE_curr - 1]* Inverse(LE_curr));LE_curr++;}RE memory[n];}TE IN DynamicMods DynamicMods::Combination(CRUI n,CRUI i){RE i <= n?Factorial(n)* FactorialInverse(i)* FactorialInverse(n - i):zero();}TE IN CRUI DynamicMods::RP()CO NE{RE m_n;}TE IN DynamicMods DynamicMods::DeRP(uint n)NE{DynamicMods n_copy{};n_copy.m_n = MO(n);RE n_copy;}TE IN CO DynamicMods& DynamicMods::zero()NE{ST CO DynamicMods z{};RE z;}TE IN CO DynamicMods& DynamicMods::one()NE{ST CO DynamicMods o{1};RE o;}TE IN CRUI DynamicMods::GetModulo()NE{RE COants::g_M;}TE IN VO DynamicMods::SetModulo(CRUI M,CRI order_minus_1)NE{COants::g_M = M;COants::g_M_minus = M - 1;COants::g_order = order_minus_1 == -1?M - 1:order_minus_1+1;COants::g_order_minus_1 = COants::g_order-1;COants::g_order_minus_1_neg = -COants::g_order_minus_1;COants::g_M_is_prime = order_minus_1 == -1;}TE IN DynamicMods Inverse(CO DynamicMods& n){RE MO(DynamicMods(n).Invert());}TE IN DynamicMods PW(DynamicMods n,INT EX){RE MO(n.PW(MO(EX)));}TE IN VO swap(DynamicMods& n0,DynamicMods& n1)NE{n0.swap(n1);}TE IN string to_string(CO DynamicMods& n)NE{RE to_string(n.RP())+ " + " + to_string(DynamicMods::GetModulo())+ "Z";}TE IN IS& OP>>(IS& is,DynamicMods& n){ll m;is >> m;n = m;RE is;}TE IN OS& OP<<(OS& os,CO DynamicMods& n){RE os << n.RP();} TE INT1 GCD(CO INT1& b_0,CO INT2& b_1){INT1 a_0 = b_0 < 0?-b_0:b_0;INT1 a_1 = b_1 < 0?-b_1:b_1;WH(a_1 != 0){swap(a_0 %= a_1,a_1);}RE a_0;}TE IN INT1 LCM(CO INT1& b_0,CO INT2& b_1){RE(b_0 == 0 && b_1 == 0)?0:(b_0 / GCD(b_0,b_1))* b_1;} // AAA ライブラリは以上に挿入する。 #define INCLUDE_MAIN #include __FILE__ #else // INCLUDE_LIBRARY #ifdef DEBUG #define _GLIBCXX_DEBUG #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE2 ) #define SIGNAL signal( SIGABRT , &AlertAbort ); #define ASSERT( A , MIN , MAX ) CERR( "ASSERTチェック: " , ( MIN ) , ( ( MIN ) <= A ? "<=" : ">" ) , A , ( A <= ( MAX ) ? "<=" : ">" ) , ( MAX ) ); assert( ( MIN ) <= A && A <= ( MAX ) ) #define COUT( ... ) VariadicCout( cout << "出力:" , __VA_ARGS__ ) << endl #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) << flush #define CERR( ... ) VariadicCout( cerr , __VA_ARGS__ ) << endl #define CERRNS( ... ) VariadicCout( cerr , __VA_ARGS__ ) << flush #define COUT_A( A , N ) OUTPUT_ARRAY( cout << "出力:" , A , N ) << endl #define CERR_A( A , N ) OUTPUT_ARRAY( cerr , A , N ) << endl int exec_mode = 0; #else #pragma GCC optimize ( "O3" ) #pragma GCC optimize ( "unroll-loops" ) #pragma GCC target ( "sse4.2,fma,avx2,popcnt,lzcnt,bmi2" ) #define SIGNAL #define DEXPR( LL , BOUND , VALUE1 , VALUE2 ) CEXPR( LL , BOUND , VALUE1 ) #define ASSERT( A , MIN , MAX ) assert( ( MIN ) <= A && A <= ( MAX ) ) #define COUT( ... ) VariadicCout( cout , __VA_ARGS__ ) << ENDL #define COUTNS( ... ) VariadicCoutNonSep( cout , __VA_ARGS__ ) #define CERR( ... ) #define CERRNS( ... ) #define COUT_A( A , N ) OUTPUT_ARRAY( cout , A , N ) << ENDL #define CERR_A( A , N ) #endif #ifdef REACTIVE #ifdef DEBUG #define RSET( A , ... ) A = __VA_ARGS__ #else #define RSET( A , ... ) cin >> A #endif #define RCIN( LL , A , ... ) LL A; RSET( A , __VA_ARGS__ ) #define ENDL endl #else #define ENDL "\n" #endif #ifdef USE_GETLINE #define SET_LL( A ) { GETLINE( A ## _str ); A = stoll( A ## _str ); } #define GETLINE_SEPARATE( SEPARATOR , ... ) string __VA_ARGS__; VariadicGetline( cin , SEPARATOR , __VA_ARGS__ ) #define GETLINE( ... ) GETLINE_SEPARATE( '\n' , __VA_ARGS__ ) #else #define SET_LL( A ) cin >> A #define CIN( LL , ... ) LL __VA_ARGS__; VariadicCin( cin , __VA_ARGS__ ) #define SET_A( I , N , ... ) 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 , ... ) vector __VA_ARGS__; SET_A( I , N , __VA_ARGS__ ) #define CIN_AA( LL , I0 , N0 , I1 , N1 , VAR ) vector> VAR( N0 + I0 ); FOR( VARIABLE_FOR_CIN_AA , 0 , N0 ){ SET_A( I1 , N1 , VAR[VARIABLE_FOR_CIN_AA + I0] ); } #endif #include using namespace std; #define REPEAT_MAIN( BOUND ) int main(){ ios_base::sync_with_stdio( false ); cin.tie( nullptr ); SIGNAL; CEXPR( int , bound_test_case_num , BOUND ); int test_case_num = 1; if constexpr( bound_test_case_num > 1 ){ CERR( "テストケースの個数を入力してください。" ); SET_ASSERT( test_case_num , 1 , bound_test_case_num ); } REPEAT( test_case_num ){ if constexpr( bound_test_case_num > 1 ){ CERR( "testcase " , VARIABLE_FOR_REPEAT_test_case_num , ":" ); } Solve(); CERR( "" ); } CHECK_REDUNDANT_INPUT; } #define START_WATCH chrono::system_clock::time_point watch = chrono::system_clock::now() #define CURRENT_TIME static_cast( chrono::duration_cast( chrono::system_clock::now() - watch ).count() / 1000.0 ) #define CHECK_WATCH( TL_MS ) ( CURRENT_TIME < TL_MS - 100.0 ) #define CEXPR( LL , BOUND , VALUE ) constexpr LL BOUND = VALUE #define SET_ASSERT( A , MIN , MAX ) SET_LL( A ); ASSERT( A , MIN , MAX ) #define SET_A_ASSERT( I , N , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_A , 0 , N ){ SET_ASSERT( A[VARIABLE_FOR_SET_A + I] , MIN , MAX ); } #define SET_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) FOR( VARIABLE_FOR_SET_AA0 , 0 , N0 ){ FOR( VARIABLE_FOR_SET_AA1 , 0 , N1 ){ SET_ASSERT( A[VARIABLE_FOR_SET_AA0 + I0][VARIABLE_FOR_SET_AA1 + I1] , 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 A( N + I ); SET_A_ASSERT( I , N , A , MIN , MAX ) #define CIN_AA_ASSERT( I0 , N0 , I1 , N1 , A , MIN , MAX ) vector A( N0 + I0 , vector( N1 + I1 ) ); SET_AA_ASSERT( I0 , N0 , I1 , N1 , 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( ARRAY , ... ) for( auto&& __VA_ARGS__ : ARRAY ) #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( ... ) COUT( __VA_ARGS__ ); return // 型のエイリアス #define decldecay_t( VAR ) decay_t template using ret_t = decltype( declval()( declval()... ) ); template using inner_t = typename T::type; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; using lld = __float128; template using T2 = pair; template using T3 = tuple; template using T4 = tuple; using path = pair; /* VVV 常設ライブラリの非圧縮版は以下に挿入する。*/ // Random ll GetRand( const int& Rand_min , const int& Rand_max ) { assert( Rand_min <= Rand_max ); ll answer = time( NULL ); return answer * rand() % ( Rand_max + 1 - Rand_min ) + Rand_min; } // Set #define DECLARATION_OF_HASH( ... ) \ struct hash<__VA_ARGS__> \ { \ \ inline size_t operator()( const __VA_ARGS__& n ) const; \ \ }; \ class is_ordered { private: is_ordered() = delete; template static constexpr auto Check( const T& t ) -> decltype( t < t , true_type() ); static constexpr false_type Check( ... ); public: template static constexpr const bool value = is_same_v< decltype( Check( declval() ) ) , true_type >; }; template using Set = conditional_t>,unordered_set,conditional_t,set,void>>; // Tuple #define DECLARATION_OF_ARITHMETIC_FOR_TUPLE( OPR ) \ template typename V> inline auto operator OPR ## =( V& t0 , const V& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )&; \ template inline tuple& operator OPR ## =( tuple& t0 , const tuple& t1 ); \ template inline tuple& operator OPR ## =( tuple& t0 , const tuple& t1 ); \ template typename V> inline auto operator OPR ## =( V& t0 , const ARG& t1 ) -> decltype( ( get<0>( t0 ) , t0 ) )&; \ template inline tuple& operator OPR ## =( tuple& t0 , const ARG& t1 ); \ template inline tuple& operator OPR ## =( tuple& t0 , const ARG& t1 ); \ template