#include <iostream> #include <vector> #include <algorithm> struct obr { uint32_t x; uint32_t y; std::vector<uint32_t> sizes; }; void load(std::istream& in, obr& o) { in >> o.x; in >> o.y; if (o.x > o.y) { auto tmp = o.y; o.y = o.x; o.x = tmp; } uint32_t num_squares = 0; in >> num_squares; o.sizes.reserve(num_squares); for (uint32_t i = 0; i < num_squares; i++) { uint32_t val; in >> val; o.sizes.insert(o.sizes.begin(), val); } } void print(std::ostream& out, obr& o) { out << o.x << "x" << o.y << "\n"; out << o.sizes.size() << "\n"; uint32_t i = 0; for (auto it = o.sizes.begin(); it != o.sizes.end(); ++it) { out << "[" << i << "] : " << *it << "\n"; i++; } } std::vector<uint32_t>::iterator find_first_lower( std::vector<uint32_t>::iterator &start, std::vector<uint32_t>::iterator &end, uint32_t lower ) { for (auto it = start; it != end; ++it) { if (*it <= lower) { return it; } } return end; } uint32_t count_stripe(std::vector<uint32_t> &sizes, uint32_t dim, std::vector<uint32_t>::iterator &max_it) { uint32_t result = 0; auto start = sizes.begin(); auto end = sizes.end(); auto it = find_first_lower(start, end, dim); if (it == sizes.end()) { return 0; } else { auto factor = 1; auto size = *it; max_it = it; result += factor * (dim / size); auto next_dim = dim % size; while (next_dim != 0) { it = find_first_lower(it, end, next_dim); if (it == sizes.end()) { return 0; } else { auto new_size = *it; factor *= size / new_size; size = new_size; result += factor * (next_dim / size); next_dim = next_dim % size; } } } return result; } bool verify_lowest_div(obr& o) { auto lowest = o.sizes[o.sizes.size()-1]; if ((o.x % lowest != 0) || (o.y % lowest) != 0) { return false; } return true; } void prog_main(std::istream& in, std::ostream& out) { obr o; load(in, o); if (!verify_lowest_div(o)) { out << "-1\n"; return; } std::vector<uint32_t>::iterator it; auto result = count_stripe(o.sizes, o.x, it); if (result == 0) { out << "-1\n"; } else { auto new_dim = o.y % *it; result *= (o.y / *it); while (new_dim > 0 && it != o.sizes.end()) { o.sizes.erase(it); auto new_result = count_stripe(o.sizes, o.x, it); if (new_result == 0 || it == o.sizes.end()) { out << "-1\n"; return; } else { auto newer_dim = new_dim % *it; new_result *= (new_dim / *it); result += new_result; new_dim = newer_dim; } } out << result << std::endl; } } #ifndef TEST int main(int argc, char* argv[]) { prog_main(std::cin, std::cout); return 0; } #endif
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include <iostream> #include <vector> #include <algorithm> struct obr { uint32_t x; uint32_t y; std::vector<uint32_t> sizes; }; void load(std::istream& in, obr& o) { in >> o.x; in >> o.y; if (o.x > o.y) { auto tmp = o.y; o.y = o.x; o.x = tmp; } uint32_t num_squares = 0; in >> num_squares; o.sizes.reserve(num_squares); for (uint32_t i = 0; i < num_squares; i++) { uint32_t val; in >> val; o.sizes.insert(o.sizes.begin(), val); } } void print(std::ostream& out, obr& o) { out << o.x << "x" << o.y << "\n"; out << o.sizes.size() << "\n"; uint32_t i = 0; for (auto it = o.sizes.begin(); it != o.sizes.end(); ++it) { out << "[" << i << "] : " << *it << "\n"; i++; } } std::vector<uint32_t>::iterator find_first_lower( std::vector<uint32_t>::iterator &start, std::vector<uint32_t>::iterator &end, uint32_t lower ) { for (auto it = start; it != end; ++it) { if (*it <= lower) { return it; } } return end; } uint32_t count_stripe(std::vector<uint32_t> &sizes, uint32_t dim, std::vector<uint32_t>::iterator &max_it) { uint32_t result = 0; auto start = sizes.begin(); auto end = sizes.end(); auto it = find_first_lower(start, end, dim); if (it == sizes.end()) { return 0; } else { auto factor = 1; auto size = *it; max_it = it; result += factor * (dim / size); auto next_dim = dim % size; while (next_dim != 0) { it = find_first_lower(it, end, next_dim); if (it == sizes.end()) { return 0; } else { auto new_size = *it; factor *= size / new_size; size = new_size; result += factor * (next_dim / size); next_dim = next_dim % size; } } } return result; } bool verify_lowest_div(obr& o) { auto lowest = o.sizes[o.sizes.size()-1]; if ((o.x % lowest != 0) || (o.y % lowest) != 0) { return false; } return true; } void prog_main(std::istream& in, std::ostream& out) { obr o; load(in, o); if (!verify_lowest_div(o)) { out << "-1\n"; return; } std::vector<uint32_t>::iterator it; auto result = count_stripe(o.sizes, o.x, it); if (result == 0) { out << "-1\n"; } else { auto new_dim = o.y % *it; result *= (o.y / *it); while (new_dim > 0 && it != o.sizes.end()) { o.sizes.erase(it); auto new_result = count_stripe(o.sizes, o.x, it); if (new_result == 0 || it == o.sizes.end()) { out << "-1\n"; return; } else { auto newer_dim = new_dim % *it; new_result *= (new_dim / *it); result += new_result; new_dim = newer_dim; } } out << result << std::endl; } } #ifndef TEST int main(int argc, char* argv[]) { prog_main(std::cin, std::cout); return 0; } #endif |