#include <bits/stdc++.h> using namespace std; typedef long long ll; ll res(ll h, ll w, ll n, ll d[]) { if (h == 0 || w == 0) { return 0; } ll p = 1; ll k = n; ll m; while (p < k) { m = (p + k + 1) / 2; if (d[m] <= min(h, w)) { p = m; } else { k = m - 1; } } ll ind = p; return (h / d[ind]) * (w / d[ind]) + res(h % d[ind], d[ind] * (w / d[ind]), ind - 1, d) + res(d[ind] * (h / d[ind]), w % d[ind], ind - 1, d) + res(h % d[ind], w % d[ind], ind - 1, d); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int h, w; cin >> h >> w; int n; cin >> n; ll d[n + 1]; for (int i = 1; i <= n; i++) { cin >> d[i]; } if (h % d[1] == 0 && w % d[1] == 0) { h /= d[1]; w /= d[1]; for (int i = n; i > 0; i--) { d[i] /= d[1]; } cout << res(h, w, n, d) << "\n"; } else { cout << "-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 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; typedef long long ll; ll res(ll h, ll w, ll n, ll d[]) { if (h == 0 || w == 0) { return 0; } ll p = 1; ll k = n; ll m; while (p < k) { m = (p + k + 1) / 2; if (d[m] <= min(h, w)) { p = m; } else { k = m - 1; } } ll ind = p; return (h / d[ind]) * (w / d[ind]) + res(h % d[ind], d[ind] * (w / d[ind]), ind - 1, d) + res(d[ind] * (h / d[ind]), w % d[ind], ind - 1, d) + res(h % d[ind], w % d[ind], ind - 1, d); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int h, w; cin >> h >> w; int n; cin >> n; ll d[n + 1]; for (int i = 1; i <= n; i++) { cin >> d[i]; } if (h % d[1] == 0 && w % d[1] == 0) { h /= d[1]; w /= d[1]; for (int i = n; i > 0; i--) { d[i] /= d[1]; } cout << res(h, w, n, d) << "\n"; } else { cout << "-1\n"; } return 0; } |