#include <stdio.h> #include <vector> #include <utility> inline bool intersect(std::pair<int, int> p1, std::pair<int, int> p2) { return p2.second >= p1.first && p1.second >= p2.first; } int main() { int n, m, p; std::vector<std::pair<int, int>> intervals; scanf("%d%d%d", &m, &n, &p); if (m == 1) { printf("%lld\n", ((long long)(n+1) * n / 2) % p); return 0; } for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) intervals.push_back(std::make_pair(i, j)); int DP[intervals.size()][2]; for (int i = 0; i < intervals.size(); i++) DP[i][1] = 1; for (int j = 2; j <= m; j++) { int curr = j % 2; int prev = 1 - curr; for (int i = 0; i < intervals.size(); i++) { DP[i][curr] = 0; for (int k = 0; k < intervals.size(); k++) if (intersect(intervals[i], intervals[k])) DP[i][curr] = (DP[i][curr] + DP[k][prev]) % p; } } int answer = 0; for (int i = 0; i < intervals.size(); i++) answer = (answer + DP[i][m%2]) % p; printf("%d\n", answer); 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 | #include <stdio.h> #include <vector> #include <utility> inline bool intersect(std::pair<int, int> p1, std::pair<int, int> p2) { return p2.second >= p1.first && p1.second >= p2.first; } int main() { int n, m, p; std::vector<std::pair<int, int>> intervals; scanf("%d%d%d", &m, &n, &p); if (m == 1) { printf("%lld\n", ((long long)(n+1) * n / 2) % p); return 0; } for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) intervals.push_back(std::make_pair(i, j)); int DP[intervals.size()][2]; for (int i = 0; i < intervals.size(); i++) DP[i][1] = 1; for (int j = 2; j <= m; j++) { int curr = j % 2; int prev = 1 - curr; for (int i = 0; i < intervals.size(); i++) { DP[i][curr] = 0; for (int k = 0; k < intervals.size(); k++) if (intersect(intervals[i], intervals[k])) DP[i][curr] = (DP[i][curr] + DP[k][prev]) % p; } } int answer = 0; for (int i = 0; i < intervals.size(); i++) answer = (answer + DP[i][m%2]) % p; printf("%d\n", answer); return 0; } |