#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll h, w; cin >> h >> w; int n; cin >> n; vector<ll> sizes(n); for(int i = 0; i < n; i++) cin >> sizes[i]; if(h % sizes[0] == 0 && w % sizes[0] == 0) { ll cnt = 0; ll usedh = 0, usedw = 0; for(int i = n - 1; i >= 0; i--) { cnt += (h/sizes[i]) * (w/sizes[i]) - (usedh/sizes[i]) * (usedw/sizes[i]); usedh += ((h - usedh) / sizes[i]) * sizes[i]; usedw += ((w - usedw) / sizes[i]) * sizes[i]; } cout << cnt << '\n'; } else cout << "-1\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll h, w; cin >> h >> w; int n; cin >> n; vector<ll> sizes(n); for(int i = 0; i < n; i++) cin >> sizes[i]; if(h % sizes[0] == 0 && w % sizes[0] == 0) { ll cnt = 0; ll usedh = 0, usedw = 0; for(int i = n - 1; i >= 0; i--) { cnt += (h/sizes[i]) * (w/sizes[i]) - (usedh/sizes[i]) * (usedw/sizes[i]); usedh += ((h - usedh) / sizes[i]) * sizes[i]; usedw += ((w - usedw) / sizes[i]) * sizes[i]; } cout << cnt << '\n'; } else cout << "-1\n"; } |