#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 40; ll D[MAXN]; ll solve(ll h, ll w, int n){ if (h % D[0] || w % D[0]){ return ll(-1); } ll ans = 0; int hori = n - 1, diag = n - 1; while (w){ while (D[hori] > w || D[hori] > h){ hori--; } ll cur = D[hori]; ll tmp = (w / cur) * cur; w -= tmp; ans += (tmp / cur) * (h / cur); ll htmp = h - ((h / cur) * cur); for (int i = n - 1; i >= 0; i--){ if (D[i] <= htmp){ ans += (htmp / D[i]) * (tmp / D[i]); htmp %= D[i]; } } } return ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; int n; cin >> n; for (int i = 0; i < n; i++){ cin >> D[i]; } cout << solve(h, w, n) << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 40; ll D[MAXN]; ll solve(ll h, ll w, int n){ if (h % D[0] || w % D[0]){ return ll(-1); } ll ans = 0; int hori = n - 1, diag = n - 1; while (w){ while (D[hori] > w || D[hori] > h){ hori--; } ll cur = D[hori]; ll tmp = (w / cur) * cur; w -= tmp; ans += (tmp / cur) * (h / cur); ll htmp = h - ((h / cur) * cur); for (int i = n - 1; i >= 0; i--){ if (D[i] <= htmp){ ans += (htmp / D[i]) * (tmp / D[i]); htmp %= D[i]; } } } return ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; int n; cin >> n; for (int i = 0; i < n; i++){ cin >> D[i]; } cout << solve(h, w, n) << "\n"; } |