#define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 5e6+69, INF = 2e9+6969; ll n,m,mod; ll dp[nax],pp[nax],Dp[nax]; ll Pow(ll a, ll b) { ll w = 1; while(b) { if(b&1) w = (w*a) % mod; a = (a*a) % mod; b >>= 1; } return w; } int main() { scanf("%lld%lld%lld", &n, &m, &mod); if(n == 1) { ll ans = m*(m+1)/2; ans %= mod; printf("%lld\n", ans); return 0; } ll sum = 0; for(int i=1;i<=m;++i) dp[i] = i; for(int i=1;i<=m;++i) sum = (sum + dp[i]) % mod; for(int i=1;i<=m;++i) dp[i] = (dp[i-1] + dp[i]) % mod; for(int i=1;i<=m;++i) Dp[i] = (Dp[i-1] + dp[i]) % mod; for(int i=1;i<n;++i) { for(int j=1;j<=m;++j) { pp[j] = (sum * j) % mod; } for(int p=1;p<=m;++p) { ll tmp = (dp[p-1] * (m-p+1) + Dp[m-p])%mod; pp[m-p+1] -= tmp; if(pp[m-p+1] < 0) pp[m-p+1] += mod; } for(int j=1;j<=m;++j) dp[j] = (pp[j] + dp[j-1])%mod; for(int j=1;j<=m;++j) Dp[j] = (Dp[j-1] + dp[j]) % mod; sum = dp[m]; } printf("%lld\n", sum); 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 | #define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 5e6+69, INF = 2e9+6969; ll n,m,mod; ll dp[nax],pp[nax],Dp[nax]; ll Pow(ll a, ll b) { ll w = 1; while(b) { if(b&1) w = (w*a) % mod; a = (a*a) % mod; b >>= 1; } return w; } int main() { scanf("%lld%lld%lld", &n, &m, &mod); if(n == 1) { ll ans = m*(m+1)/2; ans %= mod; printf("%lld\n", ans); return 0; } ll sum = 0; for(int i=1;i<=m;++i) dp[i] = i; for(int i=1;i<=m;++i) sum = (sum + dp[i]) % mod; for(int i=1;i<=m;++i) dp[i] = (dp[i-1] + dp[i]) % mod; for(int i=1;i<=m;++i) Dp[i] = (Dp[i-1] + dp[i]) % mod; for(int i=1;i<n;++i) { for(int j=1;j<=m;++j) { pp[j] = (sum * j) % mod; } for(int p=1;p<=m;++p) { ll tmp = (dp[p-1] * (m-p+1) + Dp[m-p])%mod; pp[m-p+1] -= tmp; if(pp[m-p+1] < 0) pp[m-p+1] += mod; } for(int j=1;j<=m;++j) dp[j] = (pp[j] + dp[j-1])%mod; for(int j=1;j<=m;++j) Dp[j] = (Dp[j-1] + dp[j]) % mod; sum = dp[m]; } printf("%lld\n", sum); return 0; } |