#include <iostream> #include <functional> #include <cstdint> namespace std { template<> struct hash<std::pair<uint64_t, uint64_t>> { size_t operator()(const std::pair<uint64_t, uint64_t>& v) const { auto h1 = std::hash<uint64_t>{}(v.first); auto h2 = std::hash<uint64_t>{}(v.second); return h1 ^ h2; } }; } #include <unordered_map> uint64_t count(uint64_t* tab, uint64_t i, uint64_t width, uint64_t height) { if (width == 0 || height == 0) return 0; static std::unordered_map<std::pair<uint64_t, uint64_t>, uint64_t> map; auto a = std::make_pair(width, height); auto b = std::make_pair(height, width); if (map.find(a) != map.end()) return map[a]; if (map.find(b) != map.end()) return map[b]; uint64_t x = width / tab[i], y = height / tab[i], areaX = x * tab[i], areaY = y * tab[i]; uint64_t total = x * y; if (i != 0) { if (areaX < width) total += count(tab, i - 1, width - areaX, areaY); if (areaY < height) total += count(tab, i - 1, areaX, height - areaY); if (areaX < width && areaY < height) total += count(tab, i - 1, width - areaX, height - areaY); } map[a] = total; return total; } int main() { uint64_t w, h, n; std::cin >> w >> h >> n; uint64_t tab[n]; for (uint64_t i = 0; i < n; ++i) std::cin >> tab[i]; if (w % tab[0] != 0 || h % tab[0] != 0) { std::cout << "-1\n"; return 0; } std::cout << count(tab, n - 1, w, h) << '\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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <iostream> #include <functional> #include <cstdint> namespace std { template<> struct hash<std::pair<uint64_t, uint64_t>> { size_t operator()(const std::pair<uint64_t, uint64_t>& v) const { auto h1 = std::hash<uint64_t>{}(v.first); auto h2 = std::hash<uint64_t>{}(v.second); return h1 ^ h2; } }; } #include <unordered_map> uint64_t count(uint64_t* tab, uint64_t i, uint64_t width, uint64_t height) { if (width == 0 || height == 0) return 0; static std::unordered_map<std::pair<uint64_t, uint64_t>, uint64_t> map; auto a = std::make_pair(width, height); auto b = std::make_pair(height, width); if (map.find(a) != map.end()) return map[a]; if (map.find(b) != map.end()) return map[b]; uint64_t x = width / tab[i], y = height / tab[i], areaX = x * tab[i], areaY = y * tab[i]; uint64_t total = x * y; if (i != 0) { if (areaX < width) total += count(tab, i - 1, width - areaX, areaY); if (areaY < height) total += count(tab, i - 1, areaX, height - areaY); if (areaX < width && areaY < height) total += count(tab, i - 1, width - areaX, height - areaY); } map[a] = total; return total; } int main() { uint64_t w, h, n; std::cin >> w >> h >> n; uint64_t tab[n]; for (uint64_t i = 0; i < n; ++i) std::cin >> tab[i]; if (w % tab[0] != 0 || h % tab[0] != 0) { std::cout << "-1\n"; return 0; } std::cout << count(tab, n - 1, w, h) << '\n'; } |