#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; ll DP[2][10000007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,m,ans=0; cin>>n>>m>>mod; for(ll i=1;i<=m;i++) DP[1][i]=m-i+1; for(ll i=2;i<=n;i++) { ll cnt=0,add=0,act=0; for(ll j=1;j<m;j++) { cnt=(cnt+DP[(i-1)%2][j])%mod; cnt=((cnt-DP[(i-1)%2][m-j+1])+mod)%mod; } for(ll j=m;j>0;j--) { add=(DP[(i-1)%2][j]*(m-j+1)+add)%mod; DP[i%2][j]=(add+cnt*(m-j+1))%mod; cnt=(cnt+DP[(i-1)%2][m-j+2])%mod; cnt=((cnt-DP[(i-1)%2][j-1])+mod)%mod; } } for(ll i=1;i<=m;i++) ans=(ans+DP[n%2][i])%mod; cout<<ans; 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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; ll DP[2][10000007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,m,ans=0; cin>>n>>m>>mod; for(ll i=1;i<=m;i++) DP[1][i]=m-i+1; for(ll i=2;i<=n;i++) { ll cnt=0,add=0,act=0; for(ll j=1;j<m;j++) { cnt=(cnt+DP[(i-1)%2][j])%mod; cnt=((cnt-DP[(i-1)%2][m-j+1])+mod)%mod; } for(ll j=m;j>0;j--) { add=(DP[(i-1)%2][j]*(m-j+1)+add)%mod; DP[i%2][j]=(add+cnt*(m-j+1))%mod; cnt=(cnt+DP[(i-1)%2][m-j+2])%mod; cnt=((cnt-DP[(i-1)%2][j-1])+mod)%mod; } } for(ll i=1;i<=m;i++) ans=(ans+DP[n%2][i])%mod; cout<<ans; return 0; } |