#include <iostream> #include <vector> #include <algorithm> using namespace std; long long rect(int h, int w, vector<int>& data, int n){ if(h == 0 || w == 0) return 0; while(data[n] > h || data[n] > w) --n; long long ans = (h / data[n]) * (long long) (w / data[n]); ans += rect(h % data[n], (w / data[n]) * data[n], data, n - 1); ans += rect(h, w % data[n], data, n - 1); return ans; } void case1(){ int h, w, n; cin >> h >> w >> n; vector<int> data(n); for(int i = 0; i < n; ++i) cin >> data[i]; if(h % data[0] != 0 || w % data[0] != 0){ cout << "-1\n"; return; } cout << rect(h, w, data, n - 1) << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); case1(); 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; long long rect(int h, int w, vector<int>& data, int n){ if(h == 0 || w == 0) return 0; while(data[n] > h || data[n] > w) --n; long long ans = (h / data[n]) * (long long) (w / data[n]); ans += rect(h % data[n], (w / data[n]) * data[n], data, n - 1); ans += rect(h, w % data[n], data, n - 1); return ans; } void case1(){ int h, w, n; cin >> h >> w >> n; vector<int> data(n); for(int i = 0; i < n; ++i) cin >> data[i]; if(h % data[0] != 0 || w % data[0] != 0){ cout << "-1\n"; return; } cout << rect(h, w, data, n - 1) << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); case1(); return 0; } |