#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int M = 100;
bool t[M][M][M][M];
int64_t dp1[M][M], dp2[M][M];
int main() {
ios_base::sync_with_stdio(false);
int64_t n, m, p;
cin >> n >> m >> p;
for(int a1 = 0; a1 < m; ++a1)
for(int b1 = a1; b1 < m; ++b1)
for(int a2 = 0; a2 < m; ++a2)
for(int b2 = a2; b2 < m; ++b2) {
if(a1 <= b2 && b2 <= b1) t[a1][b1][a2][b2] = true;
if(a1 <= a2 && a2 <= b1) t[a1][b1][a2][b2] = true;
if(a2 <= a1 && b1 <= b2) t[a1][b1][a2][b2] = true;
}
for(int a1 = 0; a1 < m; ++a1)
for(int b1 = a1; b1 < m; ++b1)
dp1[a1][b1] = 1;
for(int i = 0; i < n-1; ++i) {
for(int a1 = 0; a1 < m; ++a1)
for(int b1 = a1; b1 < m; ++b1)
for(int a2 = 0; a2 < m; ++a2)
for(int b2 = a2; b2 < m; ++b2) {
if(t[a1][b1][a2][b2]) {
dp2[a2][b2] += dp1[a1][b1];
dp2[a2][b2] %= p;
}
}
for(int i = 0; i < m; ++i)
for(int j = 0; j < m; ++j) {
dp1[i][j] = dp2[i][j];
dp2[i][j] = 0;
}
}
int64_t res = 0;
for(int a1 = 0; a1 < m; ++a1)
for(int b1 = a1; b1 < m; ++b1)
res = (res + dp1[a1][b1])%p;
cout << res << '\n';
}
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 | #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; const int M = 100; bool t[M][M][M][M]; int64_t dp1[M][M], dp2[M][M]; int main() { ios_base::sync_with_stdio(false); int64_t n, m, p; cin >> n >> m >> p; for(int a1 = 0; a1 < m; ++a1) for(int b1 = a1; b1 < m; ++b1) for(int a2 = 0; a2 < m; ++a2) for(int b2 = a2; b2 < m; ++b2) { if(a1 <= b2 && b2 <= b1) t[a1][b1][a2][b2] = true; if(a1 <= a2 && a2 <= b1) t[a1][b1][a2][b2] = true; if(a2 <= a1 && b1 <= b2) t[a1][b1][a2][b2] = true; } for(int a1 = 0; a1 < m; ++a1) for(int b1 = a1; b1 < m; ++b1) dp1[a1][b1] = 1; for(int i = 0; i < n-1; ++i) { for(int a1 = 0; a1 < m; ++a1) for(int b1 = a1; b1 < m; ++b1) for(int a2 = 0; a2 < m; ++a2) for(int b2 = a2; b2 < m; ++b2) { if(t[a1][b1][a2][b2]) { dp2[a2][b2] += dp1[a1][b1]; dp2[a2][b2] %= p; } } for(int i = 0; i < m; ++i) for(int j = 0; j < m; ++j) { dp1[i][j] = dp2[i][j]; dp2[i][j] = 0; } } int64_t res = 0; for(int a1 = 0; a1 < m; ++a1) for(int b1 = a1; b1 < m; ++b1) res = (res + dp1[a1][b1])%p; cout << res << '\n'; } |
English