#include <iostream> #include <queue> #include <climits> using namespace std; int n, m; long long p; long long* Dp; int f(int i, int j, int k) { return i * (m+1) * (m+1) + j * (m+1) + k; } void init() { cin >> n >> m >> p; Dp = new long long[2*(m+7)*(m+7)]; for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { Dp[f(0, i, j)] = 1; } } for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { Dp[f(1, i, j)] = 0; } } } void solve() { int x = 1; for (int i=2; i<=n; i++) { for (int j=0; j<min(2, m); j++) { for (int k=1; k<=m-j; k++) { Dp[f(x, k, j+k)] = 0; for (int l=1; l<=j+k; l++) { for (int r=max(l, k); r<=j+k; r++) { Dp[f(x, k, j+k)] = (Dp[f(x, k, j+k)] + Dp[f(1-x, l, r)]) % p; } } for (int l=1; l<=j+k; l++) { for (int r=j+k+1; r<=m; r++) { Dp[f(x, k, j+k)] = (Dp[f(x, k, j+k)] + Dp[f(1-x, l, r)]) % p; } } } } for (int j=2; j<=m; j++) { for (int k=1; k<=m-j; k++) { Dp[f(x, k, j+k)] = (Dp[f(x, k+1, j+k)] + Dp[f(x, k, j+k-1)] - Dp[f(x, k+1, j+k-1)] + p) % p; } } x = 1 - x; } long long sum = 0; for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { sum = (sum + Dp[f(1-x, i, j)]) % p; } } cout << sum << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); init(); solve(); 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 | #include <iostream> #include <queue> #include <climits> using namespace std; int n, m; long long p; long long* Dp; int f(int i, int j, int k) { return i * (m+1) * (m+1) + j * (m+1) + k; } void init() { cin >> n >> m >> p; Dp = new long long[2*(m+7)*(m+7)]; for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { Dp[f(0, i, j)] = 1; } } for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { Dp[f(1, i, j)] = 0; } } } void solve() { int x = 1; for (int i=2; i<=n; i++) { for (int j=0; j<min(2, m); j++) { for (int k=1; k<=m-j; k++) { Dp[f(x, k, j+k)] = 0; for (int l=1; l<=j+k; l++) { for (int r=max(l, k); r<=j+k; r++) { Dp[f(x, k, j+k)] = (Dp[f(x, k, j+k)] + Dp[f(1-x, l, r)]) % p; } } for (int l=1; l<=j+k; l++) { for (int r=j+k+1; r<=m; r++) { Dp[f(x, k, j+k)] = (Dp[f(x, k, j+k)] + Dp[f(1-x, l, r)]) % p; } } } } for (int j=2; j<=m; j++) { for (int k=1; k<=m-j; k++) { Dp[f(x, k, j+k)] = (Dp[f(x, k+1, j+k)] + Dp[f(x, k, j+k-1)] - Dp[f(x, k+1, j+k-1)] + p) % p; } } x = 1 - x; } long long sum = 0; for (int i=1; i<=m; i++) { for (int j=i; j<=m; j++) { sum = (sum + Dp[f(1-x, i, j)]) % p; } } cout << sum << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); init(); solve(); return 0; } |