#include <iostream> #include <vector> #include <algorithm> using ll = long long; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); ll h, w; std::cin >> h >> w; int n; std::cin >> n; std::vector<ll> D(n+1); D[0] = 1; for (int i = 1; i <= n; ++i) { std::cin >> D[i]; } if (w % D[1] != 0 || h % D[1] != 0) { std::cout << "-1\n"; return 0; } std::vector<ll> DD(n); for (int i = 0; i < n; ++i) { DD[i] = D[i+1] / D[i]; } // std::cout << "DD:"; // for (ll x : DD) std::cout << ' ' << x; // std::cout << '\n'; ll ans = 0; for (int i = 0; i < n; ++i) { ll dw = w - w / DD[i] * DD[i]; ll dh = h - h / DD[i] * DD[i]; // std::cout << "w="<<w<<", h="<<h<<", dw=" << dw << ", dh=" << dh << '\n'; ans += w * dh + h * dw - dw * dh; w /= DD[i]; h /= DD[i]; } ans += w * h; std::cout << ans << '\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 45 46 | #include <iostream> #include <vector> #include <algorithm> using ll = long long; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); ll h, w; std::cin >> h >> w; int n; std::cin >> n; std::vector<ll> D(n+1); D[0] = 1; for (int i = 1; i <= n; ++i) { std::cin >> D[i]; } if (w % D[1] != 0 || h % D[1] != 0) { std::cout << "-1\n"; return 0; } std::vector<ll> DD(n); for (int i = 0; i < n; ++i) { DD[i] = D[i+1] / D[i]; } // std::cout << "DD:"; // for (ll x : DD) std::cout << ' ' << x; // std::cout << '\n'; ll ans = 0; for (int i = 0; i < n; ++i) { ll dw = w - w / DD[i] * DD[i]; ll dh = h - h / DD[i] * DD[i]; // std::cout << "w="<<w<<", h="<<h<<", dw=" << dw << ", dh=" << dh << '\n'; ans += w * dh + h * dw - dw * dh; w /= DD[i]; h /= DD[i]; } ans += w * h; std::cout << ans << '\n'; } |