#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long LL; int n, m, p; int suma(vector<int> &w, int aw, int bw) { aw=max(aw, 0); bw=min(bw, (int) w.size()-1); LL wyn=0; for (int i=aw; i<=bw; ++i) wyn+=w[i]; return wyn%p; } int suma(vector<int> &w) { return suma(w, 0, w.size()-1); } int main() { cin>>n>>m>>p; vector<int> wg(m), wd(m), nwg(m), nwd(m); wg[0]=1; for (int i=1; i<m; ++i) wg[i]=wg[i-1]+1; wd[m-1]=1; for (int i=m-2; 0<=i; --i) wd[i]=wd[i+1]+1; for (int nn=1; nn<n; ++nn) { cerr<<suma(wg)<<endl; LL sp=suma(wg); LL sg1=sp-wd[0]; for (int i=0; i<m; ++i) { LL g=sp*(i+1); for (int j=i; 0<=j; --j) { g-=suma(wd, i+1, m-1); g-=suma(wg, 0, j-1); } LL d=sp*(m-i); for (int j=i; j<m; ++j) { d-=suma(wd, j+1, m-1); d-=suma(wg, 0, i-1); } while (d<0) d+=p; while (g<0) g+=p; nwd[i]=d%p; nwg[i]=g%p; } wg.swap(nwg); wd.swap(nwd); } cout<<suma(wg)<<endl; 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long LL; int n, m, p; int suma(vector<int> &w, int aw, int bw) { aw=max(aw, 0); bw=min(bw, (int) w.size()-1); LL wyn=0; for (int i=aw; i<=bw; ++i) wyn+=w[i]; return wyn%p; } int suma(vector<int> &w) { return suma(w, 0, w.size()-1); } int main() { cin>>n>>m>>p; vector<int> wg(m), wd(m), nwg(m), nwd(m); wg[0]=1; for (int i=1; i<m; ++i) wg[i]=wg[i-1]+1; wd[m-1]=1; for (int i=m-2; 0<=i; --i) wd[i]=wd[i+1]+1; for (int nn=1; nn<n; ++nn) { cerr<<suma(wg)<<endl; LL sp=suma(wg); LL sg1=sp-wd[0]; for (int i=0; i<m; ++i) { LL g=sp*(i+1); for (int j=i; 0<=j; --j) { g-=suma(wd, i+1, m-1); g-=suma(wg, 0, j-1); } LL d=sp*(m-i); for (int j=i; j<m; ++j) { d-=suma(wd, j+1, m-1); d-=suma(wg, 0, i-1); } while (d<0) d+=p; while (g<0) g+=p; nwd[i]=d%p; nwg[i]=g%p; } wg.swap(nwg); wd.swap(nwd); } cout<<suma(wg)<<endl; return 0; } |