#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define ll LL #define st f #define nd s #define pb PB #define mp MP #define eb emplace_back #define siz(c) SIZ(c) const int inf = 1e9+7; const ll INF = 1e18L+7; #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #ifndef LOCAL #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #else #include</Users/dawid/algo/mac/ordered_set> #endif const int N = 1e7 + 7; int n, m, mod; int dp[2][N][2]; int pref[2][N][2]; int pref_mul[2][N][2]; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; else if (x <= -mod) x += mod; } inline int mul(int x, int y) { return (ll) x * y % mod; } void add_pref(int arr[][2], int l, int r, int b, int val) { add(arr[l][b], val); add(arr[r + 1][b], -val); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> mod; if (n == 1) { cout << ((ll) m * (m + 1) / 2) % mod << endl; return 0; } for (int i = 1; i <= m; ++i) { dp[1][i][1] = (m - i + 1) % mod; dp[1][i][0] = (ll) (m - i + 1) * (i - 1) % mod; } for (int i = 1; i < n; ++i) { int dim = (i & 1); for (int x = 1; x <= m; ++x) { for (int b = 0; b < 2; ++b) { dp[dim ^ 1][x][b] = 0; pref[dim ^ 1][x][b] = 0; pref_mul[dim ^ 1][x][b] = 0; } } for (int x = 1; x <= m; ++x) { add_pref(pref[dim ^ 1], 1, x, 1, mul(dp[dim][x][1], (m - x + 1))); add_pref(pref[dim ^ 1], 1, x, 0, mul(-dp[dim][x][1], (m - x + 1))); add_pref(pref_mul[dim ^ 1], 1, x, 0, mul(dp[dim][x][1], (m - x + 1))); add_pref(pref[dim ^ 1], x + 1, m, 0, mul(mul(dp[dim][x][1], x), (m + 1))); add_pref(pref_mul[dim ^ 1], x + 1, m, 0, mul(-dp[dim][x][1], x)); add(dp[dim ^ 1][x][1], mul(dp[dim][x][0], (m - x + 1))); add_pref(pref[dim ^ 1], x + 1, m, 0, mul(dp[dim][x][0], (m + 1))); add_pref(pref_mul[dim ^ 1], x + 1, m, 0, -dp[dim][x][0] % mod); } for (int x = 1; x <= m; ++x) { for (int b = 0; b < 2; ++b) { add(pref[dim ^ 1][x][b], pref[dim ^ 1][x - 1][b]); add(pref_mul[dim ^ 1][x][b], pref_mul[dim ^ 1][x - 1][b]); add(dp[dim ^ 1][x][b], pref[dim ^ 1][x][b]); add(dp[dim ^ 1][x][b], mul(x, pref_mul[dim ^ 1][x][b])); } } } int ans = 0; for (int i = 1; i <= m; ++i) { add(ans, mul(dp[(n - 1) & 1][i][0], (m - i + 1))); add(ans, mul(mul(dp[(n - 1) & 1][i][1], (m - i + 1)), i)); } ans %= mod; ans += mod; ans %= mod; cout << ans << endl; }
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define endl '\n' #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define ll LL #define st f #define nd s #define pb PB #define mp MP #define eb emplace_back #define siz(c) SIZ(c) const int inf = 1e9+7; const ll INF = 1e18L+7; #define sim template<class n sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} sim, class s> void mini(n &p, s y) {if(p>y) p = y;} sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define I(x) #x " =", (x), " " #ifndef LOCAL #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #else #include</Users/dawid/algo/mac/ordered_set> #endif const int N = 1e7 + 7; int n, m, mod; int dp[2][N][2]; int pref[2][N][2]; int pref_mul[2][N][2]; inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; else if (x <= -mod) x += mod; } inline int mul(int x, int y) { return (ll) x * y % mod; } void add_pref(int arr[][2], int l, int r, int b, int val) { add(arr[l][b], val); add(arr[r + 1][b], -val); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> mod; if (n == 1) { cout << ((ll) m * (m + 1) / 2) % mod << endl; return 0; } for (int i = 1; i <= m; ++i) { dp[1][i][1] = (m - i + 1) % mod; dp[1][i][0] = (ll) (m - i + 1) * (i - 1) % mod; } for (int i = 1; i < n; ++i) { int dim = (i & 1); for (int x = 1; x <= m; ++x) { for (int b = 0; b < 2; ++b) { dp[dim ^ 1][x][b] = 0; pref[dim ^ 1][x][b] = 0; pref_mul[dim ^ 1][x][b] = 0; } } for (int x = 1; x <= m; ++x) { add_pref(pref[dim ^ 1], 1, x, 1, mul(dp[dim][x][1], (m - x + 1))); add_pref(pref[dim ^ 1], 1, x, 0, mul(-dp[dim][x][1], (m - x + 1))); add_pref(pref_mul[dim ^ 1], 1, x, 0, mul(dp[dim][x][1], (m - x + 1))); add_pref(pref[dim ^ 1], x + 1, m, 0, mul(mul(dp[dim][x][1], x), (m + 1))); add_pref(pref_mul[dim ^ 1], x + 1, m, 0, mul(-dp[dim][x][1], x)); add(dp[dim ^ 1][x][1], mul(dp[dim][x][0], (m - x + 1))); add_pref(pref[dim ^ 1], x + 1, m, 0, mul(dp[dim][x][0], (m + 1))); add_pref(pref_mul[dim ^ 1], x + 1, m, 0, -dp[dim][x][0] % mod); } for (int x = 1; x <= m; ++x) { for (int b = 0; b < 2; ++b) { add(pref[dim ^ 1][x][b], pref[dim ^ 1][x - 1][b]); add(pref_mul[dim ^ 1][x][b], pref_mul[dim ^ 1][x - 1][b]); add(dp[dim ^ 1][x][b], pref[dim ^ 1][x][b]); add(dp[dim ^ 1][x][b], mul(x, pref_mul[dim ^ 1][x][b])); } } } int ans = 0; for (int i = 1; i <= m; ++i) { add(ans, mul(dp[(n - 1) & 1][i][0], (m - i + 1))); add(ans, mul(mul(dp[(n - 1) & 1][i][1], (m - i + 1)), i)); } ans %= mod; ans += mod; ans %= mod; cout << ans << endl; } |