#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; constexpr int M=1e7+7; int P[M]; int P2[M]; int K[M]; int K2[M]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long sigP, sigK, totalsig; int n, m, mod, res=0; cin >> n >> m >> mod; for(int j=1; j<=m; j++){ P[j] = m-j+1; K[j] = j; } for(int i=2; i<=n; i++){ sigP = sigK = totalsig = 0; for(int j=1; j<=m; j++){ //K sigP += P[j]; if(sigP>=mod) sigP-=mod; K2[j] = ((long long)j * sigP)%mod - totalsig; K2[j]%=mod; if(K2[j]<0) K2[j] += mod; sigK += K[j]; if(sigK>=mod) sigK-=mod; totalsig += sigK; if(totalsig>=mod) totalsig-=mod; } sigP = sigK = totalsig = 0; for(int j=m; j>=1; j--){ //P sigK += K[j]; if(sigK>=mod) sigK-=mod; P2[j] = ((long long)(m-j+1) * sigK)%mod - totalsig; P2[j]%=mod; if(P2[j]<0) P2[j] += mod; sigP += P[j]; if(sigP>=mod) sigP-=mod; totalsig += sigP; if(totalsig>=mod) totalsig-=mod; } for(int j=1; j<=m; j++){ P[j] = P2[j]; P2[j] = 0; K[j] = K2[j]; K2[j] = 0; } } for(int i=1; i<=m; i++){ res += P[i]; if(res>=mod) res-=mod; } cout << res; 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 | #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; constexpr int M=1e7+7; int P[M]; int P2[M]; int K[M]; int K2[M]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long sigP, sigK, totalsig; int n, m, mod, res=0; cin >> n >> m >> mod; for(int j=1; j<=m; j++){ P[j] = m-j+1; K[j] = j; } for(int i=2; i<=n; i++){ sigP = sigK = totalsig = 0; for(int j=1; j<=m; j++){ //K sigP += P[j]; if(sigP>=mod) sigP-=mod; K2[j] = ((long long)j * sigP)%mod - totalsig; K2[j]%=mod; if(K2[j]<0) K2[j] += mod; sigK += K[j]; if(sigK>=mod) sigK-=mod; totalsig += sigK; if(totalsig>=mod) totalsig-=mod; } sigP = sigK = totalsig = 0; for(int j=m; j>=1; j--){ //P sigK += K[j]; if(sigK>=mod) sigK-=mod; P2[j] = ((long long)(m-j+1) * sigK)%mod - totalsig; P2[j]%=mod; if(P2[j]<0) P2[j] += mod; sigP += P[j]; if(sigP>=mod) sigP-=mod; totalsig += sigP; if(totalsig>=mod) totalsig-=mod; } for(int j=1; j<=m; j++){ P[j] = P2[j]; P2[j] = 0; K[j] = K2[j]; K2[j] = 0; } } for(int i=1; i<=m; i++){ res += P[i]; if(res>=mod) res-=mod; } cout << res; return 0; } |