#include <bits/stdc++.h> using namespace std; typedef long long ll; ll h, w, n; ll H, W, ile; ll d[30]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> d[n - 1 - i]; for (int i = 0; i < n; i++) { ll ileH = (h / d[i]) * d[i]; ll ileW = (w / d[i]) * d[i]; ile += (ileH * ileW - H * W) / (d[i] * d[i]); H = ileH; W = ileW; //cout << H << ' ' << W << ' ' << ile << '\n'; } if (H == h && W == w) cout << ile; else cout << -1; 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; ll h, w, n; ll H, W, ile; ll d[30]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> d[n - 1 - i]; for (int i = 0; i < n; i++) { ll ileH = (h / d[i]) * d[i]; ll ileW = (w / d[i]) * d[i]; ile += (ileH * ileW - H * W) / (d[i] * d[i]); H = ileH; W = ileW; //cout << H << ' ' << W << ' ' << ile << '\n'; } if (H == h && W == w) cout << ile; else cout << -1; return 0; } |