#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long ll;
const int N = 2e2 + 10;
const int P = 101;
const ll MLL = 1e18;
const ll mod = 998244353;
const ll LOG = 31;
vector<ll> v;
ll h , w;
int n;
ll ans = 0; // robimy lewa i prawa a potem rekursywnie do przedialu na dole
bool ne() {
while(v.size() > 0 && v[v.size() - 1] > min(h , w)) v.pop_back();
if(v.size() == 0) {
return false;
}
n = v.size();
ll last = v[n - 1];
ans += (h / last) * (w / last);
ll hfil = h - (h % last), wfil = w - (w % last);
ll hmod = h % last , wmod = w % last;
for(int i = n - 2; i >= 0; i--) {
if(v[i] > hmod) continue;
ll use = hmod / v[i];
hmod %= v[i];
ans += wfil/v[i] * use;
}
for(int i = n - 2; i >= 0; i--) {
if(v[i] > wmod) continue;
ll use = wmod / v[i];
wmod %= v[i];
ans += hfil/v[i] * use;
}
if(wmod != 0 || hmod != 0) {
return false;
}
h = h % last , w = w % last;
return true;
}
void solve() {
cin >> h >> w;
cin >> n;
v.resize(n);
for(int i =0 ; i < n; i++) cin >> v[i];
bool ok = true;
while(w != 0 && h != 0) {
bool f = ne();
if(!f) {
ok = false;
break;
}
}
if(ok) cout << ans << endl;
else cout << -1 << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <iostream> #include <vector> #include <algorithm> #include <math.h> #include <set> #include <map> #include <stack> #include <queue> #include <iomanip> using namespace std; typedef long long ll; const int N = 2e2 + 10; const int P = 101; const ll MLL = 1e18; const ll mod = 998244353; const ll LOG = 31; vector<ll> v; ll h , w; int n; ll ans = 0; // robimy lewa i prawa a potem rekursywnie do przedialu na dole bool ne() { while(v.size() > 0 && v[v.size() - 1] > min(h , w)) v.pop_back(); if(v.size() == 0) { return false; } n = v.size(); ll last = v[n - 1]; ans += (h / last) * (w / last); ll hfil = h - (h % last), wfil = w - (w % last); ll hmod = h % last , wmod = w % last; for(int i = n - 2; i >= 0; i--) { if(v[i] > hmod) continue; ll use = hmod / v[i]; hmod %= v[i]; ans += wfil/v[i] * use; } for(int i = n - 2; i >= 0; i--) { if(v[i] > wmod) continue; ll use = wmod / v[i]; wmod %= v[i]; ans += hfil/v[i] * use; } if(wmod != 0 || hmod != 0) { return false; } h = h % last , w = w % last; return true; } void solve() { cin >> h >> w; cin >> n; v.resize(n); for(int i =0 ; i < n; i++) cin >> v[i]; bool ok = true; while(w != 0 && h != 0) { bool f = ne(); if(!f) { ok = false; break; } } if(ok) cout << ans << endl; else cout << -1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); return 0; } |
English