#include<bits/stdc++.h> using namespace std; vector<long long> dimension; long long solve(long long w, long long h, int lvl){ while(min(w, h) < dimension[lvl]){ lvl--; if(lvl == -1){ return -1; } } long long w_mod = w/dimension[lvl]; long long h_mod = h/dimension[lvl]; long long ans = w_mod * h_mod; if(w%dimension[lvl] != 0){ long long tmp = solve(w%dimension[lvl], dimension[lvl] * h_mod, lvl); if(tmp == -1){ return -1; } ans += tmp; } if(h%dimension[lvl] != 0){ long long tmp = solve(w, h%dimension[lvl], lvl); if(tmp == -1){ return -1; } ans += tmp; } return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int w, h; cin >> w >> h; int n; cin >> n; for(int i = 0; i < n; i++){ int a; cin >> a; dimension.push_back(a); } long long ans = solve(w, h, n - 1); cout << ans << '\n'; 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 | #include<bits/stdc++.h> using namespace std; vector<long long> dimension; long long solve(long long w, long long h, int lvl){ while(min(w, h) < dimension[lvl]){ lvl--; if(lvl == -1){ return -1; } } long long w_mod = w/dimension[lvl]; long long h_mod = h/dimension[lvl]; long long ans = w_mod * h_mod; if(w%dimension[lvl] != 0){ long long tmp = solve(w%dimension[lvl], dimension[lvl] * h_mod, lvl); if(tmp == -1){ return -1; } ans += tmp; } if(h%dimension[lvl] != 0){ long long tmp = solve(w, h%dimension[lvl], lvl); if(tmp == -1){ return -1; } ans += tmp; } return ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int w, h; cin >> w >> h; int n; cin >> n; for(int i = 0; i < n; i++){ int a; cin >> a; dimension.push_back(a); } long long ans = solve(w, h, n - 1); cout << ans << '\n'; return 0; } |