#include <vector> #include <iostream> #include <cassert> using namespace std; #define NDEBUG #define LAZY 0 using ll = long long; ll fpw(ll a, ll b, ll p){ ll r = 1; while(b) { if(b&1) r = r*a%p; a = a*a%p; b /= 2; } return r; } ll M; vector<ll> fac; vector<ll> fac_inv; vector<ll> Vcompute_range; vector<ll> Vcompute; vector<vector<ll> > Vnewton; inline ll AM(ll a) { //if (a > M) a -= M; return a; return a % M; } const int MLIMIT = 12; int I(ll n, ll m, bool sl, bool sr) { assert(n >= 0 && m >= 0); assert(m < MLIMIT && n <= 1000); //return (MLIMIT*n + m) * 4 + (sl?1:0) + (sr?2:0); return (MLIMIT*n + m) + ((sl?1:0) + (sr?2:0))*1001*MLIMIT; //return (n + m*1001) + ((sl?1:0) + (sr?2:0))*1001*MLIMIT; } ll newton(ll n, ll k) { return Vnewton[n][k];//(((fac[n] * fac_inv[k]) % M) * fac_inv[n-k]) % M; } ll do_compute_range(ll n, ll m, ll sl, ll sr); ll do_compute(ll n, ll m, ll sl, ll sr); ll compute_range(ll n, ll m, ll sl, ll sr) { if (m <= 0) return 0; #if LAZY if (Vcompute_range[I(n,m,sl,sr)] == -1) Vcompute_range[I(n,m,sl,sr)] = do_compute_range(n,m,sl,sr); #endif return Vcompute_range[I(n,m,sl,sr)]; } ll compute(ll n, ll m, ll sl, ll sr) { if (m < 0) return 0; #if LAZY if (Vcompute[I(n,m,sl,sr)] == -1) Vcompute[I(n,m,sl,sr)] = do_compute(n,m,sl,sr); #endif return Vcompute[I(n,m,sl,sr)]; } ll do_compute_range(ll n, ll m, ll sl, ll sr) { if(m <= 0) return 0; return AM(compute(n, m-1, sl, sr) + compute_range(n, m-1, sl, sr)); } ll do_compute(ll n, ll m, ll sl, ll sr) { if(n == 0) { if(m == 0) { return 1; } else { return 0; } } if(m < 0) return 0; ll res = 0; bool fastmode = sl == sr; for(ll k1=0; k1 < (fastmode ? ((n+1)/2) : n); k1++) { ll k2 = n - 1 - k1; ll x = newton(n-1, k1); ll lres = 0; // both finish at the same time lres += (compute(k1, m-1, sl, true) * compute(k2, m-1, true, sr)) % M; //lres %= M; if(sl) { // left dies first, we are consumed by wall lres += (compute_range(k1, m, sl, true) * compute(k2, m, true, sr)) % M; } else { // left dies first, no wall lres += (compute_range(k1, m-1, sl, true) * compute(k2, m-1, true, sr)) % M; } //lres %= M; if(sr) { // right dies first, we are consumed by wall lres += (compute(k1, m, sl, true) * compute_range(k2, m, true, sr)) % M; } else { // right dies first, no wall lres += (compute(k1, m-1, sl, true) * compute_range(k2, m-1, true, sr)) % M; } if (fastmode && 2*k1+1 != n) { lres *= 2; } lres %= M; //print(n,m, '..', k1, k2, '->', lres) res += (lres * x); res %= M; } return res; } bool debug = false; int main() { int n, k; cin >> n >> k >> M; if (M == -1) { debug = true; M = 100000007; } Vcompute.resize(1001 * MLIMIT * 4, -1); Vcompute_range.resize(1001 * MLIMIT * 4, -1); fac.push_back(1); fac_inv.push_back(1); for (int i=1; i <= 1005; i ++) { fac.push_back((fac.back() * i) % M); fac_inv.push_back(fpw(fac.back(), M-2, M)); } Vnewton.resize(1001, vector<ll>(1001)); for (int n=0; n < 1001; n++) for (int k=0; k < 1001; k++) Vnewton[n][k] = (((fac[n] * fac_inv[k]) % M) * fac_inv[n-k]) % M; #if !LAZY for (ll n=0; n <= 1000; n++ ){ for (ll m=0; m < MLIMIT; m++ ){ for (int sl=0; sl <= 1; sl ++) { for (int sr=0; sr <= 1; sr ++) { Vcompute[I(n,m, sl, sr)] = do_compute(n,m, sl, sr); Vcompute_range[I(n,m, sl, sr)] = do_compute_range(n,m, sl, sr); } } } } #endif if (debug) { for (int n=0; n <= 1000; n ++){ cout << "["; for (int m=0; m < MLIMIT; m ++){ if(m) cout << ", "; cout << compute(n, m, false, false); } cout << "]" << endl; } cout << compute(1000, 10 + 1, false, false) << endl; ll chksum = 0; for (int n=0; n <= 1000; n ++){ for (int m=1; m < MLIMIT; m ++){ chksum += (compute(n, m, false, false) * n * (m-1)) % M; chksum %= M; } } cout << chksum << endl; assert(chksum == 59180009); } else { k ++; if (k >= MLIMIT) { cout << 0 << endl; } else { cout << compute(n, k, false, false) << endl; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | #include <vector> #include <iostream> #include <cassert> using namespace std; #define NDEBUG #define LAZY 0 using ll = long long; ll fpw(ll a, ll b, ll p){ ll r = 1; while(b) { if(b&1) r = r*a%p; a = a*a%p; b /= 2; } return r; } ll M; vector<ll> fac; vector<ll> fac_inv; vector<ll> Vcompute_range; vector<ll> Vcompute; vector<vector<ll> > Vnewton; inline ll AM(ll a) { //if (a > M) a -= M; return a; return a % M; } const int MLIMIT = 12; int I(ll n, ll m, bool sl, bool sr) { assert(n >= 0 && m >= 0); assert(m < MLIMIT && n <= 1000); //return (MLIMIT*n + m) * 4 + (sl?1:0) + (sr?2:0); return (MLIMIT*n + m) + ((sl?1:0) + (sr?2:0))*1001*MLIMIT; //return (n + m*1001) + ((sl?1:0) + (sr?2:0))*1001*MLIMIT; } ll newton(ll n, ll k) { return Vnewton[n][k];//(((fac[n] * fac_inv[k]) % M) * fac_inv[n-k]) % M; } ll do_compute_range(ll n, ll m, ll sl, ll sr); ll do_compute(ll n, ll m, ll sl, ll sr); ll compute_range(ll n, ll m, ll sl, ll sr) { if (m <= 0) return 0; #if LAZY if (Vcompute_range[I(n,m,sl,sr)] == -1) Vcompute_range[I(n,m,sl,sr)] = do_compute_range(n,m,sl,sr); #endif return Vcompute_range[I(n,m,sl,sr)]; } ll compute(ll n, ll m, ll sl, ll sr) { if (m < 0) return 0; #if LAZY if (Vcompute[I(n,m,sl,sr)] == -1) Vcompute[I(n,m,sl,sr)] = do_compute(n,m,sl,sr); #endif return Vcompute[I(n,m,sl,sr)]; } ll do_compute_range(ll n, ll m, ll sl, ll sr) { if(m <= 0) return 0; return AM(compute(n, m-1, sl, sr) + compute_range(n, m-1, sl, sr)); } ll do_compute(ll n, ll m, ll sl, ll sr) { if(n == 0) { if(m == 0) { return 1; } else { return 0; } } if(m < 0) return 0; ll res = 0; bool fastmode = sl == sr; for(ll k1=0; k1 < (fastmode ? ((n+1)/2) : n); k1++) { ll k2 = n - 1 - k1; ll x = newton(n-1, k1); ll lres = 0; // both finish at the same time lres += (compute(k1, m-1, sl, true) * compute(k2, m-1, true, sr)) % M; //lres %= M; if(sl) { // left dies first, we are consumed by wall lres += (compute_range(k1, m, sl, true) * compute(k2, m, true, sr)) % M; } else { // left dies first, no wall lres += (compute_range(k1, m-1, sl, true) * compute(k2, m-1, true, sr)) % M; } //lres %= M; if(sr) { // right dies first, we are consumed by wall lres += (compute(k1, m, sl, true) * compute_range(k2, m, true, sr)) % M; } else { // right dies first, no wall lres += (compute(k1, m-1, sl, true) * compute_range(k2, m-1, true, sr)) % M; } if (fastmode && 2*k1+1 != n) { lres *= 2; } lres %= M; //print(n,m, '..', k1, k2, '->', lres) res += (lres * x); res %= M; } return res; } bool debug = false; int main() { int n, k; cin >> n >> k >> M; if (M == -1) { debug = true; M = 100000007; } Vcompute.resize(1001 * MLIMIT * 4, -1); Vcompute_range.resize(1001 * MLIMIT * 4, -1); fac.push_back(1); fac_inv.push_back(1); for (int i=1; i <= 1005; i ++) { fac.push_back((fac.back() * i) % M); fac_inv.push_back(fpw(fac.back(), M-2, M)); } Vnewton.resize(1001, vector<ll>(1001)); for (int n=0; n < 1001; n++) for (int k=0; k < 1001; k++) Vnewton[n][k] = (((fac[n] * fac_inv[k]) % M) * fac_inv[n-k]) % M; #if !LAZY for (ll n=0; n <= 1000; n++ ){ for (ll m=0; m < MLIMIT; m++ ){ for (int sl=0; sl <= 1; sl ++) { for (int sr=0; sr <= 1; sr ++) { Vcompute[I(n,m, sl, sr)] = do_compute(n,m, sl, sr); Vcompute_range[I(n,m, sl, sr)] = do_compute_range(n,m, sl, sr); } } } } #endif if (debug) { for (int n=0; n <= 1000; n ++){ cout << "["; for (int m=0; m < MLIMIT; m ++){ if(m) cout << ", "; cout << compute(n, m, false, false); } cout << "]" << endl; } cout << compute(1000, 10 + 1, false, false) << endl; ll chksum = 0; for (int n=0; n <= 1000; n ++){ for (int m=1; m < MLIMIT; m ++){ chksum += (compute(n, m, false, false) * n * (m-1)) % M; chksum %= M; } } cout << chksum << endl; assert(chksum == 59180009); } else { k ++; if (k >= MLIMIT) { cout << 0 << endl; } else { cout << compute(n, k, false, false) << endl; } } } |