#include <bits/stdc++.h> #define ndl '\n' #define ll long long #define INF 1000000007 #define st first #define nd second #define debug(x) cout << #x << ": " << x << ndl #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define all(x) (x).begin(), (x).end() using namespace std; ll minimalizuj(ll a, ll b, vector<ll> &t) { for(ll i = t.size()-1; i>=0; i--) if(t[i] <= a && t[i] <= b) { ll ile_a = a/t[i], ile_b = b/t[i]; return ile_a * ile_b + minimalizuj(a-ile_a*t[i], b-ile_b*t[i], t) + minimalizuj(ile_a*t[i], b-ile_b*t[i], t) + minimalizuj(a-ile_a*t[i], ile_b*t[i], t); } return 0; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll a, b, n; cin>>a>>b>>n; vector<ll> t(n); for(auto &x:t) cin>>x; if(a%t[0] || b%t[0]) { cout<<-1; return 0; } cout<<minimalizuj(a, b, t); }
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 | #include <bits/stdc++.h> #define ndl '\n' #define ll long long #define INF 1000000007 #define st first #define nd second #define debug(x) cout << #x << ": " << x << ndl #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define all(x) (x).begin(), (x).end() using namespace std; ll minimalizuj(ll a, ll b, vector<ll> &t) { for(ll i = t.size()-1; i>=0; i--) if(t[i] <= a && t[i] <= b) { ll ile_a = a/t[i], ile_b = b/t[i]; return ile_a * ile_b + minimalizuj(a-ile_a*t[i], b-ile_b*t[i], t) + minimalizuj(ile_a*t[i], b-ile_b*t[i], t) + minimalizuj(a-ile_a*t[i], ile_b*t[i], t); } return 0; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll a, b, n; cin>>a>>b>>n; vector<ll> t(n); for(auto &x:t) cin>>x; if(a%t[0] || b%t[0]) { cout<<-1; return 0; } cout<<minimalizuj(a, b, t); } |