#include <bits/stdc++.h> using namespace std; #define mk make_pair #define fi first #define sz(x) x.size() #define se second #define pb push_back #define VAR(v) #v << " = " << v << " " #define debug if(0) #define M_PI 3.14159265358979323846 typedef complex<long double> C; typedef long long ll; typedef pair<int, int> ii; typedef vector<ll> Matrix; const int maxn = (int)1e6 + 9; const int INF = (int)1e9 + 7; int mod = (int)1e9 + 7, n, m; vector<vector<int>> pref, act, lst; ll get_sum(int a, int b, int c, int d){ return (ll(pref[c][d]) - ll(pref[a-1][d]) - ll(pref[c][b-1]) + ll(pref[a-1][b-1]) + ll(mod))%mod; } void printM(vector<vector<int>> V){ for(int i = 0; i < V.size(); i++) { for(int x : V[i]) cout<<x<<" "; cout<<endl; } } int ComputeDp() { pref.resize(m+1), act.resize(m+1), lst.resize(m+1); for(int i = 0; i <= m; i++) { pref[i].resize(m+1), act[i].resize(m+1), lst[i].resize(m+1); for(int j = 0; j <= m; j++) { pref[i][j] = act[i][j] = lst[i][j] = 0; if(i <= j) lst[i][j] = 1; } } for(int i = 1; i <= n; i++) { for(int i = 1; i <= m; i++) for(int j = 1; j <= m; j++) pref[i][j] = (ll(lst[i][j]) + ll(pref[i-1][j]) + ll(pref[i][j-1]) - ll(pref[i-1][j-1]) + ll(mod))%ll(mod), act[i][j] = 0; for(int a = 1; a <= m; a++) for(int b = a; b <= m; b++) act[a][b] = get_sum(1,a,b,m); debug{cout<<"PREF\n"; printM(pref);} debug{cout<<"ACT\n"; printM(act);} debug{cout<<"LST\n"; printM(lst);} swap(act, lst); } return pref[m][m]; } int main(int argc, char* argv[]) { ios::sync_with_stdio(false); debug; else cin.tie(NULL), cout.tie(NULL); cin>>n>>m>>mod; cout<<ComputeDp()<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; #define mk make_pair #define fi first #define sz(x) x.size() #define se second #define pb push_back #define VAR(v) #v << " = " << v << " " #define debug if(0) #define M_PI 3.14159265358979323846 typedef complex<long double> C; typedef long long ll; typedef pair<int, int> ii; typedef vector<ll> Matrix; const int maxn = (int)1e6 + 9; const int INF = (int)1e9 + 7; int mod = (int)1e9 + 7, n, m; vector<vector<int>> pref, act, lst; ll get_sum(int a, int b, int c, int d){ return (ll(pref[c][d]) - ll(pref[a-1][d]) - ll(pref[c][b-1]) + ll(pref[a-1][b-1]) + ll(mod))%mod; } void printM(vector<vector<int>> V){ for(int i = 0; i < V.size(); i++) { for(int x : V[i]) cout<<x<<" "; cout<<endl; } } int ComputeDp() { pref.resize(m+1), act.resize(m+1), lst.resize(m+1); for(int i = 0; i <= m; i++) { pref[i].resize(m+1), act[i].resize(m+1), lst[i].resize(m+1); for(int j = 0; j <= m; j++) { pref[i][j] = act[i][j] = lst[i][j] = 0; if(i <= j) lst[i][j] = 1; } } for(int i = 1; i <= n; i++) { for(int i = 1; i <= m; i++) for(int j = 1; j <= m; j++) pref[i][j] = (ll(lst[i][j]) + ll(pref[i-1][j]) + ll(pref[i][j-1]) - ll(pref[i-1][j-1]) + ll(mod))%ll(mod), act[i][j] = 0; for(int a = 1; a <= m; a++) for(int b = a; b <= m; b++) act[a][b] = get_sum(1,a,b,m); debug{cout<<"PREF\n"; printM(pref);} debug{cout<<"ACT\n"; printM(act);} debug{cout<<"LST\n"; printM(lst);} swap(act, lst); } return pref[m][m]; } int main(int argc, char* argv[]) { ios::sync_with_stdio(false); debug; else cin.tie(NULL), cout.tie(NULL); cin>>n>>m>>mod; cout<<ComputeDp()<<"\n"; return 0; } |