#include <bits/stdc++.h> using namespace std; //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin(), (x).end() #define ZAPS {int t; scanf("%i", &t); while(t--) solve();} #define make_unique(x) (x).erase( unique(all(x)), (x).end()) template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } lld dp[2][10000001]; int32_t main(void){ int n, m; lld p; scanf("%d%d%lld", &n, &m, &p); For(i, 1, m + 1) dp[0][i] = i; For(i, 1, n){ int pos = (i & 1); int prv = pos ^ 1; lld minusuj = 0; lld sufuj = 0; lld prefuj = 0; For(h, 1, m + 1) (minusuj += dp[prv][h]) %= p, (sufuj += dp[prv][h]) %= p; // cout<<sufuj<<endl; For(h, 1, m + 1){ (minusuj += (p - dp[prv][m - h + 1])) %= p; // cout<<minusuj<<" "<<sufuj<<" "<<prefuj<<endl; dp[pos][h] = (lld)h * (sufuj - minusuj + p) + prefuj; dp[pos][h] %= p; (sufuj += p - dp[prv][h]) %= p; (prefuj += (lld)(h) * (lld)(dp[prv][h])) %= p; // cout<<dp[pos][h]<<endl; } // puts(""); } lld wyn = 0; For(i, 1, m + 1) (wyn += dp[(n & 1) ^ 1][i]) %= p; printf("%lld", wyn); }
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 | #include <bits/stdc++.h> using namespace std; //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin(), (x).end() #define ZAPS {int t; scanf("%i", &t); while(t--) solve();} #define make_unique(x) (x).erase( unique(all(x)), (x).end()) template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } lld dp[2][10000001]; int32_t main(void){ int n, m; lld p; scanf("%d%d%lld", &n, &m, &p); For(i, 1, m + 1) dp[0][i] = i; For(i, 1, n){ int pos = (i & 1); int prv = pos ^ 1; lld minusuj = 0; lld sufuj = 0; lld prefuj = 0; For(h, 1, m + 1) (minusuj += dp[prv][h]) %= p, (sufuj += dp[prv][h]) %= p; // cout<<sufuj<<endl; For(h, 1, m + 1){ (minusuj += (p - dp[prv][m - h + 1])) %= p; // cout<<minusuj<<" "<<sufuj<<" "<<prefuj<<endl; dp[pos][h] = (lld)h * (sufuj - minusuj + p) + prefuj; dp[pos][h] %= p; (sufuj += p - dp[prv][h]) %= p; (prefuj += (lld)(h) * (lld)(dp[prv][h])) %= p; // cout<<dp[pos][h]<<endl; } // puts(""); } lld wyn = 0; For(i, 1, m + 1) (wyn += dp[(n & 1) ^ 1][i]) %= p; printf("%lld", wyn); } |