#include <cstdio> #include <utility> #include <unordered_map> #include <map> #include <iostream> int n, m, p; std::map<std::tuple<int, int, int>, long long> mem; long long solve(int i, int j, int n_) { long long result = 0LL; if (n_ == 1) return 1LL; if (mem.find(std::make_tuple(i, j, n_)) == mem.end() && mem.find(std::make_tuple(m - j + 1, m - i + 1, n_)) == mem.end()) { for (int ii = 1; ii <= j; ++ii) for (int jj = (ii < i ? i : ii); jj <= m; ++jj) result = (result + solve(ii, jj, n_ - 1)) % p; mem[std::make_tuple(i, j, n_)] = result; mem[std::make_tuple(m - j + 1, m - i + 1, n_)] = result; } if (mem.find(std::make_tuple(i, j, n_)) == mem.end()) mem[std::make_tuple(i, j, n_)] = mem[std::make_tuple(m - j + 1, m - i + 1, n_)]; if (mem.find(std::make_tuple(m - j + 1, m - i + 1, n_)) == mem.end()) mem[std::make_tuple(m - j + 1, m - i + 1, n_)] = mem[std::make_tuple(i, j, n_)]; return mem[std::make_tuple(i, j, n_)]; } int main() { scanf("%d%d%d", &n, &m, &p); printf("%lld\n", solve(1, m, n + 1)); 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 | #include <cstdio> #include <utility> #include <unordered_map> #include <map> #include <iostream> int n, m, p; std::map<std::tuple<int, int, int>, long long> mem; long long solve(int i, int j, int n_) { long long result = 0LL; if (n_ == 1) return 1LL; if (mem.find(std::make_tuple(i, j, n_)) == mem.end() && mem.find(std::make_tuple(m - j + 1, m - i + 1, n_)) == mem.end()) { for (int ii = 1; ii <= j; ++ii) for (int jj = (ii < i ? i : ii); jj <= m; ++jj) result = (result + solve(ii, jj, n_ - 1)) % p; mem[std::make_tuple(i, j, n_)] = result; mem[std::make_tuple(m - j + 1, m - i + 1, n_)] = result; } if (mem.find(std::make_tuple(i, j, n_)) == mem.end()) mem[std::make_tuple(i, j, n_)] = mem[std::make_tuple(m - j + 1, m - i + 1, n_)]; if (mem.find(std::make_tuple(m - j + 1, m - i + 1, n_)) == mem.end()) mem[std::make_tuple(m - j + 1, m - i + 1, n_)] = mem[std::make_tuple(i, j, n_)]; return mem[std::make_tuple(i, j, n_)]; } int main() { scanf("%d%d%d", &n, &m, &p); printf("%lld\n", solve(1, m, n + 1)); return 0; } |