#include <bits/stdc++.h> using namespace std; typedef int64_t i64; void solve() { i64 h, w, n; cin >> h >> w >> n; vector<i64> d(n); for (i64 i = 0; i < n; i++) { cin >> d[i]; } if (h % d[0] != 0 || w % d[0] != 0) { cout << -1 << "\n"; return; } i64 ans = 0; i64 i = n - 1; i64 tmpH = h; while (tmpH > 0) { while (d[i] > tmpH || d[i] > w) { i--; } i64 tmpAns = 0; i64 j = i; i64 tmpW = w; i64 mult = 1; while (tmpW > 0) { tmpAns += (tmpW / d[j]) * mult; tmpW %= d[j]; j--; if (j >= 0) { mult *= d[j + 1] / d[j]; } } ans += (tmpH / d[i]) * tmpAns; tmpH %= d[i]; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 | #include <bits/stdc++.h> using namespace std; typedef int64_t i64; void solve() { i64 h, w, n; cin >> h >> w >> n; vector<i64> d(n); for (i64 i = 0; i < n; i++) { cin >> d[i]; } if (h % d[0] != 0 || w % d[0] != 0) { cout << -1 << "\n"; return; } i64 ans = 0; i64 i = n - 1; i64 tmpH = h; while (tmpH > 0) { while (d[i] > tmpH || d[i] > w) { i--; } i64 tmpAns = 0; i64 j = i; i64 tmpW = w; i64 mult = 1; while (tmpW > 0) { tmpAns += (tmpW / d[j]) * mult; tmpW %= d[j]; j--; if (j >= 0) { mult *= d[j + 1] / d[j]; } } ans += (tmpH / d[i]) * tmpAns; tmpH %= d[i]; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |