#include <bits/stdc++.h> #define pb push_back #define ll long long #define int long long using namespace std; ll ans = 0; vector <int> d; int readInt () { bool minus = false; int result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch-'0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = result*10 + (ch - '0'); } if (minus) return -result; else return result; } void s(int w, int h, int last) { for (; last >= 0; last--) { if (d[last] <= w && d[last] <= h) break; } auto dl = d[last]; ans += ((w/dl) * (h/dl)); if (w % dl != 0) s(w%dl, h-(h%dl), last-1); if (h % dl != 0) s(w-(w%dl), h%dl, last-1); if (w %dl != 0 && h % dl != 0) s(w%dl, h%dl, last-1); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int h = readInt(); int w = readInt(); int n = readInt(); for (int i = 0; i < n; i++) { int di = readInt(); d.pb(di); } if (w % d[0] != 0 || h % d[0]!= 0) { cout << -1 << endl; return 0; } s(w, h, d.size()-1); cout << ans << endl; }
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 58 59 60 | #include <bits/stdc++.h> #define pb push_back #define ll long long #define int long long using namespace std; ll ans = 0; vector <int> d; int readInt () { bool minus = false; int result = 0; char ch; ch = getchar(); while (true) { if (ch == '-') break; if (ch >= '0' && ch <= '9') break; ch = getchar(); } if (ch == '-') minus = true; else result = ch-'0'; while (true) { ch = getchar(); if (ch < '0' || ch > '9') break; result = result*10 + (ch - '0'); } if (minus) return -result; else return result; } void s(int w, int h, int last) { for (; last >= 0; last--) { if (d[last] <= w && d[last] <= h) break; } auto dl = d[last]; ans += ((w/dl) * (h/dl)); if (w % dl != 0) s(w%dl, h-(h%dl), last-1); if (h % dl != 0) s(w-(w%dl), h%dl, last-1); if (w %dl != 0 && h % dl != 0) s(w%dl, h%dl, last-1); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int h = readInt(); int w = readInt(); int n = readInt(); for (int i = 0; i < n; i++) { int di = readInt(); d.pb(di); } if (w % d[0] != 0 || h % d[0]!= 0) { cout << -1 << endl; return 0; } s(w, h, d.size()-1); cout << ans << endl; } |