#include<iostream> #include<algorithm> int arr[35]; int p; long long f(long long a, long long b) { if (a != 0 && b != 0) { int m = std::min(a, b); int M = std::max(a, b); int i = p - 1; while (true) { if (i == -1) { std::cout << -1; exit(0); } if (arr[i] <= m) { break; } i--; } long long s1 = m / arr[i]; long long s2 = M / arr[i]; long long s = s1 * s2; return s + f(m - s1 * arr[i], s2 * arr[i]) + f(M - s2 * arr[i], s1 * arr[i]) + f(M - s2 * arr[i], m - s1 * arr[i]); } else { long long v = 0; return v; } } int main() { long long n, k, a, b, w, h; std::cin >> w >> h >> n; p = n; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } std::cout << f(w, h); }
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 | #include<iostream> #include<algorithm> int arr[35]; int p; long long f(long long a, long long b) { if (a != 0 && b != 0) { int m = std::min(a, b); int M = std::max(a, b); int i = p - 1; while (true) { if (i == -1) { std::cout << -1; exit(0); } if (arr[i] <= m) { break; } i--; } long long s1 = m / arr[i]; long long s2 = M / arr[i]; long long s = s1 * s2; return s + f(m - s1 * arr[i], s2 * arr[i]) + f(M - s2 * arr[i], s1 * arr[i]) + f(M - s2 * arr[i], m - s1 * arr[i]); } else { long long v = 0; return v; } } int main() { long long n, k, a, b, w, h; std::cin >> w >> h >> n; p = n; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } std::cout << f(w, h); } |