#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second
#define PL 1
#define MI 2
const int NN = 10100100;
int n,m,p;
int dpc[3][NN], dpp[3][NN];
int sdp[3][NN];
int run_nm() {
FORI(i,m) dpp[PL][i] = 1;
FORI(k,n) {
//cerr << "n = " << k << "\n";
FORI(j,m-1) sdp[PL][j+1] = (1LL * dpp[0][j] * j + dpp[PL][j] + sdp[PL][j]) % p;
REPD(j,m,2) sdp[MI][j-1] = (1LL * dpp[0][j] * (m+1-j) + dpp[MI][j] + sdp[MI][j]) % p;
FORI(j,m) {
dpc[MI][j] = 1LL * j * sdp[MI][j] % p;
dpc[ 0][j] = (1LL * dpp[0][j] * j % p * (m+1-j) + 1LL * dpp[MI][j] * j + 1LL * dpp[PL][j] * (m+1-j)) % p;
dpc[PL][j] = 1LL * (m+1-j) * sdp[PL][j] % p;
}
//FORI(i,m) {
// cerr << i << " : " << dpc[MI][i] << "\t" << dpc[0][i] << "\t" << dpc[PL][i] << "\n";
//}
//cerr << "\n";
FOR(c,3) FORI(i,m) {
dpp[c][i] = dpc[c][i];
dpc[c][i] = 0;
}
}
int res = 0;
FORI(i,m) {
res += dpp[0][i];
if (res >= p) res -= p;
}
return res;
}
int main() {
scanf("%d%d%d", &n, &m, &p);
printf("%d\n", run_nm());
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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second #define PL 1 #define MI 2 const int NN = 10100100; int n,m,p; int dpc[3][NN], dpp[3][NN]; int sdp[3][NN]; int run_nm() { FORI(i,m) dpp[PL][i] = 1; FORI(k,n) { //cerr << "n = " << k << "\n"; FORI(j,m-1) sdp[PL][j+1] = (1LL * dpp[0][j] * j + dpp[PL][j] + sdp[PL][j]) % p; REPD(j,m,2) sdp[MI][j-1] = (1LL * dpp[0][j] * (m+1-j) + dpp[MI][j] + sdp[MI][j]) % p; FORI(j,m) { dpc[MI][j] = 1LL * j * sdp[MI][j] % p; dpc[ 0][j] = (1LL * dpp[0][j] * j % p * (m+1-j) + 1LL * dpp[MI][j] * j + 1LL * dpp[PL][j] * (m+1-j)) % p; dpc[PL][j] = 1LL * (m+1-j) * sdp[PL][j] % p; } //FORI(i,m) { // cerr << i << " : " << dpc[MI][i] << "\t" << dpc[0][i] << "\t" << dpc[PL][i] << "\n"; //} //cerr << "\n"; FOR(c,3) FORI(i,m) { dpp[c][i] = dpc[c][i]; dpc[c][i] = 0; } } int res = 0; FORI(i,m) { res += dpp[0][i]; if (res >= p) res -= p; } return res; } int main() { scanf("%d%d%d", &n, &m, &p); printf("%d\n", run_nm()); return 0; } |
English