#include <bits/stdc++.h> using namespace std; typedef long long ll; ll countFit(ll w, ll h, ll s, ll &cornerX, ll &cornerY) { ll wX = w / s, wY = h / s; cornerX = wX * s; cornerY = wY * s; return wX * wY; } int main() { ios_base::sync_with_stdio(0); ll w, h; cin >> w >> h; ll n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.rbegin(), v.rend()); ll cornerX = 0, cornerY = 0; ll result = countFit(w, h, v[0], cornerX, cornerY); ll id = 1; while ((cornerX < w || cornerY < h) && id < v.size()) { ll tempX, tempY; result += countFit(w, h, v[id], tempX, tempY) - countFit(cornerX, cornerY, v[id], cornerX, cornerY); cornerX = tempX; cornerY = tempY; id++; } if (cornerX < w || cornerY < h) cout << -1; else cout << result; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; ll countFit(ll w, ll h, ll s, ll &cornerX, ll &cornerY) { ll wX = w / s, wY = h / s; cornerX = wX * s; cornerY = wY * s; return wX * wY; } int main() { ios_base::sync_with_stdio(0); ll w, h; cin >> w >> h; ll n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } sort(v.rbegin(), v.rend()); ll cornerX = 0, cornerY = 0; ll result = countFit(w, h, v[0], cornerX, cornerY); ll id = 1; while ((cornerX < w || cornerY < h) && id < v.size()) { ll tempX, tempY; result += countFit(w, h, v[id], tempX, tempY) - countFit(cornerX, cornerY, v[id], cornerX, cornerY); cornerX = tempX; cornerY = tempY; id++; } if (cornerX < w || cornerY < h) cout << -1; else cout << result; } |