#include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int INF = 1'000'000'000'000'000'010; vector<int> v; int cnt = 0; int f(int n, int m, int I) { cnt++; // cerr << "f: " << n << " " << m << " " << I << "\n"; if(n==0||m==0) return 0; if(I==sz(v)) { return INF; } int val = v[I]; if(val>n||val>m) return f(n, m, I+1); int cur = 0; cur += (n/val)*(m/val); cur += f(n, m%val, I+1); cur += f(n%val, m/val*val, I+1); return min(cur, INF); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int x; cin >> x; v.resize(x); for(int i=0;i<x;i++) { cin >> v[i]; } sort(all(v)); reverse(all(v)); int ans = f(n, m, 0); if(ans==INF) { cout << "-1\n"; } else { cout << ans << "\n"; } // cout << cnt << "\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 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include<bits/stdc++.h> using namespace std; #define int int64_t #define pb push_back #define pii pair<int, int> #define st first #define nd second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() const int INF = 1'000'000'000'000'000'010; vector<int> v; int cnt = 0; int f(int n, int m, int I) { cnt++; // cerr << "f: " << n << " " << m << " " << I << "\n"; if(n==0||m==0) return 0; if(I==sz(v)) { return INF; } int val = v[I]; if(val>n||val>m) return f(n, m, I+1); int cur = 0; cur += (n/val)*(m/val); cur += f(n, m%val, I+1); cur += f(n%val, m/val*val, I+1); return min(cur, INF); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int x; cin >> x; v.resize(x); for(int i=0;i<x;i++) { cin >> v[i]; } sort(all(v)); reverse(all(v)); int ans = f(n, m, 0); if(ans==INF) { cout << "-1\n"; } else { cout << ans << "\n"; } // cout << cnt << "\n"; } |