#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const int N = 40; ll d[N], n, h, w; ll gen(ll h, ll w, int x) { if (w > h) swap(h, w); if (h == 0 || w == 0) return 0; for (int i=x; i>=1; --i) { if (d[i] > min(h, w)) continue; ll hd=h/d[i], wd=w/d[i]; if (hd*wd == 0) continue; return hd*wd+gen(h, w-wd*d[i], x-1)+gen(h-hd*d[i], wd*d[i], x-1); } assert(false); } void solve() { cin>>h>>w>>n; for (int i=1; i<=n; ++i) cin>>d[i]; if (h%d[1] != 0 || w%d[1] != 0 || d[1] > min(h, w)) { cout<<-1<<"\n"; return; } cout<<gen(h, w, n)<<"\n"; } int main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); //cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pii; const int N = 40; ll d[N], n, h, w; ll gen(ll h, ll w, int x) { if (w > h) swap(h, w); if (h == 0 || w == 0) return 0; for (int i=x; i>=1; --i) { if (d[i] > min(h, w)) continue; ll hd=h/d[i], wd=w/d[i]; if (hd*wd == 0) continue; return hd*wd+gen(h, w-wd*d[i], x-1)+gen(h-hd*d[i], wd*d[i], x-1); } assert(false); } void solve() { cin>>h>>w>>n; for (int i=1; i<=n; ++i) cin>>d[i]; if (h%d[1] != 0 || w%d[1] != 0 || d[1] > min(h, w)) { cout<<-1<<"\n"; return; } cout<<gen(h, w, n)<<"\n"; } int main() { //ios_base::sync_with_stdio(0); //cin.tie(NULL); //cout.tie(NULL); int t=1; //cin>>t; while (t--) solve(); } |