#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) vec<long> sizes; map<pair<int,int>, long> mem; long biggest_poss(int n) { for (long s: sizes) { if (s<=n) return s; } return -1; } long solve(int x, int y) { //cout << x sp y << endl; if (x == 0 || y == 0) return 0; if (x < y) swap(x,y); if (mem.count(mp(x,y))) { return mem[mp(x,y)]; } long side = min(biggest_poss(x), biggest_poss(y)); //cout << "side " << side << endl; if (side == -1 || side == -1) return -1; long rect_x = side * (x/side); long rect_y = side * (y/side); //cout << "inner " << rect_x sp rect_y << endl; long rect1_res = solve(rect_x, y - rect_y); long rect2_res = solve(x - rect_x, rect_y); long rect3_res = solve(x - rect_x, y - rect_y); if (rect1_res == -1 || rect2_res == -1 || rect3_res == -1) return -1; return (x/side)*(y/side) + rect1_res + rect2_res + rect3_res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long h,w; int n; cin >> h >> w >> n; rep (_, n) { int s; cin >> s; sizes.eb(s); } sort(sizes.begin(), sizes.end(), greater()); cout << solve(h, w) << endl; 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) vec<long> sizes; map<pair<int,int>, long> mem; long biggest_poss(int n) { for (long s: sizes) { if (s<=n) return s; } return -1; } long solve(int x, int y) { //cout << x sp y << endl; if (x == 0 || y == 0) return 0; if (x < y) swap(x,y); if (mem.count(mp(x,y))) { return mem[mp(x,y)]; } long side = min(biggest_poss(x), biggest_poss(y)); //cout << "side " << side << endl; if (side == -1 || side == -1) return -1; long rect_x = side * (x/side); long rect_y = side * (y/side); //cout << "inner " << rect_x sp rect_y << endl; long rect1_res = solve(rect_x, y - rect_y); long rect2_res = solve(x - rect_x, rect_y); long rect3_res = solve(x - rect_x, y - rect_y); if (rect1_res == -1 || rect2_res == -1 || rect3_res == -1) return -1; return (x/side)*(y/side) + rect1_res + rect2_res + rect3_res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long h,w; int n; cin >> h >> w >> n; rep (_, n) { int s; cin >> s; sizes.eb(s); } sort(sizes.begin(), sizes.end(), greater()); cout << solve(h, w) << endl; return 0; } |