#include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int maxM = 1e7 + 10; int n, m, mod; int D[2][maxM]; int U[2][maxM]; int PD[2][maxM]; int SU[2][maxM]; int PPD[2][maxM]; int SSU[2][maxM]; // vector<Vi> D, U, PD, SU, PPD, SSU; // void resize() { // D.resize(2, Vi(m+2)); // U.resize(2, Vi(m+2)); // PD.resize(2, Vi(m+2)); // SU.resize(2, Vi(m+2)); // PPD.resize(2, Vi(m+2)); // SSU.resize(2, Vi(m+2)); // } void updateCol(int k) { for (int i = 1; i <= m+1; i++) { PD[k&1][i] = (D[k&1][i] + PD[k&1][i-1]) % mod; PPD[k&1][i] = (PD[k&1][i] + PPD[k&1][i-1]) % mod; } for (int i = m; i >= 0; i--) { SU[k&1][i] = (U[k&1][i] + SU[k&1][i+1]) % mod; SSU[k&1][i] = (SU[k&1][i] + SSU[k&1][i+1]) % mod; } } int getD(int k, int j) { ll res = j * ((ll)PD[(k-1)&1][m] - SU[(k-1)&1][j+1]) - PPD[(k-1)&1][j-1]; return int((res % mod + mod) % mod); } int getU(int k, int i) { ll res = (m-i+1) * ((ll)PD[(k-1)&1][m] - PD[(k-1)&1][i-1]) - SSU[(k-1)&1][i+1]; return int((res % mod + mod) % mod); } void solve() { cin >> n >> m >> mod; for (int i = 1; i <= m; i++) { D[1][i] = i % mod; U[1][i] = (m - i + 1) % mod; } updateCol(1); for (int k = 2; k <= n; k++) { for (int i = 1; i <= m; i++) { D[k&1][i] = getD(k, i); U[k&1][i] = getU(k, i); } updateCol(k); } cout << PD[n&1][m] << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; using Pii = pair<int,int>; using Vi = vector<int>; #define mp make_pair #define pb push_back #define x first #define y second #define rep(i,b,e) for (int i = (b); i < (e); i++) #define each(a,x) for (auto& a : (x)) #define all(x) (x).begin(),(x).end() #define sz(x) int((x).size()) #define endl '\n' const int maxM = 1e7 + 10; int n, m, mod; int D[2][maxM]; int U[2][maxM]; int PD[2][maxM]; int SU[2][maxM]; int PPD[2][maxM]; int SSU[2][maxM]; // vector<Vi> D, U, PD, SU, PPD, SSU; // void resize() { // D.resize(2, Vi(m+2)); // U.resize(2, Vi(m+2)); // PD.resize(2, Vi(m+2)); // SU.resize(2, Vi(m+2)); // PPD.resize(2, Vi(m+2)); // SSU.resize(2, Vi(m+2)); // } void updateCol(int k) { for (int i = 1; i <= m+1; i++) { PD[k&1][i] = (D[k&1][i] + PD[k&1][i-1]) % mod; PPD[k&1][i] = (PD[k&1][i] + PPD[k&1][i-1]) % mod; } for (int i = m; i >= 0; i--) { SU[k&1][i] = (U[k&1][i] + SU[k&1][i+1]) % mod; SSU[k&1][i] = (SU[k&1][i] + SSU[k&1][i+1]) % mod; } } int getD(int k, int j) { ll res = j * ((ll)PD[(k-1)&1][m] - SU[(k-1)&1][j+1]) - PPD[(k-1)&1][j-1]; return int((res % mod + mod) % mod); } int getU(int k, int i) { ll res = (m-i+1) * ((ll)PD[(k-1)&1][m] - PD[(k-1)&1][i-1]) - SSU[(k-1)&1][i+1]; return int((res % mod + mod) % mod); } void solve() { cin >> n >> m >> mod; for (int i = 1; i <= m; i++) { D[1][i] = i % mod; U[1][i] = (m - i + 1) % mod; } updateCol(1); for (int k = 2; k <= n; k++) { for (int i = 1; i <= m; i++) { D[k&1][i] = getD(k, i); U[k&1][i] = getU(k, i); } updateCol(k); } cout << PD[n&1][m] << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; for (int i = 0; i < t; i++) { solve(); } } |