#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> obrazy;
pair<long long, pair<int, int>> f(int h, int w) {
if (h > w)
swap(h, w);
int hleft = h, builtw = 0;
long long ile = 0;
for (auto it : obrazy) {
if (it <= hleft) {
if (builtw == 0) {
builtw = it;
}
long long countpion = hleft / it;
ile += countpion * builtw / it;
hleft %= it;
}
}
ile *= w / builtw;
w %= builtw;
return {ile, {h, w}};
}
int main() {
int h, w, n, x;
cin >> h >> w;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
obrazy.push_back(x);
}
reverse(obrazy.begin(), obrazy.end());
if (h % obrazy.back() != 0 || w % obrazy.back() != 0) {
cout << -1;
return 0;
}
long long wyn = 0;
while (h > 0 && w > 0) {
auto ret = f(h, w);
wyn += ret.first;
h = ret.second.first;
w = ret.second.second;
}
cout << wyn;
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> obrazy; pair<long long, pair<int, int>> f(int h, int w) { if (h > w) swap(h, w); int hleft = h, builtw = 0; long long ile = 0; for (auto it : obrazy) { if (it <= hleft) { if (builtw == 0) { builtw = it; } long long countpion = hleft / it; ile += countpion * builtw / it; hleft %= it; } } ile *= w / builtw; w %= builtw; return {ile, {h, w}}; } int main() { int h, w, n, x; cin >> h >> w; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; obrazy.push_back(x); } reverse(obrazy.begin(), obrazy.end()); if (h % obrazy.back() != 0 || w % obrazy.back() != 0) { cout << -1; return 0; } long long wyn = 0; while (h > 0 && w > 0) { auto ret = f(h, w); wyn += ret.first; h = ret.second.first; w = ret.second.second; } cout << wyn; return 0; } |
English