#include <bits/stdc++.h> using namespace std; using ll = long long; void solve(int n, int m, int p) { vector<vector<int>> dp(m + 2, vector<int>(m + 2)); vector<vector<ll>> tmp(m + 2, vector<ll>(m + 2)); dp[1][m] = 1; for (int k = 0; k <= n; k++) { for (int i = 1; i <= m; i++) { for (int j = m; j >= 1; j--) { tmp[i][j] = dp[i][j] + tmp[i - 1][j] + tmp[i][j + 1] - tmp[i - 1][j + 1]; } } for (int i = 1; i <= m; i++) { for (int j = i; j <= m; j++) { dp[i][j] = tmp[j][i] % p; } } } cout << dp[1][m] << "\n"; } int main() { ios_base::sync_with_stdio(false); int n, m, p; cin >> n >> m >> p; solve(n, m, p); }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; void solve(int n, int m, int p) { vector<vector<int>> dp(m + 2, vector<int>(m + 2)); vector<vector<ll>> tmp(m + 2, vector<ll>(m + 2)); dp[1][m] = 1; for (int k = 0; k <= n; k++) { for (int i = 1; i <= m; i++) { for (int j = m; j >= 1; j--) { tmp[i][j] = dp[i][j] + tmp[i - 1][j] + tmp[i][j + 1] - tmp[i - 1][j + 1]; } } for (int i = 1; i <= m; i++) { for (int j = i; j <= m; j++) { dp[i][j] = tmp[j][i] % p; } } } cout << dp[1][m] << "\n"; } int main() { ios_base::sync_with_stdio(false); int n, m, p; cin >> n >> m >> p; solve(n, m, p); } |