#include<bits/stdc++.h> #define int long long using namespace std; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h, w, mw, mh; cin >> h >> w; mw = w, mh = h; int n, res =0; cin >> n; vector<int> inp(n+1); inp[0] = 1; for(int i=0; i<n; ++i) cin >> inp[i+1]; if(w%inp[1] >0 || h%inp[1] > 0){ cout << -1; return 0; } int il = 1; for(int i=1, a, b; i<n; ++i){ w/= inp[i]/inp[i-1], h/=inp[i]/inp[i-1]; a = h%(inp[i+1]/inp[i]); b = w%(inp[i+1]/inp[i]); res += h*b + w*a - a*b; il = inp[i]; // cout << h << " " << w << " " << a << " " << b << " "; // cout << res << "\n"; } w/= inp[n]/inp[n-1], h/=inp[n]/inp[n-1]; // cout << h << " " << w << "\n"; cout << res + (h*w) << "\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<bits/stdc++.h> #define int long long using namespace std; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h, w, mw, mh; cin >> h >> w; mw = w, mh = h; int n, res =0; cin >> n; vector<int> inp(n+1); inp[0] = 1; for(int i=0; i<n; ++i) cin >> inp[i+1]; if(w%inp[1] >0 || h%inp[1] > 0){ cout << -1; return 0; } int il = 1; for(int i=1, a, b; i<n; ++i){ w/= inp[i]/inp[i-1], h/=inp[i]/inp[i-1]; a = h%(inp[i+1]/inp[i]); b = w%(inp[i+1]/inp[i]); res += h*b + w*a - a*b; il = inp[i]; // cout << h << " " << w << " " << a << " " << b << " "; // cout << res << "\n"; } w/= inp[n]/inp[n-1], h/=inp[n]/inp[n-1]; // cout << h << " " << w << "\n"; cout << res + (h*w) << "\n"; } |