#include <bits/stdc++.h> using namespace std; int main() { long long h, w, n, o, a, b; cin >> h >> w >> n; vector<long long> obr; for(int i = 0; i < n; i++) { cin >> o; obr.push_back(o); } long long hz = h; long long wz = w; long long ans = 0; long long ktore = n-1; if(h%obr[0] != 0 || w%obr[0] != 0) { cout << -1 << '\n'; return 0; } //cout << h << w << obr[0]; while(obr[ktore]>h || obr[ktore]>w) { ktore--; } ans+=(h/obr[ktore])*(w/obr[ktore]); hz=h%obr[ktore]; wz=w%obr[ktore]; ktore--; while(hz>0 || wz>0) { a = hz/obr[ktore]; b = wz/obr[ktore]; ans+= a*(w-wz)/obr[ktore] + b*(h-hz)/obr[ktore] + a*b; wz-=b*obr[ktore]; hz-=a*obr[ktore]; ktore--; } cout << ans << "\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 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <bits/stdc++.h> using namespace std; int main() { long long h, w, n, o, a, b; cin >> h >> w >> n; vector<long long> obr; for(int i = 0; i < n; i++) { cin >> o; obr.push_back(o); } long long hz = h; long long wz = w; long long ans = 0; long long ktore = n-1; if(h%obr[0] != 0 || w%obr[0] != 0) { cout << -1 << '\n'; return 0; } //cout << h << w << obr[0]; while(obr[ktore]>h || obr[ktore]>w) { ktore--; } ans+=(h/obr[ktore])*(w/obr[ktore]); hz=h%obr[ktore]; wz=w%obr[ktore]; ktore--; while(hz>0 || wz>0) { a = hz/obr[ktore]; b = wz/obr[ktore]; ans+= a*(w-wz)/obr[ktore] + b*(h-hz)/obr[ktore] + a*b; wz-=b*obr[ktore]; hz-=a*obr[ktore]; ktore--; } cout << ans << "\n"; } |