#include <iostream> #include <vector> #include <algorithm> int h, w, n; std::vector<int> d; void input() { std::cin >> h >> w >> n; int x; for (int i = 0; i < n; i++) { std::cin >> x; d.push_back(x); } std::reverse(d.begin(), d.end()); } long long solve(int x, int y) { int m = std::min(x, y); if (m == 0) { return 0; } int i = 0; while (i < n && d[i] > m) { i++; } // std::cout << x << " " << y << " " << m << " " << d[i] << "\n"; if (i == n) { return -1; } long long used_horizontal = x / d[i]; long long used_vertical = y / d[i]; long long result = used_horizontal * used_vertical; long long x_diff = used_horizontal * d[i]; long long y_diff = used_vertical * d[i]; long long r1 = solve(x - x_diff, y_diff); long long r2 = solve(x_diff, y - y_diff); long long r3 = solve(x - x_diff, y - y_diff); if (r1 == -1 || r2 == -1 || r3 == -1) { return -1; } return result + r1 + r2 + r3; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); input(); std::cout << solve(w, h) << "\n"; 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 | #include <iostream> #include <vector> #include <algorithm> int h, w, n; std::vector<int> d; void input() { std::cin >> h >> w >> n; int x; for (int i = 0; i < n; i++) { std::cin >> x; d.push_back(x); } std::reverse(d.begin(), d.end()); } long long solve(int x, int y) { int m = std::min(x, y); if (m == 0) { return 0; } int i = 0; while (i < n && d[i] > m) { i++; } // std::cout << x << " " << y << " " << m << " " << d[i] << "\n"; if (i == n) { return -1; } long long used_horizontal = x / d[i]; long long used_vertical = y / d[i]; long long result = used_horizontal * used_vertical; long long x_diff = used_horizontal * d[i]; long long y_diff = used_vertical * d[i]; long long r1 = solve(x - x_diff, y_diff); long long r2 = solve(x_diff, y - y_diff); long long r3 = solve(x - x_diff, y - y_diff); if (r1 == -1 || r2 == -1 || r3 == -1) { return -1; } return result + r1 + r2 + r3; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); input(); std::cout << solve(w, h) << "\n"; return 0; } |