#include <bits/stdc++.h> using namespace std; #ifdef LOCAL ostream& operator<<(ostream& os, vector<vector<long long>> v) { os << "{\n"; for (auto& v1 : v) { os << " {"; int o = 0; for (auto x : v1) { if (o++) { os << ", "; } os << x; } os << "}\n"; } return os << "}"; } #define debug(x) { cerr << #x << " = " << (x) << endl; } #else #define debug(...) {} #endif using ll = long long; using vvl = vector<vector<ll>>; const int K = 11; vvl compute_newton(int N, int p) { vvl newton(N + 1, vector<ll>(N + 1)); for (int n = 0; n <= N; n++) { newton[n][0] = 1; for (int i = 1; i <= n; i++) { newton[n][i] = (newton[n - 1][i - 1] + newton[n - 1][i]) % p; } } return newton; } pair<vvl, vvl> compute_dp2(int N, int p, const vvl &newton) { vvl dp2(N + 1, vector<ll>(K)); vvl dp2sum(N + 1, vector<ll>(K)); dp2[0][0] = 1; for (int k = 0; k < K; k++) { dp2sum[0][k] = 1; } for (int n = 1; n <= N; n++) { for (int k = 1; k < K; k++) { for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp2[i][k] * dp2sum[j][k - 1] + dp2sum[i][k - 1] * dp2[j][k] + dp2[i][k - 1] * dp2[j][k - 1]) % p; dp2[n][k] += newton[n - 1][i] * add; dp2[n][k] %= p; } dp2sum[n][k] = (dp2sum[n][k - 1] + dp2[n][k]) % p; } } return make_pair(dp2, dp2sum); } pair<vvl, vvl> compute_dp1(int N, int p, const vvl &dp2, const vvl &dp2sum, const vvl &newton) { vvl dp1(N + 1, vector<ll>(K)); vvl dp1sum(N + 1, vector<ll>(K)); dp1[0][0] = 1; for (int k = 0; k < K; k++) { dp1sum[0][k] = 1; } for (int n = 1; n <= N; n++) { for (int k = 1; k < K; k++) { for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp1sum[i][k - 1] * dp2[j][k - 1] + dp1[i][k] * dp2sum[j][k - 1]) % p; dp1[n][k] += newton[n - 1][i] * add; dp1[n][k] %= p; } dp1sum[n][k] = (dp1sum[n][k - 1] + dp1[n][k]) % p; } } return make_pair(dp1, dp1sum); } ll solve(int n, int k, int p) { if (k >= K) { return 0; } auto newton = compute_newton(n, p); debug(newton); auto dp2 = compute_dp2(n, p, newton); debug(dp2.first); debug(dp2.second); auto dp1 = compute_dp1(n, p, dp2.first, dp2.second, newton); debug(dp1.first); debug(dp1.second); ll res = 0; for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp1.first[i][k] * dp1.second[j][k - 1] + dp1.second[i][k - 1] * dp1.first[j][k] + dp1.first[i][k] * dp1.first[j][k]) % p; res += newton[n - 1][i] * add; res %= p; } return res; } int main() { ios_base::sync_with_stdio(false); int n, k, p; cin >> n >> k >> p; cout << solve(n, k, 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 | #include <bits/stdc++.h> using namespace std; #ifdef LOCAL ostream& operator<<(ostream& os, vector<vector<long long>> v) { os << "{\n"; for (auto& v1 : v) { os << " {"; int o = 0; for (auto x : v1) { if (o++) { os << ", "; } os << x; } os << "}\n"; } return os << "}"; } #define debug(x) { cerr << #x << " = " << (x) << endl; } #else #define debug(...) {} #endif using ll = long long; using vvl = vector<vector<ll>>; const int K = 11; vvl compute_newton(int N, int p) { vvl newton(N + 1, vector<ll>(N + 1)); for (int n = 0; n <= N; n++) { newton[n][0] = 1; for (int i = 1; i <= n; i++) { newton[n][i] = (newton[n - 1][i - 1] + newton[n - 1][i]) % p; } } return newton; } pair<vvl, vvl> compute_dp2(int N, int p, const vvl &newton) { vvl dp2(N + 1, vector<ll>(K)); vvl dp2sum(N + 1, vector<ll>(K)); dp2[0][0] = 1; for (int k = 0; k < K; k++) { dp2sum[0][k] = 1; } for (int n = 1; n <= N; n++) { for (int k = 1; k < K; k++) { for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp2[i][k] * dp2sum[j][k - 1] + dp2sum[i][k - 1] * dp2[j][k] + dp2[i][k - 1] * dp2[j][k - 1]) % p; dp2[n][k] += newton[n - 1][i] * add; dp2[n][k] %= p; } dp2sum[n][k] = (dp2sum[n][k - 1] + dp2[n][k]) % p; } } return make_pair(dp2, dp2sum); } pair<vvl, vvl> compute_dp1(int N, int p, const vvl &dp2, const vvl &dp2sum, const vvl &newton) { vvl dp1(N + 1, vector<ll>(K)); vvl dp1sum(N + 1, vector<ll>(K)); dp1[0][0] = 1; for (int k = 0; k < K; k++) { dp1sum[0][k] = 1; } for (int n = 1; n <= N; n++) { for (int k = 1; k < K; k++) { for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp1sum[i][k - 1] * dp2[j][k - 1] + dp1[i][k] * dp2sum[j][k - 1]) % p; dp1[n][k] += newton[n - 1][i] * add; dp1[n][k] %= p; } dp1sum[n][k] = (dp1sum[n][k - 1] + dp1[n][k]) % p; } } return make_pair(dp1, dp1sum); } ll solve(int n, int k, int p) { if (k >= K) { return 0; } auto newton = compute_newton(n, p); debug(newton); auto dp2 = compute_dp2(n, p, newton); debug(dp2.first); debug(dp2.second); auto dp1 = compute_dp1(n, p, dp2.first, dp2.second, newton); debug(dp1.first); debug(dp1.second); ll res = 0; for (int i = 0; i < n; i++) { int j = n - 1 - i; ll add = (dp1.first[i][k] * dp1.second[j][k - 1] + dp1.second[i][k - 1] * dp1.first[j][k] + dp1.first[i][k] * dp1.first[j][k]) % p; res += newton[n - 1][i] * add; res %= p; } return res; } int main() { ios_base::sync_with_stdio(false); int n, k, p; cin >> n >> k >> p; cout << solve(n, k, p) << "\n"; } |