#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define ll long long const ll maxn = 1e2 + 7; ll x, y, n, a, b, sa, sb, res, t[maxn]; priority_queue<pair<ll, ll>> q; vector<ll> v; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> x >> y >> n; for(int i = 1; i <= n; i++){ cin >> t[i]; } if(x % t[1] != 0 || y % t[1] != 0){ cout << -1 <<'\n'; return 0; } q.push({min(x, y),max(x, y)}); for(int i = n; i >= 1; i--){ while(!q.empty() && q.top().st >= t[i] && q.top().nd >= t[i]){//cout << i << " " << '\n'; a = q.top().st; b = q.top().nd; q.pop(); sa = a / t[i]; sb = b / t[i]; res += sa * sb; if(a % t[i] != 0) q.push({min(a - (sa * t[i]), sb * t[i]), max(a - (sa * t[i]), sb * t[i])}); if(b % t[i] != 0) q.push({min(a, b - (sb * t[i])), max(a, b - (sb * t[i]))}); // cout << i << " " << a << " " << sa << " " << b << " " << sb << " " << res << '\n'; } } cout << res << '\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 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define ll long long const ll maxn = 1e2 + 7; ll x, y, n, a, b, sa, sb, res, t[maxn]; priority_queue<pair<ll, ll>> q; vector<ll> v; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> x >> y >> n; for(int i = 1; i <= n; i++){ cin >> t[i]; } if(x % t[1] != 0 || y % t[1] != 0){ cout << -1 <<'\n'; return 0; } q.push({min(x, y),max(x, y)}); for(int i = n; i >= 1; i--){ while(!q.empty() && q.top().st >= t[i] && q.top().nd >= t[i]){//cout << i << " " << '\n'; a = q.top().st; b = q.top().nd; q.pop(); sa = a / t[i]; sb = b / t[i]; res += sa * sb; if(a % t[i] != 0) q.push({min(a - (sa * t[i]), sb * t[i]), max(a - (sa * t[i]), sb * t[i])}); if(b % t[i] != 0) q.push({min(a, b - (sb * t[i])), max(a, b - (sb * t[i]))}); // cout << i << " " << a << " " << sa << " " << b << " " << sb << " " << res << '\n'; } } cout << res << '\n'; } |