#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int MaxN = 1024; const int LogMaxN = 15; int N, K, P; int binom[MaxN][MaxN]; int factorials[MaxN]; int dp_side[MaxN][LogMaxN]; int dp_full_side[MaxN][LogMaxN]; int dp_full_side_suf[MaxN][LogMaxN]; int dp_side_pref[MaxN][LogMaxN]; int dp_ds_dfs[MaxN][LogMaxN]; int dp_ds_dfs_suf[MaxN][LogMaxN]; int dp_dfs_dfs[MaxN][LogMaxN]; int dp_dfs_dfs_suf[MaxN][LogMaxN]; int dp_dfs_dfsprev[MaxN][LogMaxN]; int dp_dfs_dfsprev_suf[MaxN][LogMaxN]; int dp_answer_pref[MaxN][LogMaxN]; void MakeBinoms() { binom[0][0] = 1; for (int n = 1; n <= N; ++n) { binom[n][0] = 1; for (int k = 1; k <= n; ++k) { binom[n][k] = (binom[n - 1][k - 1] + binom[n - 1][k]) % P; } } factorials[0] = 1; for (int i = 1; i <= N; ++i) factorials[i] = ((LL)factorials[i - 1] * i) % P; } void MakeDP() { dp_side[1][0] = dp_full_side[1][0] = 1; dp_ds_dfs[1][0] = dp_dfs_dfs[1][0] = 1; for (int i = 0; i < LogMaxN; ++i) { dp_side_pref[1][i] = 1; dp_answer_pref[1][i] = 1; } for (int n = 2; n <= N; ++n) { for (int k = 1; k < LogMaxN; ++k) { LL num_side = 0; LL num_full_side = 0; for (int l = 1; l < n; ++l) { const LL coef_base = (LL)binom[n - 1][l] * factorials[n - l - 1] % P; const LL coef_side = (dp_ds_dfs[l][k - 1] /*- dp_ds_dfs_suf[l][k - 1]*/ + P) % P; const LL coef_full = ((LL)dp_dfs_dfs[l][k - 1] - dp_dfs_dfsprev[l][k] - dp_dfs_dfs_suf[l][k - 1] /*+ dp_dfs_dfsprev_suf[l][k]*/ + 2 * P) % P; num_side = (num_side + coef_base * coef_side) % P; num_full_side = (num_full_side + coef_base * coef_full) % P; if (n == 3 && k <= 2) { debug("!", n, k, l, coef_base, coef_side); //debug("!", n, k, l, coef_base, coef_full); //debug(dp_dfs_dfs[l][k - 1], dp_dfs_dfsprev[l][k]); //debug(dp_dfs_dfs_suf[l][k - 1], dp_dfs_dfsprev_suf[l][k]); } } dp_side[n][k] = num_side; dp_full_side[n][k] = num_full_side; } if (n == 3) { debug(dp_side[n][1], dp_side[n][2], dp_side[n][3]); } // dp_full_side_suf[n][0] = dp_full_side[n][1]; for (int k = 1; k < LogMaxN - 1; ++k) { // dp_full_side_suf[n][k] = dp_full_side[n][k]; dp_side[n][k] = (dp_side[n][k] - dp_side[n][k + 1] + P) % P; // dp_full_side[n][k] = // (dp_full_side[n][k] - dp_full_side[n][k + 1] + P) % P; } dp_full_side_suf[n][LogMaxN - 1] = dp_full_side[n][LogMaxN - 1]; for (int i = LogMaxN - 2; i >= 0; --i) { dp_full_side_suf[n][i] = (dp_full_side_suf[n][i + 1] + dp_full_side[n][i]) % P; } for (int k = 1; k < LogMaxN; ++k) { dp_side_pref[n][k] = (dp_side_pref[n][k - 1] + dp_side[n][k]) % P; } for (int k = 0; k < LogMaxN - 1; ++k) { for (int left = 1; left <= n; ++left) { const int right = n - left + 1; // ds_dfs, ds_dfsj, dfs_dfs, dfs_dfsj const int base = binom[n - 1][left - 1]; const LL ds_base = (LL)base * dp_side_pref[left][k] % P; const LL dfs_base = (LL)base * dp_full_side[left][k] % P; if (n == 3 && k == 1) { debug("?", n, k, left, base, dp_side_pref[left][k], dp_full_side_suf[right][k]); } dp_ds_dfs[n][k] = (dp_ds_dfs[n][k] + ds_base * dp_full_side[right][k]) % P; //dp_ds_dfs_suf[n][k] = // (dp_ds_dfs[n][k] + ds_base * dp_full_side[right][k + 1]) % P; dp_dfs_dfs[n][k] = (dp_dfs_dfs[n][k] + dfs_base * dp_full_side[right][k]) % P; dp_dfs_dfs_suf[n][k] = (dp_dfs_dfs_suf[n][k] + dfs_base * dp_full_side[right][k + 1]) % P; if (k) { dp_dfs_dfsprev[n][k] = (dp_dfs_dfsprev[n][k] + dfs_base * dp_full_side[right][k - 1]) % P; dp_dfs_dfsprev_suf[n][k] = (dp_dfs_dfsprev_suf[n][k] + dfs_base * dp_full_side[right][k]) % P; } dp_answer_pref[n][k] = (dp_answer_pref[n][k] + ds_base * dp_side_pref[right][k]) % P; } } for (int k = 1; k < LogMaxN; ++k) { if (dp_side[n][k] || k == 1) { debug(n, k, dp_side[n][k], dp_full_side[n][k], dp_answer_pref[n][k]); } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> N >> K >> P; MakeBinoms(); MakeDP(); #ifdef TOTALTEST int tot = 0; for (int n = 1; n <= N; ++n) { for (int k = 1; k < LogMaxN - 2; ++k) { const int v = (dp_answer_pref[n][k] - dp_answer_pref[n][k - 1] + P) % P; tot = (tot + (LL)n * k * v) % P; } } cout << tot << "\n"; #endif assert(dp_answer_pref[N][12] == factorials[N]); if (K > 12) { cout << "0\n"; return 0; } cout << (dp_answer_pref[N][K] - dp_answer_pref[N][K - 1] + P) % P << "\n"; }
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define unordered_map __fast_unordered_map template<class Key, class Value, class Hash = std::hash<Key>> using unordered_map = __gnu_pbds::gp_hash_table<Key, Value, Hash>; template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif const int MaxN = 1024; const int LogMaxN = 15; int N, K, P; int binom[MaxN][MaxN]; int factorials[MaxN]; int dp_side[MaxN][LogMaxN]; int dp_full_side[MaxN][LogMaxN]; int dp_full_side_suf[MaxN][LogMaxN]; int dp_side_pref[MaxN][LogMaxN]; int dp_ds_dfs[MaxN][LogMaxN]; int dp_ds_dfs_suf[MaxN][LogMaxN]; int dp_dfs_dfs[MaxN][LogMaxN]; int dp_dfs_dfs_suf[MaxN][LogMaxN]; int dp_dfs_dfsprev[MaxN][LogMaxN]; int dp_dfs_dfsprev_suf[MaxN][LogMaxN]; int dp_answer_pref[MaxN][LogMaxN]; void MakeBinoms() { binom[0][0] = 1; for (int n = 1; n <= N; ++n) { binom[n][0] = 1; for (int k = 1; k <= n; ++k) { binom[n][k] = (binom[n - 1][k - 1] + binom[n - 1][k]) % P; } } factorials[0] = 1; for (int i = 1; i <= N; ++i) factorials[i] = ((LL)factorials[i - 1] * i) % P; } void MakeDP() { dp_side[1][0] = dp_full_side[1][0] = 1; dp_ds_dfs[1][0] = dp_dfs_dfs[1][0] = 1; for (int i = 0; i < LogMaxN; ++i) { dp_side_pref[1][i] = 1; dp_answer_pref[1][i] = 1; } for (int n = 2; n <= N; ++n) { for (int k = 1; k < LogMaxN; ++k) { LL num_side = 0; LL num_full_side = 0; for (int l = 1; l < n; ++l) { const LL coef_base = (LL)binom[n - 1][l] * factorials[n - l - 1] % P; const LL coef_side = (dp_ds_dfs[l][k - 1] /*- dp_ds_dfs_suf[l][k - 1]*/ + P) % P; const LL coef_full = ((LL)dp_dfs_dfs[l][k - 1] - dp_dfs_dfsprev[l][k] - dp_dfs_dfs_suf[l][k - 1] /*+ dp_dfs_dfsprev_suf[l][k]*/ + 2 * P) % P; num_side = (num_side + coef_base * coef_side) % P; num_full_side = (num_full_side + coef_base * coef_full) % P; if (n == 3 && k <= 2) { debug("!", n, k, l, coef_base, coef_side); //debug("!", n, k, l, coef_base, coef_full); //debug(dp_dfs_dfs[l][k - 1], dp_dfs_dfsprev[l][k]); //debug(dp_dfs_dfs_suf[l][k - 1], dp_dfs_dfsprev_suf[l][k]); } } dp_side[n][k] = num_side; dp_full_side[n][k] = num_full_side; } if (n == 3) { debug(dp_side[n][1], dp_side[n][2], dp_side[n][3]); } // dp_full_side_suf[n][0] = dp_full_side[n][1]; for (int k = 1; k < LogMaxN - 1; ++k) { // dp_full_side_suf[n][k] = dp_full_side[n][k]; dp_side[n][k] = (dp_side[n][k] - dp_side[n][k + 1] + P) % P; // dp_full_side[n][k] = // (dp_full_side[n][k] - dp_full_side[n][k + 1] + P) % P; } dp_full_side_suf[n][LogMaxN - 1] = dp_full_side[n][LogMaxN - 1]; for (int i = LogMaxN - 2; i >= 0; --i) { dp_full_side_suf[n][i] = (dp_full_side_suf[n][i + 1] + dp_full_side[n][i]) % P; } for (int k = 1; k < LogMaxN; ++k) { dp_side_pref[n][k] = (dp_side_pref[n][k - 1] + dp_side[n][k]) % P; } for (int k = 0; k < LogMaxN - 1; ++k) { for (int left = 1; left <= n; ++left) { const int right = n - left + 1; // ds_dfs, ds_dfsj, dfs_dfs, dfs_dfsj const int base = binom[n - 1][left - 1]; const LL ds_base = (LL)base * dp_side_pref[left][k] % P; const LL dfs_base = (LL)base * dp_full_side[left][k] % P; if (n == 3 && k == 1) { debug("?", n, k, left, base, dp_side_pref[left][k], dp_full_side_suf[right][k]); } dp_ds_dfs[n][k] = (dp_ds_dfs[n][k] + ds_base * dp_full_side[right][k]) % P; //dp_ds_dfs_suf[n][k] = // (dp_ds_dfs[n][k] + ds_base * dp_full_side[right][k + 1]) % P; dp_dfs_dfs[n][k] = (dp_dfs_dfs[n][k] + dfs_base * dp_full_side[right][k]) % P; dp_dfs_dfs_suf[n][k] = (dp_dfs_dfs_suf[n][k] + dfs_base * dp_full_side[right][k + 1]) % P; if (k) { dp_dfs_dfsprev[n][k] = (dp_dfs_dfsprev[n][k] + dfs_base * dp_full_side[right][k - 1]) % P; dp_dfs_dfsprev_suf[n][k] = (dp_dfs_dfsprev_suf[n][k] + dfs_base * dp_full_side[right][k]) % P; } dp_answer_pref[n][k] = (dp_answer_pref[n][k] + ds_base * dp_side_pref[right][k]) % P; } } for (int k = 1; k < LogMaxN; ++k) { if (dp_side[n][k] || k == 1) { debug(n, k, dp_side[n][k], dp_full_side[n][k], dp_answer_pref[n][k]); } } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> N >> K >> P; MakeBinoms(); MakeDP(); #ifdef TOTALTEST int tot = 0; for (int n = 1; n <= N; ++n) { for (int k = 1; k < LogMaxN - 2; ++k) { const int v = (dp_answer_pref[n][k] - dp_answer_pref[n][k - 1] + P) % P; tot = (tot + (LL)n * k * v) % P; } } cout << tot << "\n"; #endif assert(dp_answer_pref[N][12] == factorials[N]); if (K > 12) { cout << "0\n"; return 0; } cout << (dp_answer_pref[N][K] - dp_answer_pref[N][K - 1] + P) % P << "\n"; } |