#include <bits/stdc++.h> using namespace std; long long solve(long long h, long long w, vector<int>&a, bool fl=true) { if (h == 0 || w == 0) return 0; //~ if (h < w) swap(h, w); //~ cout<<h<<" "<<w<<endl; long long r=1e18; for (auto i : a) if (i <= min(h, w)) { return (h/i) * (w/i) + solve(h%i, w-w%i, a) + solve(h-h%i, w%i, a) + solve(h%i, w%i, a); //~ return h/i * w/i + solve(h%i, w, a) + solve(h-h%i, w%i, a); //~ r=min({r, (h/i) * (w/i) + solve(h%i, w, a) + solve(h-h%i, w%i, a), h/i * w/i + solve(h%i, w-w%i, a) + solve(h, w%i, a)});; //~ if (fl) cout<<" "<<i<<" "<<h/i * w/i << " " << solve(h%i, w, a, 0) + solve(h-h%i, w%i, a, 0)<<" "<< solve(h%i, w-w%i, a, 0) + solve(h, w%i, a, 0) << endl; //~ return h/i * w/i + solve(h%i, w-w%i, a) + solve(h, w%i, a); } //~ if (fl) cout<<"XD "<<h<<" "<<w<<" "<<r<<endl; return r; } int main() { ios_base::sync_with_stdio(false); int h, w, n; cin>>h>>w>>n; vector<int>a(n); for (auto &i : a) cin>>i; if (h%a[0] || w%a[0]) { cout<<"-1\n"; return 0; } reverse(a.begin(), a.end()); cout<<solve(h, w, a)<<"\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 | #include <bits/stdc++.h> using namespace std; long long solve(long long h, long long w, vector<int>&a, bool fl=true) { if (h == 0 || w == 0) return 0; //~ if (h < w) swap(h, w); //~ cout<<h<<" "<<w<<endl; long long r=1e18; for (auto i : a) if (i <= min(h, w)) { return (h/i) * (w/i) + solve(h%i, w-w%i, a) + solve(h-h%i, w%i, a) + solve(h%i, w%i, a); //~ return h/i * w/i + solve(h%i, w, a) + solve(h-h%i, w%i, a); //~ r=min({r, (h/i) * (w/i) + solve(h%i, w, a) + solve(h-h%i, w%i, a), h/i * w/i + solve(h%i, w-w%i, a) + solve(h, w%i, a)});; //~ if (fl) cout<<" "<<i<<" "<<h/i * w/i << " " << solve(h%i, w, a, 0) + solve(h-h%i, w%i, a, 0)<<" "<< solve(h%i, w-w%i, a, 0) + solve(h, w%i, a, 0) << endl; //~ return h/i * w/i + solve(h%i, w-w%i, a) + solve(h, w%i, a); } //~ if (fl) cout<<"XD "<<h<<" "<<w<<" "<<r<<endl; return r; } int main() { ios_base::sync_with_stdio(false); int h, w, n; cin>>h>>w>>n; vector<int>a(n); for (auto &i : a) cin>>i; if (h%a[0] || w%a[0]) { cout<<"-1\n"; return 0; } reverse(a.begin(), a.end()); cout<<solve(h, w, a)<<"\n"; } |