#include <bits/stdc++.h> using namespace std; unsigned long long d[300]; bool spr; unsigned long long solve(const int &n, const unsigned long long & h,const unsigned long long &w){ unsigned long long result = 0; unsigned long long m1, m2; if(h == 0 || w == 0){ return 0; } if(h < d[0] || w < d[0]){ spr = false; return 0; } for(int i = n - 1; i >= 0; --i){ if(d[i] <= h && d[i] <= w){ m1 = (h / d[i]); m2 = (w / d[i]); result += m1 * m2; result += solve(n, m1 * d[i], w - m2 * d[i]); result += solve(n, h - m1 * d[i], m2 * d[i]); result += solve(n, h - m1 * d[i], w - m2 * d[i]); break; } } return result; } int main(){ std::ios_base::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); int n; unsigned long long h, w, result; cin >> h >> w; cin >> n; spr = true; for(int i = 0; i < n; ++i){ cin >> d[i]; } result = solve(n, h, w); if(spr){ cout << result << '\n'; } else{ cout << "-1\n"; } 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 | #include <bits/stdc++.h> using namespace std; unsigned long long d[300]; bool spr; unsigned long long solve(const int &n, const unsigned long long & h,const unsigned long long &w){ unsigned long long result = 0; unsigned long long m1, m2; if(h == 0 || w == 0){ return 0; } if(h < d[0] || w < d[0]){ spr = false; return 0; } for(int i = n - 1; i >= 0; --i){ if(d[i] <= h && d[i] <= w){ m1 = (h / d[i]); m2 = (w / d[i]); result += m1 * m2; result += solve(n, m1 * d[i], w - m2 * d[i]); result += solve(n, h - m1 * d[i], m2 * d[i]); result += solve(n, h - m1 * d[i], w - m2 * d[i]); break; } } return result; } int main(){ std::ios_base::sync_with_stdio(false); std::cout.tie(nullptr); std::cin.tie(nullptr); int n; unsigned long long h, w, result; cin >> h >> w; cin >> n; spr = true; for(int i = 0; i < n; ++i){ cin >> d[i]; } result = solve(n, h, w); if(spr){ cout << result << '\n'; } else{ cout << "-1\n"; } return 0; } |