#include <iostream> #include <algorithm> #include <vector> using namespace std; long solution(int w, int h, int m, vector< int >::const_iterator it) { if (!w || !h) return 0; if (w < h) swap(w, h); if ((w % m) != 0 || (h % m) != 0) return -1; while (*it > w) --it; while (1) { int d = *it--; long wc = w / d, hc = h / d; long aw = wc * d, bh = hc * d, ah = h - bh, bw = w - aw, cw = bw, ch = ah; long ar, br, cr; if ( (ar = solution(aw, ah, m, it)) >= 0 && (br = solution(bw, bh, m, it)) >= 0 && (cr = solution(cw, ch, m, it)) >= 0) return wc * hc + ar + br + cr; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int w, h, n; cin >> w >> h >> n; vector< int > d(n); while (n && cin >> d[--n]) { } sort(d.begin(), d.end()); vector< int >::const_iterator it = d.end(); cout << solution(w, h, d[0], --it) << '\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 35 36 37 | #include <iostream> #include <algorithm> #include <vector> using namespace std; long solution(int w, int h, int m, vector< int >::const_iterator it) { if (!w || !h) return 0; if (w < h) swap(w, h); if ((w % m) != 0 || (h % m) != 0) return -1; while (*it > w) --it; while (1) { int d = *it--; long wc = w / d, hc = h / d; long aw = wc * d, bh = hc * d, ah = h - bh, bw = w - aw, cw = bw, ch = ah; long ar, br, cr; if ( (ar = solution(aw, ah, m, it)) >= 0 && (br = solution(bw, bh, m, it)) >= 0 && (cr = solution(cw, ch, m, it)) >= 0) return wc * hc + ar + br + cr; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int w, h, n; cin >> w >> h >> n; vector< int > d(n); while (n && cin >> d[--n]) { } sort(d.begin(), d.end()); vector< int >::const_iterator it = d.end(); cout << solution(w, h, d[0], --it) << '\n'; } |