#include <bits/stdc++.h> using namespace std; void test_case() { int64_t n, m, p; cin >> n >> m >> p; vector<vector<int64_t>> arr(n+2, vector<int64_t>(m+2)); vector<int64_t> pre(m+2); for (int i = 1; i <= m; i++) { arr[n][i] = m-i+1; pre[i] = pre[i-1] + arr[n][i]; } for (int k = n-1; k >= 1; k--) { int64_t sum = 0; for (int i = 1; i <= m; i++) { sum += pre[i]; sum %= p; } for (int i = 1; i <= m; i++) { arr[k][i] = arr[k][i-1] + arr[k+1][i] - arr[k+1][m-i+2]; arr[k][i] %= p; } vector<int64_t> b(m+2); b[1] = sum; for (int i = 2; i <= m; i++) { b[i] = b[i-1] - (m-i+2) * arr[k][i-1] - (m-i+1) * arr[k+1][i] + (m-i+1)*arr[k][i]; b[i] %= p; } for (int i = 1; i <= m; i++) { arr[k][i] = b[i]; } for (int i = 1; i <= m; i++) { pre[i] = pre[i-1] + b[i]; pre[i] %= p; } } int64_t ans = 0; for (int i = 1; i <= m; i++) { ans = (ans + arr[1][i]) % p; } if (ans < 0) ans += p; cout << ans << '\n'; } void solve() { test_case(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); 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 | #include <bits/stdc++.h> using namespace std; void test_case() { int64_t n, m, p; cin >> n >> m >> p; vector<vector<int64_t>> arr(n+2, vector<int64_t>(m+2)); vector<int64_t> pre(m+2); for (int i = 1; i <= m; i++) { arr[n][i] = m-i+1; pre[i] = pre[i-1] + arr[n][i]; } for (int k = n-1; k >= 1; k--) { int64_t sum = 0; for (int i = 1; i <= m; i++) { sum += pre[i]; sum %= p; } for (int i = 1; i <= m; i++) { arr[k][i] = arr[k][i-1] + arr[k+1][i] - arr[k+1][m-i+2]; arr[k][i] %= p; } vector<int64_t> b(m+2); b[1] = sum; for (int i = 2; i <= m; i++) { b[i] = b[i-1] - (m-i+2) * arr[k][i-1] - (m-i+1) * arr[k+1][i] + (m-i+1)*arr[k][i]; b[i] %= p; } for (int i = 1; i <= m; i++) { arr[k][i] = b[i]; } for (int i = 1; i <= m; i++) { pre[i] = pre[i-1] + b[i]; pre[i] %= p; } } int64_t ans = 0; for (int i = 1; i <= m; i++) { ans = (ans + arr[1][i]) % p; } if (ans < 0) ans += p; cout << ans << '\n'; } void solve() { test_case(); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |