#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define st first
#define nd second
void TEST([[maybe_unused]] int testcase_i) {
int h, w, n;
cin >> h >> w >> n;
vector<int> v(n);
rep(i, n) {
cin >> v[i];
}
if (h % v[0] or w % v[0]) {
cout << -1;
return;
}
reverse(v.begin(), v.end());
ll ans = 0;
vector<pair<int, int>> pieces{{h, w}};
while (pieces.size()) {
auto [ph, pw] = pieces.back();
pieces.pop_back();
if (ph == 0 or pw == 0) {
continue;
}
rep(i, n) {
if (min(ph, pw) < v[i]) {
continue;
}
ans += (ll)(ph / v[i]) * (ll)(pw / v[i]);
pieces.push_back({ph, pw % v[i]});
pieces.push_back({ph % v[i], (pw / v[i]) * v[i]});
break;
}
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << setprecision(20) << fixed;
// srand(time(NULL));
int t = 1;
// cin >> t;
rep(i, t) {
TEST(i);
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, j) for (int i = 0; i < (int)(j); i++) #define st first #define nd second void TEST([[maybe_unused]] int testcase_i) { int h, w, n; cin >> h >> w >> n; vector<int> v(n); rep(i, n) { cin >> v[i]; } if (h % v[0] or w % v[0]) { cout << -1; return; } reverse(v.begin(), v.end()); ll ans = 0; vector<pair<int, int>> pieces{{h, w}}; while (pieces.size()) { auto [ph, pw] = pieces.back(); pieces.pop_back(); if (ph == 0 or pw == 0) { continue; } rep(i, n) { if (min(ph, pw) < v[i]) { continue; } ans += (ll)(ph / v[i]) * (ll)(pw / v[i]); pieces.push_back({ph, pw % v[i]}); pieces.push_back({ph % v[i], (pw / v[i]) * v[i]}); break; } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << setprecision(20) << fixed; // srand(time(NULL)); int t = 1; // cin >> t; rep(i, t) { TEST(i); } return 0; } |
English