#include <iostream> #include <unordered_map> #include <map> #include <vector> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int h, w, n; cin >> h >> w; cin >> n; vector<int> d(n, 0); for (int i = 0; i < n; i++) cin >> d[i]; long long num{ 0 }; int pic{ n - 1 }; int usedX{ 0 }, usedY{ 0 }; while (pic >= 0 && (usedX!=w || usedY!=h)) { if (usedX == 0) { if (d[pic] <= h && d[pic] <= w) { long long takeY = h / d[pic]; long long takeX = w / d[pic]; num += takeY * takeX; usedY = takeY * d[pic]; usedX = takeX * d[pic]; } } else { if (d[pic] <= h - usedY) { long long takeY = (h - usedY) / d[pic]; long long takeX = usedX / d[pic]; usedY += takeY*d[pic]; num += takeX * takeY; } if (d[pic] <= w - usedX) { long long takeY = h / d[pic]; long long takeX = (w - usedX) / d[pic]; usedX += takeX * d[pic]; num += takeX * takeY; } } pic--; } if (usedX != w || usedY != h) cout << "-1\n"; else cout << num << "\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 59 60 61 62 63 64 65 66 67 68 69 | #include <iostream> #include <unordered_map> #include <map> #include <vector> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int h, w, n; cin >> h >> w; cin >> n; vector<int> d(n, 0); for (int i = 0; i < n; i++) cin >> d[i]; long long num{ 0 }; int pic{ n - 1 }; int usedX{ 0 }, usedY{ 0 }; while (pic >= 0 && (usedX!=w || usedY!=h)) { if (usedX == 0) { if (d[pic] <= h && d[pic] <= w) { long long takeY = h / d[pic]; long long takeX = w / d[pic]; num += takeY * takeX; usedY = takeY * d[pic]; usedX = takeX * d[pic]; } } else { if (d[pic] <= h - usedY) { long long takeY = (h - usedY) / d[pic]; long long takeX = usedX / d[pic]; usedY += takeY*d[pic]; num += takeX * takeY; } if (d[pic] <= w - usedX) { long long takeY = h / d[pic]; long long takeX = (w - usedX) / d[pic]; usedX += takeX * d[pic]; num += takeX * takeY; } } pic--; } if (usedX != w || usedY != h) cout << "-1\n"; else cout << num << "\n"; return 0; } |