#include<bits/stdc++.h> using namespace std; vector<int>d; long long solve(long long h, long long w){ if(h*w==0)return 0; for(auto x : d){ if((h/x)*(w/x) == 0)continue; return (h/x)*(w/x) + solve(h-(h%x), w%x) + solve(h%x, w-(w%x)) + solve(h%x, w%x); //return (h/x)*(w/x) + min(solve(h, w%x) + solve(h%x, w-(w%x)), solve(h%x, w) + solve(h-(h%x), w%x)); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h, w; cin >> h >> w; int n; cin >> n; d.resize(n); for(auto &x : d)cin >> x; if(h%d[0] != 0 || w %d[0] != 0){ cout << "-1\n"; return 0; } reverse(d.begin(), d.end()); cout << solve(h, w) << '\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 | #include<bits/stdc++.h> using namespace std; vector<int>d; long long solve(long long h, long long w){ if(h*w==0)return 0; for(auto x : d){ if((h/x)*(w/x) == 0)continue; return (h/x)*(w/x) + solve(h-(h%x), w%x) + solve(h%x, w-(w%x)) + solve(h%x, w%x); //return (h/x)*(w/x) + min(solve(h, w%x) + solve(h%x, w-(w%x)), solve(h%x, w) + solve(h-(h%x), w%x)); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int h, w; cin >> h >> w; int n; cin >> n; d.resize(n); for(auto &x : d)cin >> x; if(h%d[0] != 0 || w %d[0] != 0){ cout << "-1\n"; return 0; } reverse(d.begin(), d.end()); cout << solve(h, w) << '\n'; return 0; } |