#include <bits/stdc++.h> using namespace std; const int maxn = 100; int a[maxn]; long long f(int i, int n, int w, int h) { if (i > n) return -1; if (w == 0 || h == 0) return 0; int cur = a[i]; long long ans = 1ll * (w / cur) * (h / cur); long long ans1 = 0, ans2 = 0, ans3 = 0; if (w % cur != 0) ans1 = f(i + 1, n, w % cur, h / cur * cur); if (h % cur != 0) ans2 = f(i + 1, n, w / cur * cur, h % cur); if ((w % cur != 0) && (h % cur != 0)) ans3 = f(i + 1, n, w % cur, h % cur); if (ans1 == -1 || ans2 == -1 || ans3 == -1) return -1; return ans + ans1 + ans2 + ans3; } int main() { ios_base::sync_with_stdio(false); int w, h; cin >> w >> h; int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; reverse(a + 1, a + n + 1); long long ans = f(1, n, w, h); cout << ans << endl; 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 | #include <bits/stdc++.h> using namespace std; const int maxn = 100; int a[maxn]; long long f(int i, int n, int w, int h) { if (i > n) return -1; if (w == 0 || h == 0) return 0; int cur = a[i]; long long ans = 1ll * (w / cur) * (h / cur); long long ans1 = 0, ans2 = 0, ans3 = 0; if (w % cur != 0) ans1 = f(i + 1, n, w % cur, h / cur * cur); if (h % cur != 0) ans2 = f(i + 1, n, w / cur * cur, h % cur); if ((w % cur != 0) && (h % cur != 0)) ans3 = f(i + 1, n, w % cur, h % cur); if (ans1 == -1 || ans2 == -1 || ans3 == -1) return -1; return ans + ans1 + ans2 + ans3; } int main() { ios_base::sync_with_stdio(false); int w, h; cin >> w >> h; int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; reverse(a + 1, a + n + 1); long long ans = f(1, n, w, h); cout << ans << endl; return 0; } |