#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 2e5+6969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } ll w, h; int n; ll ans; vll d; int32_t main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> h >> w >> n; if(h > w) swap(h, w); d.resize(n); for(auto &e: d) { cin >> e; } ll g = __gcd(h, w); if(g % d[0] != 0) { puts("-1"); return 0; } priority_queue<pll> q; q.push(mp(h, w)); for(int i = n - 1; i >= 0; i--) { ll cur = d[i]; while(!q.empty()) { pll sz = q.top(); // sz.fi *= -1; // sz.se *= -1; if(sz.fi < cur) { break; } q.pop(); ans += (sz.fi / cur) * (sz.se / cur); ll a = sz.fi % cur; ll b = sz.se / cur * cur; if(a > b) swap(a, b); if(a > 0) q.push(mp(a, b)); a = sz.se % cur; b = sz.fi; if(a > b) swap(a, b); if(a > 0) q.push(mp(a, b)); } } cout << ans << "\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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back // #define st first #define nd second #define fi first #define se second #define vt vector #define FOR(a, b, c) for(int a=b; a<c; ++a) #define all(a) (a).begin(),(a).end() #define sz(a) (int)(a).size() typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef pii PII; typedef pll PLL; constexpr ll nax = 2e5+6969, INF = 1e9+2137; constexpr ld eps = 1e-9; mt19937_64 rng(6969); // mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); inline ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } ll w, h; int n; ll ans; vll d; int32_t main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> h >> w >> n; if(h > w) swap(h, w); d.resize(n); for(auto &e: d) { cin >> e; } ll g = __gcd(h, w); if(g % d[0] != 0) { puts("-1"); return 0; } priority_queue<pll> q; q.push(mp(h, w)); for(int i = n - 1; i >= 0; i--) { ll cur = d[i]; while(!q.empty()) { pll sz = q.top(); // sz.fi *= -1; // sz.se *= -1; if(sz.fi < cur) { break; } q.pop(); ans += (sz.fi / cur) * (sz.se / cur); ll a = sz.fi % cur; ll b = sz.se / cur * cur; if(a > b) swap(a, b); if(a > 0) q.push(mp(a, b)); a = sz.se % cur; b = sz.fi; if(a > b) swap(a, b); if(a > 0) q.push(mp(a, b)); } } cout << ans << "\n"; return 0; } |