#include "bits/stdc++.h" using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates int mod; void add(int &a, int x) { a = (a + x >= mod ? a + x - mod : a + x); } void sub(int &a, int x) { a = (a - x < 0 ? a - x + mod : a - x); } int mult(int a, int b) { return LL(a) * b % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m >> mod; vector<int> dp(m+1), prev_dp(m+1), pref(m+1), prev_pref(m+1); FOR(i, 1, m) { if(i&1) dp[i] = mult(i, (i+1)/2); else dp[i] = mult(i/2, i+1); add(pref[i], pref[i-1]); add(pref[i], dp[i]); } FOR(j, 2, n) { swap(dp, prev_dp); swap(pref, prev_pref); FOR(i, 1, m) { dp[i] = mult(i, prev_dp.back()); sub(dp[i], mult(i, prev_dp[m-i])); sub(dp[i], prev_pref[i-1]); add(dp[i], dp[i-1]); pref[i] = 0; add(pref[i], pref[i-1]); add(pref[i], dp[i]); } debug(dp); } cout << dp.back() << '\n'; }
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 | #include "bits/stdc++.h" using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } // end of templates int mod; void add(int &a, int x) { a = (a + x >= mod ? a + x - mod : a + x); } void sub(int &a, int x) { a = (a - x < 0 ? a - x + mod : a - x); } int mult(int a, int b) { return LL(a) * b % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m >> mod; vector<int> dp(m+1), prev_dp(m+1), pref(m+1), prev_pref(m+1); FOR(i, 1, m) { if(i&1) dp[i] = mult(i, (i+1)/2); else dp[i] = mult(i/2, i+1); add(pref[i], pref[i-1]); add(pref[i], dp[i]); } FOR(j, 2, n) { swap(dp, prev_dp); swap(pref, prev_pref); FOR(i, 1, m) { dp[i] = mult(i, prev_dp.back()); sub(dp[i], mult(i, prev_dp[m-i])); sub(dp[i], prev_pref[i-1]); add(dp[i], dp[i-1]); pref[i] = 0; add(pref[i], pref[i-1]); add(pref[i], dp[i]); } debug(dp); } cout << dp.back() << '\n'; } |