#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vb vector<bool> #define vii vector<pii> #define siz(x) (int)x.size() #define pb push_back #define st first #define nd second #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int inf = 1e9, maxn = 1e6, mod = 1e9+7; void zle(){ cout<<-1; exit(0); } vi d; int rec(int h, int w){ int ans = 0; int dim=-1; for(auto v : d)if(v<=h && v<=w){dim = v; break;} if(dim == -1)zle(); int rows = h/dim; int cols = w/dim; ans+=rows*cols; int h1 = (rows*dim); int w1 = (cols*dim); if(h-h1 > 0 && w-w1 > 0)ans+=rec(h-h1,w-w1); if(h-h1 > 0)ans+=rec(h-h1,w1); if(w-w1 > 0)ans+=rec(h1,w-w1); return ans; } int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0); int h,w,n; cin>>h>>w>>n; d.resize(n); for(auto &w : d)cin>>w; sort(all(d));reverse(all(d)); cout<<rec(h,w); }
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 | #include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define all(x) x.begin(),x.end() #define vi vector<int> #define vb vector<bool> #define vii vector<pii> #define siz(x) (int)x.size() #define pb push_back #define st first #define nd second #define rep(i,a,b) for(int i=a; i<=b; i++) using namespace std; const int inf = 1e9, maxn = 1e6, mod = 1e9+7; void zle(){ cout<<-1; exit(0); } vi d; int rec(int h, int w){ int ans = 0; int dim=-1; for(auto v : d)if(v<=h && v<=w){dim = v; break;} if(dim == -1)zle(); int rows = h/dim; int cols = w/dim; ans+=rows*cols; int h1 = (rows*dim); int w1 = (cols*dim); if(h-h1 > 0 && w-w1 > 0)ans+=rec(h-h1,w-w1); if(h-h1 > 0)ans+=rec(h-h1,w1); if(w-w1 > 0)ans+=rec(h1,w-w1); return ans; } int32_t main(){ ios_base::sync_with_stdio(0);cin.tie(0); int h,w,n; cin>>h>>w>>n; d.resize(n); for(auto &w : d)cin>>w; sort(all(d));reverse(all(d)); cout<<rec(h,w); } |