#include <iostream> #define int long long using namespace std; int n, t[35]; int f(int a, int b, int i) { if (a == 0 || b == 0) return 0; for (; i >= 0; i--) if (t[i] <= a && t[i] <= b) return (a / t[i]) * (b / t[i]) + f(a - a % t[i], b % t[i], i - 1) + f(a % t[i], b, i - 1); cout << -1; exit(0); } int32_t main() { int h, w; cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> t[i]; cout << f(h, w, n - 1); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #define int long long using namespace std; int n, t[35]; int f(int a, int b, int i) { if (a == 0 || b == 0) return 0; for (; i >= 0; i--) if (t[i] <= a && t[i] <= b) return (a / t[i]) * (b / t[i]) + f(a - a % t[i], b % t[i], i - 1) + f(a % t[i], b, i - 1); cout << -1; exit(0); } int32_t main() { int h, w; cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> t[i]; cout << f(h, w, n - 1); } |