#include <cstdio> #include <vector> using namespace std; unsigned long long get_result(vector<unsigned long long>& d, unsigned long long h, unsigned long long w) { if (h == 0 || w == 0) { return 0; } for (int i = d.size() - 1; i >= 0; i--) { unsigned long long w_fit = w / d[i]; unsigned long long h_fit = h / d[i]; unsigned long long paintings = w_fit * h_fit; if (paintings) { return paintings + get_result(d, h - h_fit * d[i], w_fit * d[i]) + get_result(d, h_fit * d[i], w - w_fit * d[i]) + get_result(d, h - h_fit * d[i], w - w_fit * d[i]); } } return 0; } int main() { unsigned long long h, w; scanf("%llu %llu", &h, &w); int n; scanf("%d", &n); vector<unsigned long long> d(n); for (int i = 0; i < n; i++) { scanf("%llu", &d[i]); } if (h % d[0] != 0 || w % d[0] != 0) { puts("-1"); } else { printf("%llu\n", get_result(d, 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 | #include <cstdio> #include <vector> using namespace std; unsigned long long get_result(vector<unsigned long long>& d, unsigned long long h, unsigned long long w) { if (h == 0 || w == 0) { return 0; } for (int i = d.size() - 1; i >= 0; i--) { unsigned long long w_fit = w / d[i]; unsigned long long h_fit = h / d[i]; unsigned long long paintings = w_fit * h_fit; if (paintings) { return paintings + get_result(d, h - h_fit * d[i], w_fit * d[i]) + get_result(d, h_fit * d[i], w - w_fit * d[i]) + get_result(d, h - h_fit * d[i], w - w_fit * d[i]); } } return 0; } int main() { unsigned long long h, w; scanf("%llu %llu", &h, &w); int n; scanf("%d", &n); vector<unsigned long long> d(n); for (int i = 0; i < n; i++) { scanf("%llu", &d[i]); } if (h % d[0] != 0 || w % d[0] != 0) { puts("-1"); } else { printf("%llu\n", get_result(d, h, w)); } return 0; } |