#include "bits/stdc++.h" using namespace std; using ll = long long; bool impossible = false; ll find(ll h, ll w, vector <ll> d){ if(h < 1 || w < 1) return 0; if(d.size() == 0){ impossible = true; return 0; } if(d.back() > h || d.back() > w){ d.pop_back(); return find(h, w, d); } ll ans = 0; ll x = d.back(); d.pop_back(); ll maxh = (h/x)*x; ll maxw = (w/x)*x; ans += (maxh/x) * (maxw/x); ans += find(h - maxh, maxw, d); ans += find(maxh, w - maxw, d); ans += find(h - maxh, w - maxw, d); return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll h, w, n; cin >> h >> w >> n; vector <ll> d(n); for(auto& x:d) cin >> x; sort(d.begin(), d.end()); ll ans = find(h, w, d); cout << (impossible ? -1 : ans); }
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 "bits/stdc++.h" using namespace std; using ll = long long; bool impossible = false; ll find(ll h, ll w, vector <ll> d){ if(h < 1 || w < 1) return 0; if(d.size() == 0){ impossible = true; return 0; } if(d.back() > h || d.back() > w){ d.pop_back(); return find(h, w, d); } ll ans = 0; ll x = d.back(); d.pop_back(); ll maxh = (h/x)*x; ll maxw = (w/x)*x; ans += (maxh/x) * (maxw/x); ans += find(h - maxh, maxw, d); ans += find(maxh, w - maxw, d); ans += find(h - maxh, w - maxw, d); return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll h, w, n; cin >> h >> w >> n; vector <ll> d(n); for(auto& x:d) cin >> x; sort(d.begin(), d.end()); ll ans = find(h, w, d); cout << (impossible ? -1 : ans); } |