#include <bits/stdc++.h>
long long h, w;
long long n;
std::vector<long long> T;
long long find_top_score(long long a, long long b) {
long long output = 0;
//std::cout << a << " " << b << std::endl;
int idx = 0;
while (T[idx] > a || T[idx] > b) {
idx++;
}
output += (a / T[idx]) * (b / T[idx]);
long long new_a = a - (a / T[idx]) * T[idx];
long long new_b = b - (b / T[idx]) * T[idx];
if (new_a != 0) {
output += find_top_score(new_a, b - new_b);
}
if (new_b != 0) {
output += find_top_score(a - new_a, new_b);
}
if (new_a != 0 && new_b != 0) {
output += find_top_score(new_a, new_b);
}
return output;
}
int main() {
// std::ios_base::sync_with_stdio(false);
// std::cin.tie(NULL);
// std::cout.tie(NULL);
std::cin >> h >> w;
std::cin >> n;
for (int i = 0; i < n; i++) {
long long tmp;
std::cin >> tmp;
T.push_back(tmp);
}
std::sort(T.begin(), T.end(), std::greater<int>());
// std::cout << "T: ";
// for (int i = 0; i < T.size(); i++) {
// std::cout << T[i] << " ";
// }
int output = 0;
if (h % T.back() != 0 || w % T.back() != 0) {
std::cout << -1 << std::endl;
return 0;
}
std::cout << find_top_score(h, w);
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 | #include <bits/stdc++.h> long long h, w; long long n; std::vector<long long> T; long long find_top_score(long long a, long long b) { long long output = 0; //std::cout << a << " " << b << std::endl; int idx = 0; while (T[idx] > a || T[idx] > b) { idx++; } output += (a / T[idx]) * (b / T[idx]); long long new_a = a - (a / T[idx]) * T[idx]; long long new_b = b - (b / T[idx]) * T[idx]; if (new_a != 0) { output += find_top_score(new_a, b - new_b); } if (new_b != 0) { output += find_top_score(a - new_a, new_b); } if (new_a != 0 && new_b != 0) { output += find_top_score(new_a, new_b); } return output; } int main() { // std::ios_base::sync_with_stdio(false); // std::cin.tie(NULL); // std::cout.tie(NULL); std::cin >> h >> w; std::cin >> n; for (int i = 0; i < n; i++) { long long tmp; std::cin >> tmp; T.push_back(tmp); } std::sort(T.begin(), T.end(), std::greater<int>()); // std::cout << "T: "; // for (int i = 0; i < T.size(); i++) { // std::cout << T[i] << " "; // } int output = 0; if (h % T.back() != 0 || w % T.back() != 0) { std::cout << -1 << std::endl; return 0; } std::cout << find_top_score(h, w); return 0; } |
English