#include<bits/stdc++.h> using namespace std; #define int long long int w, h, n, ans; int d[35]; bool wa; int cnt(int a, int b){ if(a > b) swap(a,b); int act = 0; //lowerbound a for(int i=n-1; i>=0; i--){ if(d[i] > a) continue; act = d[i]; break; } if(act == 0){ wa = 1; return 0; } int cost = b/act; if(b % act) cost += cnt(act,b%act); int res = a/act * cost; if(a % act) res += cnt(a%act,b); return res; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> w >> h >> n; for(int i=0; i<n; i++) cin >> d[i]; ans = cnt(w, h); if(!wa) cout << ans << '\n'; else cout << -1 << '\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 | #include<bits/stdc++.h> using namespace std; #define int long long int w, h, n, ans; int d[35]; bool wa; int cnt(int a, int b){ if(a > b) swap(a,b); int act = 0; //lowerbound a for(int i=n-1; i>=0; i--){ if(d[i] > a) continue; act = d[i]; break; } if(act == 0){ wa = 1; return 0; } int cost = b/act; if(b % act) cost += cnt(act,b%act); int res = a/act * cost; if(a % act) res += cnt(a%act,b); return res; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> w >> h >> n; for(int i=0; i<n; i++) cin >> d[i]; ans = cnt(w, h); if(!wa) cout << ans << '\n'; else cout << -1 << '\n'; return 0; } |