#include <iostream> #include <vector> using namespace std; namespace { int p; using i64 = long long; class Modulo { int x; public: Modulo(): x{0} { } Modulo(int x_): x{x_} { if (x >= p) x %= p; } Modulo& operator+=(Modulo rhs) { x += rhs.x; if (x >= p) x %= p; return *this; } Modulo& operator*=(Modulo rhs) { x = (i64{x} * rhs.x) % p; return *this; } friend Modulo operator+(Modulo lhs, Modulo rhs) { return Modulo{lhs.x + rhs.x}; } friend Modulo operator*(Modulo lhs, Modulo rhs) { return Modulo(i64{lhs.x} * rhs.x % p); } friend ostream& operator<<(ostream& os, Modulo m) { return os << m.x; } }; Modulo solve(int n, int k) { if (k >= 11) return 0; if (n == 1) { return k == 0 ? 1 : 0; } vector<vector<vector<Modulo>>> eq(3, vector<vector<Modulo>>(n + 1, vector<Modulo>(k + 1))); vector<vector<vector<Modulo>>> le(3, vector<vector<Modulo>>(n + 1, vector<Modulo>(k + 1))); for (int s = 0; s <= 2; ++s) { eq[s][0][0] = 1; for (int j = 0; j <= k; ++j) { le[s][0][j] = 1; } } vector<Modulo> choose(n); choose[0] = 1; for (int i = 1; i <= n; ++i) { choose[i - 1] = 1; for (int j = i - 2; j > 0; --j) { choose[j] += choose[j - 1]; } for (int j = 1; j <= k; ++j) { for (int a = 0; a < i; ++a) { int b = i - 1 - a; eq[2][i][j] += choose[a] * (eq[2][a][j - 1] * eq[2][b][j - 1] + eq[2][a][j] * le[2][b][j - 1] + le[2][a][j - 1] * eq[2][b][j]); eq[1][i][j] += choose[a] * (eq[2][a][j - 1] * le[1][b][j - 1] + le[2][a][j - 1] * eq[1][b][j]); eq[0][i][j] += choose[a] * (eq[1][a][j] * le[1][b][j - 1] + le[1][a][j - 1] * eq[1][b][j] + eq[1][a][j] * eq[1][b][j]); } for (int s = 0; s <= 2; ++s) { le[s][i][j] = le[s][i][j - 1] + eq[s][i][j]; } } } return eq[0][n][k]; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k >> p; cout << solve(n, k) << endl; 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 | #include <iostream> #include <vector> using namespace std; namespace { int p; using i64 = long long; class Modulo { int x; public: Modulo(): x{0} { } Modulo(int x_): x{x_} { if (x >= p) x %= p; } Modulo& operator+=(Modulo rhs) { x += rhs.x; if (x >= p) x %= p; return *this; } Modulo& operator*=(Modulo rhs) { x = (i64{x} * rhs.x) % p; return *this; } friend Modulo operator+(Modulo lhs, Modulo rhs) { return Modulo{lhs.x + rhs.x}; } friend Modulo operator*(Modulo lhs, Modulo rhs) { return Modulo(i64{lhs.x} * rhs.x % p); } friend ostream& operator<<(ostream& os, Modulo m) { return os << m.x; } }; Modulo solve(int n, int k) { if (k >= 11) return 0; if (n == 1) { return k == 0 ? 1 : 0; } vector<vector<vector<Modulo>>> eq(3, vector<vector<Modulo>>(n + 1, vector<Modulo>(k + 1))); vector<vector<vector<Modulo>>> le(3, vector<vector<Modulo>>(n + 1, vector<Modulo>(k + 1))); for (int s = 0; s <= 2; ++s) { eq[s][0][0] = 1; for (int j = 0; j <= k; ++j) { le[s][0][j] = 1; } } vector<Modulo> choose(n); choose[0] = 1; for (int i = 1; i <= n; ++i) { choose[i - 1] = 1; for (int j = i - 2; j > 0; --j) { choose[j] += choose[j - 1]; } for (int j = 1; j <= k; ++j) { for (int a = 0; a < i; ++a) { int b = i - 1 - a; eq[2][i][j] += choose[a] * (eq[2][a][j - 1] * eq[2][b][j - 1] + eq[2][a][j] * le[2][b][j - 1] + le[2][a][j - 1] * eq[2][b][j]); eq[1][i][j] += choose[a] * (eq[2][a][j - 1] * le[1][b][j - 1] + le[2][a][j - 1] * eq[1][b][j]); eq[0][i][j] += choose[a] * (eq[1][a][j] * le[1][b][j - 1] + le[1][a][j - 1] * eq[1][b][j] + eq[1][a][j] * eq[1][b][j]); } for (int s = 0; s <= 2; ++s) { le[s][i][j] = le[s][i][j - 1] + eq[s][i][j]; } } } return eq[0][n][k]; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k >> p; cout << solve(n, k) << endl; return 0; } |