#include <bits/stdc++.h>
using namespace std;
#define ll long long
template<class T>
int ssize(T &c)
{
return (int)c.size();
}
template<class T>
istream &operator>>(istream &is, vector<T> &vec)
{
for (auto &v : vec)
is >> v;
return is;
}
template<class T>
auto operator<<(ostream &os, T &x)->decltype(x.end(), os);
template<class A, class B>
auto &operator<<(ostream &o, pair<A,B> &x)
{
return o << "(" << x.first << " " << x.second << ")";
}
template<class T>
auto operator<<(ostream &os, T &x)->decltype(x.end(), os)
{
os << "{";
int i = 2;
for(auto &e : x)
os << (", ") + i << e, i = 0;
return os << "}";
}
auto &operator<<(ostream &os, string &x)
{
return os << x.c_str();
}
#ifdef DEBUG
#define LOG(x...) cerr << "["#x"]: ", [](auto... e){ ((cerr << e << "; "), ...) << "\n"; }(x)
#else
#define LOG(x...) 0
#endif
#define written_by return
#define efindus
#define in_2024 0
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
int h, w;
cin >> h >> w;
if (h > w)
swap(h, w);
int n;
cin >> n;
vector<int> sizes_pre(n);
cin >> sizes_pre;
set<int> sizes(sizes_pre.begin(), sizes_pre.end());
if (h % sizes_pre[0] != 0 || w % sizes_pre[0] != 0) {
cout << "-1\n";
return 0;
}
ll pre_res = 0, res = 0;
int h_left = h, w_left = w, c_w = 0;
while (h_left != 0 && w_left != 0) {
int x = *(--sizes.upper_bound(min(h_left, w_left)));
if (h_left == h)
w_left -= x, c_w = x, pre_res = res;
res += (c_w / x) * (h_left / x), h_left %= x;
if (h_left == 0) {
res += (w_left / c_w) * (res - pre_res), w_left %= c_w;
if (w_left != 0)
h_left = h;
}
}
cout << res << "\n";
written_by efindus in_2024;
}
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 | #include <bits/stdc++.h> using namespace std; #define ll long long template<class T> int ssize(T &c) { return (int)c.size(); } template<class T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template<class T> auto operator<<(ostream &os, T &x)->decltype(x.end(), os); template<class A, class B> auto &operator<<(ostream &o, pair<A,B> &x) { return o << "(" << x.first << " " << x.second << ")"; } template<class T> auto operator<<(ostream &os, T &x)->decltype(x.end(), os) { os << "{"; int i = 2; for(auto &e : x) os << (", ") + i << e, i = 0; return os << "}"; } auto &operator<<(ostream &os, string &x) { return os << x.c_str(); } #ifdef DEBUG #define LOG(x...) cerr << "["#x"]: ", [](auto... e){ ((cerr << e << "; "), ...) << "\n"; }(x) #else #define LOG(x...) 0 #endif #define written_by return #define efindus #define in_2024 0 int main() { cin.tie(NULL)->sync_with_stdio(false); int h, w; cin >> h >> w; if (h > w) swap(h, w); int n; cin >> n; vector<int> sizes_pre(n); cin >> sizes_pre; set<int> sizes(sizes_pre.begin(), sizes_pre.end()); if (h % sizes_pre[0] != 0 || w % sizes_pre[0] != 0) { cout << "-1\n"; return 0; } ll pre_res = 0, res = 0; int h_left = h, w_left = w, c_w = 0; while (h_left != 0 && w_left != 0) { int x = *(--sizes.upper_bound(min(h_left, w_left))); if (h_left == h) w_left -= x, c_w = x, pre_res = res; res += (c_w / x) * (h_left / x), h_left %= x; if (h_left == 0) { res += (w_left / c_w) * (res - pre_res), w_left %= c_w; if (w_left != 0) h_left = h; } } cout << res << "\n"; written_by efindus in_2024; } |
English