#include <iostream> #include <vector> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; ll n; cin >> n; vector<ll>obr(n); for(int i = 0; i < n; i++) { cin>> obr[i]; } if (h % obr[0] || w % obr[0]) { cout << -1 << "\n"; return 0; } ll res = 0; for (int j = n - 1; j >= 0; j--) { if (h > w) swap(h, w); if (obr[j] > h) continue; ll set = 0; ll loch = h; for(int k = j; k >= 0; k--) { set += (loch / obr[k]) * (obr[j] / obr[k]); loch %= obr[k]; } res += (w / obr[j]) * set; w = w % obr[j]; } cout << res << "\n"; }
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 | #include <iostream> #include <vector> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll h, w; cin >> h >> w; ll n; cin >> n; vector<ll>obr(n); for(int i = 0; i < n; i++) { cin>> obr[i]; } if (h % obr[0] || w % obr[0]) { cout << -1 << "\n"; return 0; } ll res = 0; for (int j = n - 1; j >= 0; j--) { if (h > w) swap(h, w); if (obr[j] > h) continue; ll set = 0; ll loch = h; for(int k = j; k >= 0; k--) { set += (loch / obr[k]) * (obr[j] / obr[k]); loch %= obr[k]; } res += (w / obr[j]) * set; w = w % obr[j]; } cout << res << "\n"; } |