#include<bits/stdc++.h> using namespace std; using LL = long long; using D = double; #define f1 first #define f2 second #define randint(a, b) uniform_int_distribution<int>{a, b}(gen) #ifdef LOC void OUT() {cout << '\n';} template<class H, class ... T> void OUT(H h, T ... t) { cout << h << ' '; OUT(t...); } #define P(...) cout << "[" << #__VA_ARGS__ << "] ", OUT(__VA_ARGS__) #else #define P(...) #define OUT(...) #endif int n, m, p; vector<LL> dp[2]; LL add(LL a, LL b) { a += b; if(a >= p) a -= p; return a; } LL subtract(LL a, LL b) { a -= b; if(a < 0) a += p; return a; } void oper() { LL val = 0; for(LL i = 0; i < m; ++i) { val = add(val, (dp[0][i] * (i + 1)) % p); } LL sum = 0; LL revSum = 0; for(LL i = m - 1; i >= 0; --i) { dp[1][i] = subtract(val, revSum * (i + 1) % p); sum = add(dp[0][i], sum); revSum = add(revSum, dp[0][m - 1 - i]); val = subtract(val, sum); } #ifdef LOC for(int i = 0; i < m; ++i) cout << dp[1][i] << ' '; cout << '\n'; #endif swap(dp[0], dp[1]); } int main(int, char ** /*args*/) { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> p; dp[0].resize(m, 0); dp[1].resize(m, 0); for(int i = 0; i < m; ++i) { dp[0][i] = i + 1; } while(--n) { oper(); } LL ans = 0; for(int i = 0; i < m; ++i) { ans = add(ans, dp[0][i]); } cout << ans % p << '\n'; 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include<bits/stdc++.h> using namespace std; using LL = long long; using D = double; #define f1 first #define f2 second #define randint(a, b) uniform_int_distribution<int>{a, b}(gen) #ifdef LOC void OUT() {cout << '\n';} template<class H, class ... T> void OUT(H h, T ... t) { cout << h << ' '; OUT(t...); } #define P(...) cout << "[" << #__VA_ARGS__ << "] ", OUT(__VA_ARGS__) #else #define P(...) #define OUT(...) #endif int n, m, p; vector<LL> dp[2]; LL add(LL a, LL b) { a += b; if(a >= p) a -= p; return a; } LL subtract(LL a, LL b) { a -= b; if(a < 0) a += p; return a; } void oper() { LL val = 0; for(LL i = 0; i < m; ++i) { val = add(val, (dp[0][i] * (i + 1)) % p); } LL sum = 0; LL revSum = 0; for(LL i = m - 1; i >= 0; --i) { dp[1][i] = subtract(val, revSum * (i + 1) % p); sum = add(dp[0][i], sum); revSum = add(revSum, dp[0][m - 1 - i]); val = subtract(val, sum); } #ifdef LOC for(int i = 0; i < m; ++i) cout << dp[1][i] << ' '; cout << '\n'; #endif swap(dp[0], dp[1]); } int main(int, char ** /*args*/) { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> p; dp[0].resize(m, 0); dp[1].resize(m, 0); for(int i = 0; i < m; ++i) { dp[0][i] = i + 1; } while(--n) { oper(); } LL ans = 0; for(int i = 0; i < m; ++i) { ans = add(ans, dp[0][i]); } cout << ans % p << '\n'; return 0; } |