#include <iostream> #include <vector> using namespace std; vector<int> pictures; long long solve(int height, int width, int maxIndex) { int max = pictures[maxIndex]; if (max > height || max > width) { if (maxIndex > 0) { return solve(height, width, maxIndex - 1); } return -1; } long long maxRows = height / max; long long maxColumns = width / max; int maxWidth = maxColumns * max; int maxHeight = maxRows * max; if (maxIndex == 0) { if (height > maxHeight || width > maxWidth) { return -1; } else { return maxRows * maxColumns; } } long long below = 0; if (height > maxHeight) { below = solve(height - maxHeight, maxWidth, maxIndex - 1); if (below == -1) { return -1; } } long long right = 0; if (width > maxWidth) { right = solve(height, width - maxWidth, maxIndex - 1); if (right == -1) { return -1; } } return below + right + maxRows * maxColumns; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int height, width; cin >> height >> width; int n; cin >> n; for (int i = 0; i < n; i++) { int picture; cin >> picture; pictures.push_back(picture); } long long num = solve(height, width, pictures.size() - 1); cout << num << endl; }
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 59 60 61 62 | #include <iostream> #include <vector> using namespace std; vector<int> pictures; long long solve(int height, int width, int maxIndex) { int max = pictures[maxIndex]; if (max > height || max > width) { if (maxIndex > 0) { return solve(height, width, maxIndex - 1); } return -1; } long long maxRows = height / max; long long maxColumns = width / max; int maxWidth = maxColumns * max; int maxHeight = maxRows * max; if (maxIndex == 0) { if (height > maxHeight || width > maxWidth) { return -1; } else { return maxRows * maxColumns; } } long long below = 0; if (height > maxHeight) { below = solve(height - maxHeight, maxWidth, maxIndex - 1); if (below == -1) { return -1; } } long long right = 0; if (width > maxWidth) { right = solve(height, width - maxWidth, maxIndex - 1); if (right == -1) { return -1; } } return below + right + maxRows * maxColumns; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int height, width; cin >> height >> width; int n; cin >> n; for (int i = 0; i < n; i++) { int picture; cin >> picture; pictures.push_back(picture); } long long num = solve(height, width, pictures.size() - 1); cout << num << endl; } |