#include <iostream> #include <vector> using namespace std; using ll = long long; vector<ll> d; ll f(ll h, ll w) { if (h == 0 || w == 0) return 0; int n = d.size() - 1; while (d[n] > h || d[n] > w) n--; ll y = h / d[n], x = w / d[n]; return x * y + f(h - y * d[n], w - x * d[n]) + f(h - y * d[n], x * d[n]) + f(y * d[n], w - x * d[n]); } // w - d[n] | d[n] // h - d[n] // d[n] int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); ll h, w, n; cin >> h >> w >> n; d.resize(n); for (auto &i : d) cin >> i; if (h % d[0] || w % d[0]) { cout << -1; return 0; } cout << f(h, w) << '\n'; } /* +------------------------+ | | + +-------------------+ | | | | | | | | | +----+-------------------+ */
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 | #include <iostream> #include <vector> using namespace std; using ll = long long; vector<ll> d; ll f(ll h, ll w) { if (h == 0 || w == 0) return 0; int n = d.size() - 1; while (d[n] > h || d[n] > w) n--; ll y = h / d[n], x = w / d[n]; return x * y + f(h - y * d[n], w - x * d[n]) + f(h - y * d[n], x * d[n]) + f(y * d[n], w - x * d[n]); } // w - d[n] | d[n] // h - d[n] // d[n] int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); ll h, w, n; cin >> h >> w >> n; d.resize(n); for (auto &i : d) cin >> i; if (h % d[0] || w % d[0]) { cout << -1; return 0; } cout << f(h, w) << '\n'; } /* +------------------------+ | | + +-------------------+ | | | | | | | | | +----+-------------------+ */ |