#include <bits/stdc++.h> using namespace std; int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int n, k, p; cin >> n >> k >> p; vector< vector<int> > P(n + 1); P[0].push_back(1); for(int i = 1; i <= n; i++) { P[i].push_back(1); for(int j = 1; j < i; j++) { P[i].push_back((P[i-1][j-1] + P[i-1][j]) % p); } P[i].push_back(1); } vector< vector<int> > B(n + 1); B[0].push_back(1); B[1].push_back(0); B[1].push_back(1); B[2].push_back(0); B[2].push_back(2 % p); for(int i = 3; i <= n; i++) { for(int j = 0; j < i; j++) { for(int z = 0; z < int(B[j].size()); z++) { for(int y = 0; y < int(B[i - 1 - j].size()); y++) { if(y == z) { if(int(B[i].size()) <= z + 1) B[i].resize(z + 2, 0); B[i][z + 1] = (B[i][z + 1] + P[i-1][j] * (B[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } else { int t = max(y, z); if(int(B[i].size()) <= t) B[i].resize(t + 1, 0); B[i][t] = (B[i][t] + P[i-1][j] * (B[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } } } } } vector< vector<int> > G(n + 1); G[0].push_back(1); G[1].push_back(0); G[1].push_back(1); G[2].push_back(0); G[2].push_back(1); G[2].push_back(1); for(int i = 3; i <= n; i++) { for(int j = 0; j < i; j++) { for(int z = 0; z < int(G[j].size()); z++) { for(int y = 0; y < int(B[i - 1 - j].size()); y++) { int t = max(y + 1, z); if(int(G[i].size()) <= t) G[i].resize(t + 1, 0); G[i][t] = (G[i][t] + P[i-1][j] * (G[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } } } } vector<int> An; { int i = n; for(int j = 0; j < i; j++) { for(int z = 0; z < int(G[j].size()); z++) { for(int y = 0; y < int(G[i - 1 - j].size()); y++) { int t = max(y, z); if(int(An.size()) <= t) An.resize(t + 1, 0); An[t] = (An[t] + P[i-1][j] * (G[j][z] * int64_t(G[i - 1 - j][y]) % p)) % p; } } } } cout << (k < int(An.size()) ? An[k] : 0) << '\n'; 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 | #include <bits/stdc++.h> using namespace std; int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int n, k, p; cin >> n >> k >> p; vector< vector<int> > P(n + 1); P[0].push_back(1); for(int i = 1; i <= n; i++) { P[i].push_back(1); for(int j = 1; j < i; j++) { P[i].push_back((P[i-1][j-1] + P[i-1][j]) % p); } P[i].push_back(1); } vector< vector<int> > B(n + 1); B[0].push_back(1); B[1].push_back(0); B[1].push_back(1); B[2].push_back(0); B[2].push_back(2 % p); for(int i = 3; i <= n; i++) { for(int j = 0; j < i; j++) { for(int z = 0; z < int(B[j].size()); z++) { for(int y = 0; y < int(B[i - 1 - j].size()); y++) { if(y == z) { if(int(B[i].size()) <= z + 1) B[i].resize(z + 2, 0); B[i][z + 1] = (B[i][z + 1] + P[i-1][j] * (B[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } else { int t = max(y, z); if(int(B[i].size()) <= t) B[i].resize(t + 1, 0); B[i][t] = (B[i][t] + P[i-1][j] * (B[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } } } } } vector< vector<int> > G(n + 1); G[0].push_back(1); G[1].push_back(0); G[1].push_back(1); G[2].push_back(0); G[2].push_back(1); G[2].push_back(1); for(int i = 3; i <= n; i++) { for(int j = 0; j < i; j++) { for(int z = 0; z < int(G[j].size()); z++) { for(int y = 0; y < int(B[i - 1 - j].size()); y++) { int t = max(y + 1, z); if(int(G[i].size()) <= t) G[i].resize(t + 1, 0); G[i][t] = (G[i][t] + P[i-1][j] * (G[j][z] * int64_t(B[i - 1 - j][y]) % p)) % p; } } } } vector<int> An; { int i = n; for(int j = 0; j < i; j++) { for(int z = 0; z < int(G[j].size()); z++) { for(int y = 0; y < int(G[i - 1 - j].size()); y++) { int t = max(y, z); if(int(An.size()) <= t) An.resize(t + 1, 0); An[t] = (An[t] + P[i-1][j] * (G[j][z] * int64_t(G[i - 1 - j][y]) % p)) % p; } } } } cout << (k < int(An.size()) ? An[k] : 0) << '\n'; return 0; } |