#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, m, p; cin >> n >> m >> p; vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(m+1, vector<ll>(m+1))); fill(dp[0].begin(), dp[0].end(), vector<ll>(m+1, 1)); for (int _ = 1; _ < n; ++_) { for (int i = 1; i <= m; ++i) { for (int j = i; j <= m; ++j) { for (int i2 = 1; i2 <= j; ++i2) { for (int j2 = max(i, i2); j2 <= m; ++j2) { dp[1][i][j] += dp[0][i2][j2]; dp[1][i][j] %= p; } } } } swap(dp[0], dp[1]); fill(dp[1].begin(), dp[1].end(), vector<ll>(m+1, 0)); } ll s = 0; for (int i = 1; i <= m; ++i) { for (int j = i; j <= m; ++j) { s += dp[0][i][j]; } s %= p; } cout << s << '\n'; } // 3 2 1000000007 // 6 9 813443923
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n, m, p; cin >> n >> m >> p; vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(m+1, vector<ll>(m+1))); fill(dp[0].begin(), dp[0].end(), vector<ll>(m+1, 1)); for (int _ = 1; _ < n; ++_) { for (int i = 1; i <= m; ++i) { for (int j = i; j <= m; ++j) { for (int i2 = 1; i2 <= j; ++i2) { for (int j2 = max(i, i2); j2 <= m; ++j2) { dp[1][i][j] += dp[0][i2][j2]; dp[1][i][j] %= p; } } } } swap(dp[0], dp[1]); fill(dp[1].begin(), dp[1].end(), vector<ll>(m+1, 0)); } ll s = 0; for (int i = 1; i <= m; ++i) { for (int j = i; j <= m; ++j) { s += dp[0][i][j]; } s %= p; } cout << s << '\n'; } // 3 2 1000000007 // 6 9 813443923 |