#include <iostream>
#include <string>
#include <bitset>
#include <utility>
#include <vector>
#include <array>
#include <map>
#include <algorithm>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <memory>
using ll = long long int;
ll solve_line(int h, int w, std::vector<int> const& images, int end);
ll solve(int h, int w, std::vector<int> const& images, int end);
ll solve_line(int h, int w, std::vector<int> const& images, int end)
{
int value = images.at(end);
int count = w / value;
int rest = w % value;
return count + solve(std::min(rest, h), std::max(rest, h), images, end-1);
}
ll solve(int h, int w, std::vector<int> const& images, int end)
{
if (h <= 0 || w <= 0 || end < 0)
return 0;
while(images.at(end)>h)
end--;
int value = images.at(end);
int line_count = h / value;
ll tmp = line_count * solve_line(value, w, images, end);
if (h % value == 0)
return tmp;
return tmp + solve(h % value, w, images, end);
}
int main()
{
int h,w;
std::cin >> h >> w;
int n;
std::cin>>n;
std::vector<int> images;
for(int i = 0; i<n; ++i)
{
int tmp;
std::cin >> tmp;
images.push_back(tmp);
}
int smallest = images.front();
if (h % smallest != 0 || w % smallest != 0)
{
std::cout << "-1" << std::endl;
}
else
{
int small = std::min(h,w);
int big = std::max(h,w);
std::cout << solve(small, big, images, images.size() - 1) << std::endl;
}
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 | #include <iostream> #include <string> #include <bitset> #include <utility> #include <vector> #include <array> #include <map> #include <algorithm> #include <deque> #include <unordered_set> #include <unordered_map> #include <memory> using ll = long long int; ll solve_line(int h, int w, std::vector<int> const& images, int end); ll solve(int h, int w, std::vector<int> const& images, int end); ll solve_line(int h, int w, std::vector<int> const& images, int end) { int value = images.at(end); int count = w / value; int rest = w % value; return count + solve(std::min(rest, h), std::max(rest, h), images, end-1); } ll solve(int h, int w, std::vector<int> const& images, int end) { if (h <= 0 || w <= 0 || end < 0) return 0; while(images.at(end)>h) end--; int value = images.at(end); int line_count = h / value; ll tmp = line_count * solve_line(value, w, images, end); if (h % value == 0) return tmp; return tmp + solve(h % value, w, images, end); } int main() { int h,w; std::cin >> h >> w; int n; std::cin>>n; std::vector<int> images; for(int i = 0; i<n; ++i) { int tmp; std::cin >> tmp; images.push_back(tmp); } int smallest = images.front(); if (h % smallest != 0 || w % smallest != 0) { std::cout << "-1" << std::endl; } else { int small = std::min(h,w); int big = std::max(h,w); std::cout << solve(small, big, images, images.size() - 1) << std::endl; } return 0; } |
English