#include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<ll> vec; ll h, w; ll ans = 0; ll solve(int ind){ ll wys = vec[ind]; ll res = 0; ll sz = w; for(int j = ind; j >= 0; j--){ if(vec[j] <= sz){ ll ile = (ll)((ll)wys / vec[j]) * ((ll)sz / vec[j]); res += ile; sz = (ll)sz - (ll)vec[j] * ((ll)sz / vec[j]); } } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> h >> w; cin >> n; vec.resize(n); for(int i = 0; i < n; i++){ cin >> vec[i]; } if(h % vec[0] != 0 || w % vec[0] != 0){ cout << -1 << '\n'; return 0; } for(int i = n - 1; i >= 0; i--){ if(vec[i] <= h){ ll ile = (ll)h / vec[i]; ll cntOne = solve(i); h = (ll)h - (ll)ile * vec[i]; ans = (ll)ans + (ll)cntOne * ile; } } cout << ans << '\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 44 45 46 47 48 49 50 51 52 53 | #include <bits/stdc++.h> using namespace std; using ll = long long; int n; vector<ll> vec; ll h, w; ll ans = 0; ll solve(int ind){ ll wys = vec[ind]; ll res = 0; ll sz = w; for(int j = ind; j >= 0; j--){ if(vec[j] <= sz){ ll ile = (ll)((ll)wys / vec[j]) * ((ll)sz / vec[j]); res += ile; sz = (ll)sz - (ll)vec[j] * ((ll)sz / vec[j]); } } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> h >> w; cin >> n; vec.resize(n); for(int i = 0; i < n; i++){ cin >> vec[i]; } if(h % vec[0] != 0 || w % vec[0] != 0){ cout << -1 << '\n'; return 0; } for(int i = n - 1; i >= 0; i--){ if(vec[i] <= h){ ll ile = (ll)h / vec[i]; ll cntOne = solve(i); h = (ll)h - (ll)ile * vec[i]; ans = (ll)ans + (ll)cntOne * ile; } } cout << ans << '\n'; return 0; } |