#include <iostream> #include <vector> #include <algorithm> #include <cmath> #define st first #define nd second typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; if (h > w) swap(h, w); int n; cin >> n; vector<ll> o(n); for (int i = 0; i < n; i++) cin >> o[i]; sort(o.begin(), o.end()); if (h%o[0] || w%o[0]) { cout << "-1\n"; return 0; } ll ans = 0; for (int i = 1; i < n; i++) { ll a = h%o[i], b = w%o[i]; ans += ((a/o[i-1])*(w/o[i-1])) + ((b/o[i-1])*(h/o[i-1])) - ((a/o[i-1])*(b/o[i-1])); h -= a; w -= b; } ans += (h/o[n-1]) * (w/o[n-1]); cout << ans << '\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 | #include <iostream> #include <vector> #include <algorithm> #include <cmath> #define st first #define nd second typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; if (h > w) swap(h, w); int n; cin >> n; vector<ll> o(n); for (int i = 0; i < n; i++) cin >> o[i]; sort(o.begin(), o.end()); if (h%o[0] || w%o[0]) { cout << "-1\n"; return 0; } ll ans = 0; for (int i = 1; i < n; i++) { ll a = h%o[i], b = w%o[i]; ans += ((a/o[i-1])*(w/o[i-1])) + ((b/o[i-1])*(h/o[i-1])) - ((a/o[i-1])*(b/o[i-1])); h -= a; w -= b; } ans += (h/o[n-1]) * (w/o[n-1]); cout << ans << '\n'; } |