#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> d; typedef long long LL; LL ile(int h, int w) { if (h==0 || w==0) return 0; int x=min(h, w); int dd=-1; for (int i=d.size()-1; 0<=i; --i) if (d[i]<=x) { dd=d[i]; break; } if (dd==-1) return -1; int hh=h/dd, h3=hh*dd; int ww=w/dd, w3=ww*dd; LL a=ile(h3, w-w3); if (a==-1) return -1; LL b=ile(h-h3, w3); if (b==-1) return -1; LL c=ile(h-h3, w-w3); if (c==-1) return -1; return ((LL) hh)*ww+a+b+c; } int main() { int h, w, n; cin>>h>>w>>n; for (int i=0; i<n; ++i) { int a; cin>>a; d.push_back(a); } cout<<ile(h, w); 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> d; typedef long long LL; LL ile(int h, int w) { if (h==0 || w==0) return 0; int x=min(h, w); int dd=-1; for (int i=d.size()-1; 0<=i; --i) if (d[i]<=x) { dd=d[i]; break; } if (dd==-1) return -1; int hh=h/dd, h3=hh*dd; int ww=w/dd, w3=ww*dd; LL a=ile(h3, w-w3); if (a==-1) return -1; LL b=ile(h-h3, w3); if (b==-1) return -1; LL c=ile(h-h3, w-w3); if (c==-1) return -1; return ((LL) hh)*ww+a+b+c; } int main() { int h, w, n; cin>>h>>w>>n; for (int i=0; i<n; ++i) { int a; cin>>a; d.push_back(a); } cout<<ile(h, w); return 0; } |