#include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 3e4 + 7; int w, h, n; int d[37]; ll solve(int w, int h, int k) { if (w == 0 || h == 0) { return 0; } ll cw = 0, ch = 0; while (cw == 0 || ch == 0) { cw = w / d[k]; ch = h / d[k]; k++; } k--; ll res = cw * ch; res += solve(w - cw * d[k], h, k + 1); res += solve(cw * d[k], h - ch * d[k], k + 1); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> h >> w >> n; for (int i = 0; i < n; ++i) { cin >> d[i]; } sort(d, d + n, greater<>()); if (w % d[n - 1] != 0 || h % d[n - 1] != 0) { cout << "-1\n"; return 0; } cout << solve(w, h, 0) << "\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 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> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif using ll = long long; using pii = pair<int, int>; const int NMAX = 3e4 + 7; int w, h, n; int d[37]; ll solve(int w, int h, int k) { if (w == 0 || h == 0) { return 0; } ll cw = 0, ch = 0; while (cw == 0 || ch == 0) { cw = w / d[k]; ch = h / d[k]; k++; } k--; ll res = cw * ch; res += solve(w - cw * d[k], h, k + 1); res += solve(cw * d[k], h - ch * d[k], k + 1); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> h >> w >> n; for (int i = 0; i < n; ++i) { cin >> d[i]; } sort(d, d + n, greater<>()); if (w % d[n - 1] != 0 || h % d[n - 1] != 0) { cout << "-1\n"; return 0; } cout << solve(w, h, 0) << "\n"; return 0; } |