#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<ll> VI; typedef vector<char> VC; typedef pair<int, int> PI; ll solve(ll h, ll w, int n, VI &A) { if (w < A[n - 1] || h < A[n - 1]) return -1; if (w % A[n - 1] != 0 || h % A[n - 1] != 0) return -1; ll r = 0; ll h0 = h; for (int i0 = 0; i0 < n; i0++) { ll a0 = A[i0]; if (a0 > h0) continue; ll r0 = 0; ll w0 = w; for (int i1 = i0; i1 < n; i1++) { ll a1 = A[i1]; r0 += (w0 / a1) * (a0 / a1); w0 %= a1; } r += (h0 / a0) * r0; h0 %= a0; } return r; } int main() { ll h, w; cin >> h >> w; int n; cin >> n; VI A(n); for (int i = n - 1; i >= 0; i--) { cin >> A[i]; } cout << solve(h, w, n, A) << 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 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define sn second typedef long long ll; typedef vector<ll> VI; typedef vector<char> VC; typedef pair<int, int> PI; ll solve(ll h, ll w, int n, VI &A) { if (w < A[n - 1] || h < A[n - 1]) return -1; if (w % A[n - 1] != 0 || h % A[n - 1] != 0) return -1; ll r = 0; ll h0 = h; for (int i0 = 0; i0 < n; i0++) { ll a0 = A[i0]; if (a0 > h0) continue; ll r0 = 0; ll w0 = w; for (int i1 = i0; i1 < n; i1++) { ll a1 = A[i1]; r0 += (w0 / a1) * (a0 / a1); w0 %= a1; } r += (h0 / a0) * r0; h0 %= a0; } return r; } int main() { ll h, w; cin >> h >> w; int n; cin >> n; VI A(n); for (int i = n - 1; i >= 0; i--) { cin >> A[i]; } cout << solve(h, w, n, A) << endl; return 0; } |