#include <iostream> constexpr int max_n = 40; long long input[max_n]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int h, w; std::cin >> h >> w; int n; std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> input[i]; long long output = 0ll; int j = n - 1; while (h > 0 && j >= 0) { int count = h / input[j]; h %= input[j]; long long cost = 0ll; int w2 = w; int i = j; while (w2 > 0 && i >= 0) { cost += (w2 / input[i]) * (input[j] / input[i]); w2 %= input[i]; --i; } if (w2 != 0) { std::cout << "-1\n"; return 0; } output += cost * count; --j; } if (h != 0) { std::cout << "-1\n"; return 0; } std::cout << output << '\n'; }
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 | #include <iostream> constexpr int max_n = 40; long long input[max_n]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int h, w; std::cin >> h >> w; int n; std::cin >> n; for (int i = 0; i < n; ++i) std::cin >> input[i]; long long output = 0ll; int j = n - 1; while (h > 0 && j >= 0) { int count = h / input[j]; h %= input[j]; long long cost = 0ll; int w2 = w; int i = j; while (w2 > 0 && i >= 0) { cost += (w2 / input[i]) * (input[j] / input[i]); w2 %= input[i]; --i; } if (w2 != 0) { std::cout << "-1\n"; return 0; } output += cost * count; --j; } if (h != 0) { std::cout << "-1\n"; return 0; } std::cout << output << '\n'; } |