#include <iostream> #include <vector> typedef long long ll; ll f(int w, int h, std::vector<int>& A) { if (w == 0 || h == 0) return 0; int i = A.size()-1; while (A[i] > w) i--; int biggest = A[i]; ll sum = 0; int left = w; while (left > 0) { while (A[i] > left && i >= 0) i--; if (i < 0) return -1; sum += (ll) (left / A[i]) * (biggest / A[i]); left %= A[i]; } ll rest = f(h % biggest, w, A); if (rest == -1) return -1; return sum * (h / biggest) + rest; } void solve() { int w, h, n; std::cin >> w >> h >> n; std::vector<int> A(n); for (auto& a : A) std::cin >> a; if (w > h) std::swap(w, h); std::cout << f(w, h, A) << '\n'; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); solve(); 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 | #include <iostream> #include <vector> typedef long long ll; ll f(int w, int h, std::vector<int>& A) { if (w == 0 || h == 0) return 0; int i = A.size()-1; while (A[i] > w) i--; int biggest = A[i]; ll sum = 0; int left = w; while (left > 0) { while (A[i] > left && i >= 0) i--; if (i < 0) return -1; sum += (ll) (left / A[i]) * (biggest / A[i]); left %= A[i]; } ll rest = f(h % biggest, w, A); if (rest == -1) return -1; return sum * (h / biggest) + rest; } void solve() { int w, h, n; std::cin >> w >> h >> n; std::vector<int> A(n); for (auto& a : A) std::cin >> a; if (w > h) std::swap(w, h); std::cout << f(w, h, A) << '\n'; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); solve(); return 0; } |