#include <cstdio> using ull = unsigned long long; const int N = 37; int lens[N]; ull count(int w, int h, int k) { if (w == 0 || h == 0) return 0; while (lens[k] > w || lens[k] > h) k--; int cw = w / lens[k]; int ch = h / lens[k]; return (ull)cw * ch + count(w - cw * lens[k], h, k - 1) + count(cw * lens[k], h - ch * lens[k], k - 1); } int main() { int h, w, n; scanf("%d%d%d", &h, &w, &n); for (int i = 0; i < n; i++) scanf("%d", lens + i); if (h % lens[0] == 0 && w % lens[0] == 0) printf("%llu\n", count(w, h, n - 1)); else printf("-1\n"); 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 | #include <cstdio> using ull = unsigned long long; const int N = 37; int lens[N]; ull count(int w, int h, int k) { if (w == 0 || h == 0) return 0; while (lens[k] > w || lens[k] > h) k--; int cw = w / lens[k]; int ch = h / lens[k]; return (ull)cw * ch + count(w - cw * lens[k], h, k - 1) + count(cw * lens[k], h - ch * lens[k], k - 1); } int main() { int h, w, n; scanf("%d%d%d", &h, &w, &n); for (int i = 0; i < n; i++) scanf("%d", lens + i); if (h % lens[0] == 0 && w % lens[0] == 0) printf("%llu\n", count(w, h, n - 1)); else printf("-1\n"); return 0; } |