#include <bits/stdc++.h> #define int long long using namespace std; const int INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000; int p; int addd (int a, int b) { return ((a + b) % p + p) % p; } int multi (int a, int b) { return ((a * b) % p + p) % p; } signed main() { int n, m; cin >> n >> m >> p; vector <vector <int>> dp(n, vector<int>(m)); vector <vector <int>> dp2(n, vector<int>(m)); for (int i = 0; i < m; i++) { dp[0][i] = i + 1; } for (int i = 1; i < m; i++) { dp[0][i] = addd(dp[0][i], dp[0][i - 1]); } dp2[0][0] = dp[0][0]; for (int i = 1; i < m; i++) { dp2[0][i] = addd(dp[0][i], dp[0][i - 1]); } for (int j = 0; j < m; j++) { cerr << dp[0][j] << " "; } cerr << endl; for (int j = 0; j < m; j++) { cerr << dp2[0][j] << " "; } cerr << endl; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = multi(dp[i - 1][m - 1], (j+1)); if (j < m - 1) { dp[i][j] = addd(dp[i][j], -1LL * multi((j+1), dp[i - 1][m - j - 2])); } if (j > 0) { dp[i][j] = addd(dp[i][j], -1LL * dp2[i - 1][j - 1]); } // if (j == m - 1) { // dp[i][j] = 0; // dp[i][j] = multi(dp[0][j], dp[i - 1][j]); // dp[i][j] = addd(dp[i][j], multi(-2LL, dp2[i - 1][j - 1])); // } } for (int j = 0; j < m; j++) { cerr << dp[i][j] << " "; } cerr << endl; for (int j = 1; j < m ; j++) { dp[i][j] = addd(dp[i][j], dp[i][j - 1]); } dp2[i][0] = dp[i][0]; for (int j = 1; j < m; j++) { dp2[i][j] = addd(dp[i][j], dp[i][j - 1]); } } cout << dp[n - 1][m - 1] << endl; 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 | #include <bits/stdc++.h> #define int long long using namespace std; const int INF = 1000LL * 1000 * 1000 * 1000 * 1000 * 1000; int p; int addd (int a, int b) { return ((a + b) % p + p) % p; } int multi (int a, int b) { return ((a * b) % p + p) % p; } signed main() { int n, m; cin >> n >> m >> p; vector <vector <int>> dp(n, vector<int>(m)); vector <vector <int>> dp2(n, vector<int>(m)); for (int i = 0; i < m; i++) { dp[0][i] = i + 1; } for (int i = 1; i < m; i++) { dp[0][i] = addd(dp[0][i], dp[0][i - 1]); } dp2[0][0] = dp[0][0]; for (int i = 1; i < m; i++) { dp2[0][i] = addd(dp[0][i], dp[0][i - 1]); } for (int j = 0; j < m; j++) { cerr << dp[0][j] << " "; } cerr << endl; for (int j = 0; j < m; j++) { cerr << dp2[0][j] << " "; } cerr << endl; for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = multi(dp[i - 1][m - 1], (j+1)); if (j < m - 1) { dp[i][j] = addd(dp[i][j], -1LL * multi((j+1), dp[i - 1][m - j - 2])); } if (j > 0) { dp[i][j] = addd(dp[i][j], -1LL * dp2[i - 1][j - 1]); } // if (j == m - 1) { // dp[i][j] = 0; // dp[i][j] = multi(dp[0][j], dp[i - 1][j]); // dp[i][j] = addd(dp[i][j], multi(-2LL, dp2[i - 1][j - 1])); // } } for (int j = 0; j < m; j++) { cerr << dp[i][j] << " "; } cerr << endl; for (int j = 1; j < m ; j++) { dp[i][j] = addd(dp[i][j], dp[i][j - 1]); } dp2[i][0] = dp[i][0]; for (int j = 1; j < m; j++) { dp2[i][j] = addd(dp[i][j], dp[i][j - 1]); } } cout << dp[n - 1][m - 1] << endl; return 0; } |