#include <cstdio> using namespace std; int h, w, n; int d[30]; long long int calc(int h, int w, int k) { // printf("! h: %d w: %d k: %d\n", h, w, k); int x = k; while (d[x] > h) { x--; if (x == -1) { return -1ll; } } long long int res1, res2; res1 = 0ll; if (h % d[x] != 0) { res1 = calc(h % d[x], w, x); } res2 = 0ll; if (w % d[x] != 0) { res2 = calc(w % d[x], h - h % d[x], x); } if ((res1 == -1ll) || (res2 == -1ll)) { return -1ll; } else { return res1 + res2 + (long long)(h / d[x]) * (long long)(w / d[x]); } } int main() { scanf("%d %d", &h, &w); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } if (h > w) { int x = h; h = w; w = x; } printf("%lld\n", calc(h, w, n - 1)); 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 | #include <cstdio> using namespace std; int h, w, n; int d[30]; long long int calc(int h, int w, int k) { // printf("! h: %d w: %d k: %d\n", h, w, k); int x = k; while (d[x] > h) { x--; if (x == -1) { return -1ll; } } long long int res1, res2; res1 = 0ll; if (h % d[x] != 0) { res1 = calc(h % d[x], w, x); } res2 = 0ll; if (w % d[x] != 0) { res2 = calc(w % d[x], h - h % d[x], x); } if ((res1 == -1ll) || (res2 == -1ll)) { return -1ll; } else { return res1 + res2 + (long long)(h / d[x]) * (long long)(w / d[x]); } } int main() { scanf("%d %d", &h, &w); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } if (h > w) { int x = h; h = w; w = x; } printf("%lld\n", calc(h, w, n - 1)); return 0; } |