#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=30; int n; ll arr[MAXN+5]; ll ans=0; bool f(ll a, ll b){ if(a==0 || b==0) return 1; if(a > b) swap(a, b); int i=n; ll na=a, nb=-1; ll cb=-1; for(i;i>=1;i--){ if(arr[i] > a) continue; nb = (b / arr[i]) * arr[i]; cb = b % arr[i]; break; } for(i;i>=1;i--){ if(arr[i] > na) continue; ans += (na / arr[i]) * (nb / arr[i]); na %= arr[i]; } if(na != 0){ return 0; } if(!f(a, cb)) return 0; return 1; } void solve() { ll h,w; cin >> h >> w; cin >> n; for(int i=1;i<=n;i++){ cin >> arr[i]; } if(!f(h, w)) cout << -1; else cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=30; int n; ll arr[MAXN+5]; ll ans=0; bool f(ll a, ll b){ if(a==0 || b==0) return 1; if(a > b) swap(a, b); int i=n; ll na=a, nb=-1; ll cb=-1; for(i;i>=1;i--){ if(arr[i] > a) continue; nb = (b / arr[i]) * arr[i]; cb = b % arr[i]; break; } for(i;i>=1;i--){ if(arr[i] > na) continue; ans += (na / arr[i]) * (nb / arr[i]); na %= arr[i]; } if(na != 0){ return 0; } if(!f(a, cb)) return 0; return 1; } void solve() { ll h,w; cin >> h >> w; cin >> n; for(int i=1;i<=n;i++){ cin >> arr[i]; } if(!f(h, w)) cout << -1; else cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |