#include <bits/stdc++.h> using namespace std; int n, m, p, w; vector<vector<bool>> t; void bactrack(int i) { if (i >= n) { w++; } else { bool ok = 0; for (int k = 1; k <= m; k++) { for (int j = 0; j + k <= m; j++) { for (int ii = j; ii < j + k; ii++) { t[i][ii] = 1; if (t[i - 1][ii]) { ok = 1; } } if (ok) { bactrack(i + 1); ok = 0; } for (int ii = j; ii < j + k; ii++) { t[i][ii] = 0; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> p; if (n == 1) { cout << ((long long)m * (long long)(m + 1) / 2) % (long long)p; return 0; } t.resize(n + 3); for (int i = 0; i <= n; i++) { t[i].resize(m + 3); } for (int k = 1; k <= m; k++) { for (int j = 0; j + k <= m; j++) { for (int ii = j; ii < j + k; ii++) { t[0][ii] = 1; } bactrack(1); for (int ii = j; ii < j + k; ii++) { t[0][ii] = 0; } } } cout << w % p; 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 | #include <bits/stdc++.h> using namespace std; int n, m, p, w; vector<vector<bool>> t; void bactrack(int i) { if (i >= n) { w++; } else { bool ok = 0; for (int k = 1; k <= m; k++) { for (int j = 0; j + k <= m; j++) { for (int ii = j; ii < j + k; ii++) { t[i][ii] = 1; if (t[i - 1][ii]) { ok = 1; } } if (ok) { bactrack(i + 1); ok = 0; } for (int ii = j; ii < j + k; ii++) { t[i][ii] = 0; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> p; if (n == 1) { cout << ((long long)m * (long long)(m + 1) / 2) % (long long)p; return 0; } t.resize(n + 3); for (int i = 0; i <= n; i++) { t[i].resize(m + 3); } for (int k = 1; k <= m; k++) { for (int j = 0; j + k <= m; j++) { for (int ii = j; ii < j + k; ii++) { t[0][ii] = 1; } bactrack(1); for (int ii = j; ii < j + k; ii++) { t[0][ii] = 0; } } } cout << w % p; return 0; } |