#include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll h, w, n; cin >> h >> w >> n; vector<ll> d(n); for (int i=n-1; i>=0; i--) cin >> d[i]; //niemozliwe if (h % d[n-1] != 0 || w % d[n-1] != 0){ cout << -1 << '\n'; return 0; } //zachlanne upychanie ll left = w, res = 0; while (left > 0){ ll x, y, ind; for (int i=0; i<n; i++){ //cout << i << ": " << d[i] << ' ' << left / d[i] << ' ' << h / d[i] << '\n'; if (left / d[i] > 0 && h / d[i] > 0){ x = left / d[i]; y = h / d[i]; res += x * y; left %= d[i]; ind = i; break; } } x *= d[ind]; y = h - y * d[ind]; //cout << x << ' ' << y << ' ' << ind << " -------\n"; ind++; while (y > 0 && ind < n){ //cout << y << ": " << ind << ' ' << d[ind] << '\n'; ll cur_y; if (d[ind] <= y){ cur_y = y / d[ind]; res += cur_y * (x / d[ind]); y -= cur_y * d[ind]; } ind++; //cout << "cur: " << cur_y << '\n'; } } cout << res << '\n'; return 0; } /* 6 10 3 1 3 6 7 6 3 1 2 4 */
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 | #include <bits/stdc++.h> #define fi first #define se second using namespace std; typedef long long ll; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll h, w, n; cin >> h >> w >> n; vector<ll> d(n); for (int i=n-1; i>=0; i--) cin >> d[i]; //niemozliwe if (h % d[n-1] != 0 || w % d[n-1] != 0){ cout << -1 << '\n'; return 0; } //zachlanne upychanie ll left = w, res = 0; while (left > 0){ ll x, y, ind; for (int i=0; i<n; i++){ //cout << i << ": " << d[i] << ' ' << left / d[i] << ' ' << h / d[i] << '\n'; if (left / d[i] > 0 && h / d[i] > 0){ x = left / d[i]; y = h / d[i]; res += x * y; left %= d[i]; ind = i; break; } } x *= d[ind]; y = h - y * d[ind]; //cout << x << ' ' << y << ' ' << ind << " -------\n"; ind++; while (y > 0 && ind < n){ //cout << y << ": " << ind << ' ' << d[ind] << '\n'; ll cur_y; if (d[ind] <= y){ cur_y = y / d[ind]; res += cur_y * (x / d[ind]); y -= cur_y * d[ind]; } ind++; //cout << "cur: " << cur_y << '\n'; } } cout << res << '\n'; return 0; } /* 6 10 3 1 3 6 7 6 3 1 2 4 */ |