#include <bits/stdc++.h> using namespace std; int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif int n, m, md; scanf("%d %d %d", &n, &m, &md); auto add = [&](int &x, int y) { x += y; if (x >= md) { x -= md; } }; auto mul = [&](int x, int y) { return (long long)x * y % md; }; int max_m = 0; while (1 << max_m < n) { ++max_m; } if (m > max_m) { puts("0"); return 0; } vector<vector<int>> binom(n, vector<int> (n)); for (int i = 0; i < n; ++i) { binom[i][0] = 1; for (int j = 1; j <= i; ++j) { add(binom[i][j], binom[i - 1][j]); add(binom[i][j], binom[i - 1][j - 1]); } } vector<vector<int>> g(n + 1, vector<int> (m + 1)); vector<vector<int>> sum_g(n + 1, vector<int> (m + 1)); g[0][0] = 1; for (int i = 0; i <= m; ++i) { sum_g[0][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int k = 1; k <= i; ++k) { add(g[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j], sum_g[i - k][j - 1]))); add(g[i][j], mul(binom[i - 1][k - 1], mul(sum_g[k - 1][j - 1], g[i - k][j]))); add(g[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j - 1], g[i - k][j - 1]))); } sum_g[i][j] = sum_g[i][j - 1]; add(sum_g[i][j], g[i][j]); } } vector<vector<int>> f(n + 1, vector<int> (m + 1)); vector<vector<int>> sum_f(n + 1, vector<int> (m + 1)); f[0][0] = 1; for (int i = 0; i <= m; ++i) { sum_f[0][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int k = 1; k <= i; ++k) { add(f[i][j], mul(binom[i - 1][k - 1], mul(sum_g[k - 1][j - 1], f[i - k][j]))); add(f[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j - 1], sum_f[i - k][j - 1]))); } sum_f[i][j] = sum_f[i][j - 1]; add(sum_f[i][j], f[i][j]); } } int answer = 0; for (int i = 1; i <= n; ++i) { add(answer, mul(binom[n - 1][i - 1], mul(f[i - 1][m], sum_f[n - i][m]))); add(answer, mul(binom[n - 1][i - 1], mul(sum_f[i - 1][m - 1], f[n - i][m]))); } printf("%d\n", answer); 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 | #include <bits/stdc++.h> using namespace std; int main() { #ifdef wxh010910 freopen("input.txt", "r", stdin); #endif int n, m, md; scanf("%d %d %d", &n, &m, &md); auto add = [&](int &x, int y) { x += y; if (x >= md) { x -= md; } }; auto mul = [&](int x, int y) { return (long long)x * y % md; }; int max_m = 0; while (1 << max_m < n) { ++max_m; } if (m > max_m) { puts("0"); return 0; } vector<vector<int>> binom(n, vector<int> (n)); for (int i = 0; i < n; ++i) { binom[i][0] = 1; for (int j = 1; j <= i; ++j) { add(binom[i][j], binom[i - 1][j]); add(binom[i][j], binom[i - 1][j - 1]); } } vector<vector<int>> g(n + 1, vector<int> (m + 1)); vector<vector<int>> sum_g(n + 1, vector<int> (m + 1)); g[0][0] = 1; for (int i = 0; i <= m; ++i) { sum_g[0][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int k = 1; k <= i; ++k) { add(g[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j], sum_g[i - k][j - 1]))); add(g[i][j], mul(binom[i - 1][k - 1], mul(sum_g[k - 1][j - 1], g[i - k][j]))); add(g[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j - 1], g[i - k][j - 1]))); } sum_g[i][j] = sum_g[i][j - 1]; add(sum_g[i][j], g[i][j]); } } vector<vector<int>> f(n + 1, vector<int> (m + 1)); vector<vector<int>> sum_f(n + 1, vector<int> (m + 1)); f[0][0] = 1; for (int i = 0; i <= m; ++i) { sum_f[0][i] = 1; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { for (int k = 1; k <= i; ++k) { add(f[i][j], mul(binom[i - 1][k - 1], mul(sum_g[k - 1][j - 1], f[i - k][j]))); add(f[i][j], mul(binom[i - 1][k - 1], mul(g[k - 1][j - 1], sum_f[i - k][j - 1]))); } sum_f[i][j] = sum_f[i][j - 1]; add(sum_f[i][j], f[i][j]); } } int answer = 0; for (int i = 1; i <= n; ++i) { add(answer, mul(binom[n - 1][i - 1], mul(f[i - 1][m], sum_f[n - i][m]))); add(answer, mul(binom[n - 1][i - 1], mul(sum_f[i - 1][m - 1], f[n - i][m]))); } printf("%d\n", answer); return 0; } |