#include <algorithm> #include <iostream> #include <vector> typedef std::vector<std::vector<long long>> Matrix; typedef std::vector<int> Sizes; std::vector<long long> getBlocksPerSize(const Sizes &sizes, size_t start, long long space) { std::vector<long long> blocksPerSize(sizes.size(), 0); for (auto i = start; i < sizes.size(); i++) { long long size = sizes[i]; if (size <= space) { blocksPerSize[i] = space / size; space = space % size; } } return blocksPerSize; } Matrix buildMatrix(const Sizes &sizes, long long w) { Matrix matrix; for (auto i = 0; i < sizes.size(); i++) { matrix.push_back(getBlocksPerSize(sizes, i, w)); } return matrix; } std::vector<long long> calculateBlocksCountsPerSize(const Matrix &matrix, const Sizes &sizes) { std::vector<long long> blockCountsPerSize; for (auto i = 0; i < sizes.size(); i++) { auto baseSize = sizes[i]; long long count = 0; for (auto j = 0; j < matrix.size(); j++) { auto currentSize = sizes[j]; count += matrix[i][j] * (baseSize / currentSize); } blockCountsPerSize.push_back(count); } return blockCountsPerSize; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); long long h, w, n; std::cin >> h >> w >> n; Sizes sizes; sizes.reserve(n); for (auto i = 0; i < n; i++) { int size; std::cin >> size; if (size <= std::min(w, h)) { sizes.push_back(size); } } if (h % sizes[0] != 0 || w % sizes[0] != 0) { std::cout << "-1\n"; return 0; } std::reverse(sizes.begin(), sizes.end()); auto shorterMatrix = buildMatrix(sizes, std::min(w, h)); auto longerMatrix = buildMatrix(sizes, std::max(w, h)); auto shorterBlockCounts = calculateBlocksCountsPerSize(shorterMatrix, sizes); auto longerBlockCounts = calculateBlocksCountsPerSize(longerMatrix, sizes); auto strategyA = getBlocksPerSize(sizes, 0, std::max(w, h)); auto strategyB = getBlocksPerSize(sizes, 0, std::min(w, h)); long long resultA = 0; long long resultB = 0; for (auto i = 0; i < sizes.size(); i++) { auto count = shorterBlockCounts[i]; resultA += strategyA[i] * count; } for (auto i = 0; i < sizes.size(); i++) { auto count = longerBlockCounts[i]; resultB += strategyB[i] * count; } std::cout << std::min(resultA, resultB) << "\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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <algorithm> #include <iostream> #include <vector> typedef std::vector<std::vector<long long>> Matrix; typedef std::vector<int> Sizes; std::vector<long long> getBlocksPerSize(const Sizes &sizes, size_t start, long long space) { std::vector<long long> blocksPerSize(sizes.size(), 0); for (auto i = start; i < sizes.size(); i++) { long long size = sizes[i]; if (size <= space) { blocksPerSize[i] = space / size; space = space % size; } } return blocksPerSize; } Matrix buildMatrix(const Sizes &sizes, long long w) { Matrix matrix; for (auto i = 0; i < sizes.size(); i++) { matrix.push_back(getBlocksPerSize(sizes, i, w)); } return matrix; } std::vector<long long> calculateBlocksCountsPerSize(const Matrix &matrix, const Sizes &sizes) { std::vector<long long> blockCountsPerSize; for (auto i = 0; i < sizes.size(); i++) { auto baseSize = sizes[i]; long long count = 0; for (auto j = 0; j < matrix.size(); j++) { auto currentSize = sizes[j]; count += matrix[i][j] * (baseSize / currentSize); } blockCountsPerSize.push_back(count); } return blockCountsPerSize; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); long long h, w, n; std::cin >> h >> w >> n; Sizes sizes; sizes.reserve(n); for (auto i = 0; i < n; i++) { int size; std::cin >> size; if (size <= std::min(w, h)) { sizes.push_back(size); } } if (h % sizes[0] != 0 || w % sizes[0] != 0) { std::cout << "-1\n"; return 0; } std::reverse(sizes.begin(), sizes.end()); auto shorterMatrix = buildMatrix(sizes, std::min(w, h)); auto longerMatrix = buildMatrix(sizes, std::max(w, h)); auto shorterBlockCounts = calculateBlocksCountsPerSize(shorterMatrix, sizes); auto longerBlockCounts = calculateBlocksCountsPerSize(longerMatrix, sizes); auto strategyA = getBlocksPerSize(sizes, 0, std::max(w, h)); auto strategyB = getBlocksPerSize(sizes, 0, std::min(w, h)); long long resultA = 0; long long resultB = 0; for (auto i = 0; i < sizes.size(); i++) { auto count = shorterBlockCounts[i]; resultA += strategyA[i] * count; } for (auto i = 0; i < sizes.size(); i++) { auto count = longerBlockCounts[i]; resultB += strategyB[i] * count; } std::cout << std::min(resultA, resultB) << "\n"; return 0; } |