#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll obr[32];
void sol(ll h, ll w, ll pos, ll &odp) {
if(h <= 0 || w <= 0 || !pos) return;
while(pos >= 1 && (!(h/obr[pos]) || !(w/obr[pos]))) pos--;
ll h1 = h/obr[pos], w1 = w/obr[pos];
if(!h1 || !w1) return;
odp += h1*w1;
sol(h-h1*obr[pos], w1*obr[pos], pos, odp);
sol(h, w-w1*obr[pos], pos, odp);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n, h, w;
cin >> h >> w;
cin >> n;
for(ll i = 1; i <= n; ++i) cin >> obr[i];
if(h%obr[1] || w%obr[1]) {
cout << -1;
return 0;
}
ll odp = 0;
sol(h, w, n, odp);
cout << odp;
}
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 | #include<bits/stdc++.h> using namespace std; using ll = long long; ll obr[32]; void sol(ll h, ll w, ll pos, ll &odp) { if(h <= 0 || w <= 0 || !pos) return; while(pos >= 1 && (!(h/obr[pos]) || !(w/obr[pos]))) pos--; ll h1 = h/obr[pos], w1 = w/obr[pos]; if(!h1 || !w1) return; odp += h1*w1; sol(h-h1*obr[pos], w1*obr[pos], pos, odp); sol(h, w-w1*obr[pos], pos, odp); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, h, w; cin >> h >> w; cin >> n; for(ll i = 1; i <= n; ++i) cin >> obr[i]; if(h%obr[1] || w%obr[1]) { cout << -1; return 0; } ll odp = 0; sol(h, w, n, odp); cout << odp; } |
English