#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll h, w, n, d[37]; ll policz(ll h, ll w, ll n) { if (h == 0 || w == 0) return 0; while (n >= 1 && (d[n] > h || d[n] > w)) n--; if (n == 0) return -1; ll x = h / d[n]; ll xr = h - (x * d[n]); ll y = w / d[n]; ll yr = w - (y * d[n]); ll s1 = policz(xr, yr, n); ll s2 = policz(xr, w - yr, n); ll s3 = policz(h - xr, yr, n); if (s1 == -1 || s2 == -1 || s3 == -1) return -1; return x * y + s1 + s2 + s3; } int main () { boost(); cin >> h >> w; cin >> n; for (int i = 1; i <= n; i++) { cin >> d[i]; } cout << policz(h, w, n) << "\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 54 | #include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair <int, int>; using pll = pair <ll, ll>; const int inf = 1e9+7; const ll inf_ll = 1e18+7; void boost () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll h, w, n, d[37]; ll policz(ll h, ll w, ll n) { if (h == 0 || w == 0) return 0; while (n >= 1 && (d[n] > h || d[n] > w)) n--; if (n == 0) return -1; ll x = h / d[n]; ll xr = h - (x * d[n]); ll y = w / d[n]; ll yr = w - (y * d[n]); ll s1 = policz(xr, yr, n); ll s2 = policz(xr, w - yr, n); ll s3 = policz(h - xr, yr, n); if (s1 == -1 || s2 == -1 || s3 == -1) return -1; return x * y + s1 + s2 + s3; } int main () { boost(); cin >> h >> w; cin >> n; for (int i = 1; i <= n; i++) { cin >> d[i]; } cout << policz(h, w, n) << "\n"; return 0; } |