#include <iostream> #include <algorithm> #include <map> #include <vector> #include <stack> using namespace std; long long sol(vector<long long>& d, long long h, long long w) { if (w == 0 || h == 0) { return 0; } for (auto it = d.rbegin(); it != d.rend(); ++it) { if (*it <= h && *it <= w) { long long a = (w / *it) * sol(d, h % *it, *it); long long b = (h / *it) * sol(d, *it, w % *it); long long c = sol(d, h % *it, w % *it); long long result = (a > -1 && b > -1 && c > -1) ? ((h / *it) * (w / *it)) + a + b + c : -1; return result; } } return -1; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int h, w; cin >> h >> w; int n; cin >> n; vector<long long> d(n); for (int i = 0; i < n; ++i) { cin >> d[i]; } cout << sol(d, h, w) << "\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 | #include <iostream> #include <algorithm> #include <map> #include <vector> #include <stack> using namespace std; long long sol(vector<long long>& d, long long h, long long w) { if (w == 0 || h == 0) { return 0; } for (auto it = d.rbegin(); it != d.rend(); ++it) { if (*it <= h && *it <= w) { long long a = (w / *it) * sol(d, h % *it, *it); long long b = (h / *it) * sol(d, *it, w % *it); long long c = sol(d, h % *it, w % *it); long long result = (a > -1 && b > -1 && c > -1) ? ((h / *it) * (w / *it)) + a + b + c : -1; return result; } } return -1; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int h, w; cin >> h >> w; int n; cin >> n; vector<long long> d(n); for (int i = 0; i < n; ++i) { cin >> d[i]; } cout << sol(d, h, w) << "\n"; return 0; } |