#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int read() { int n; scanf("%d", &n); return n; } int mod; ll A(int n, int k); ll B(int n, int k); ll C(int n, int k); ll SB(int n, int k) { static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = 0; for (int i=1; i<=k; ++i) res += B(n, i); res %= mod; data[n][k] = res; return res; } ll SC(int n, int k) { static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = 0; for (int i=1; i<=k; ++i) res += C(n, i); res %= mod; data[n][k] = res; return res; } ll binom(int n, int k) { if (k == 0 || k == n) return 1; static vector<vector<ll> > data(1009, vector<ll>(1009, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = binom(n-1, k-1) + binom(n-1, k); if (res >= mod) res -= mod; data[n][k] = res; return res; } ll powMod(ll a, int n) { ll res = 1; while (n > 0) { if (n & 1) res = (res * a) % mod; a = (a * a) % mod; n >>= 1; } return res; } ll A(int n, int k) { //printf("A(%d, %d)\n", n, k); if (n == 1) return k==0 ? 1 : 0; if (n == 2) return k==1 ? 2 : 0; if (k == 1) return powMod(2, n-1); ll res = B(n-1, k) * 2; for (int i=1; i+1<n; ++i) { ll cur = (B(i, k) * SB(n-1-i, k-1) + SB(i, k) * B(n-1-i, k)) % mod; res = (res + cur * binom(n-1, i)) % mod; } return res; } ll B(int n, int k) { //printf("B(%d, %d)\n", n, k); if (n < 1 || k < 1) return 0; if (k == 1) return 1; if (n == 2) return k==2 ? 1 : 0; static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = B(n-1, k) + C(n-1, k-1); for (int i=1; i+1<n; ++i) { ll cur = (B(i, k) * SC(n-1-i, k-1) + SB(i, k-1) * C(n-1-i, k-1)) % mod; res = (res + cur * binom(n-1, i)) % mod; } data[n][k] = res; return res; } ll C(int n, int k) { //printf("C(%d, %d)\n", n, k); if (n < 1 || k < 1) return 0; if (n <= 2) return k==1 ? n : 0; if (k == 1) return powMod(2, n-1); static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = C(n-1, k) * 2; for (int i=1; i+1<n; ++i) { ll cur = (C(i, k) * SC(n-1-i, k-1) + SC(i, k-1) * C(n-1-i, k) + C(i, k-1) * C(n-1-i, k-1)) % mod; res = (res + cur * binom(n-1, i)) % mod; } data[n][k] = res; return res; } int main() { int n, k; scanf("%d%d%d", &n, &k, &mod); int res = 0; if (k <= 11) res = A(n, k); printf("%d\n", res); return 0; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int read() { int n; scanf("%d", &n); return n; } int mod; ll A(int n, int k); ll B(int n, int k); ll C(int n, int k); ll SB(int n, int k) { static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = 0; for (int i=1; i<=k; ++i) res += B(n, i); res %= mod; data[n][k] = res; return res; } ll SC(int n, int k) { static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = 0; for (int i=1; i<=k; ++i) res += C(n, i); res %= mod; data[n][k] = res; return res; } ll binom(int n, int k) { if (k == 0 || k == n) return 1; static vector<vector<ll> > data(1009, vector<ll>(1009, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = binom(n-1, k-1) + binom(n-1, k); if (res >= mod) res -= mod; data[n][k] = res; return res; } ll powMod(ll a, int n) { ll res = 1; while (n > 0) { if (n & 1) res = (res * a) % mod; a = (a * a) % mod; n >>= 1; } return res; } ll A(int n, int k) { //printf("A(%d, %d)\n", n, k); if (n == 1) return k==0 ? 1 : 0; if (n == 2) return k==1 ? 2 : 0; if (k == 1) return powMod(2, n-1); ll res = B(n-1, k) * 2; for (int i=1; i+1<n; ++i) { ll cur = (B(i, k) * SB(n-1-i, k-1) + SB(i, k) * B(n-1-i, k)) % mod; res = (res + cur * binom(n-1, i)) % mod; } return res; } ll B(int n, int k) { //printf("B(%d, %d)\n", n, k); if (n < 1 || k < 1) return 0; if (k == 1) return 1; if (n == 2) return k==2 ? 1 : 0; static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = B(n-1, k) + C(n-1, k-1); for (int i=1; i+1<n; ++i) { ll cur = (B(i, k) * SC(n-1-i, k-1) + SB(i, k-1) * C(n-1-i, k-1)) % mod; res = (res + cur * binom(n-1, i)) % mod; } data[n][k] = res; return res; } ll C(int n, int k) { //printf("C(%d, %d)\n", n, k); if (n < 1 || k < 1) return 0; if (n <= 2) return k==1 ? n : 0; if (k == 1) return powMod(2, n-1); static vector<vector<ll> > data(1009, vector<ll>(12, -1)); if (data[n][k] >= 0) return data[n][k]; ll res = C(n-1, k) * 2; for (int i=1; i+1<n; ++i) { ll cur = (C(i, k) * SC(n-1-i, k-1) + SC(i, k-1) * C(n-1-i, k) + C(i, k-1) * C(n-1-i, k-1)) % mod; res = (res + cur * binom(n-1, i)) % mod; } data[n][k] = res; return res; } int main() { int n, k; scanf("%d%d%d", &n, &k, &mod); int res = 0; if (k <= 11) res = A(n, k); printf("%d\n", res); return 0; } |