#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif ll dp(int h, int w, const vector<int>& a) { for (int d : a) { if (d <= min(h,w)) { return (ll)(w/d)*(h/d) + dp(h,w%d,a) + dp(h%d,w-w%d,a); } } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; int n; cin >> n; vector<int> a(n); for (int& x : a) cin >> x; if (h % a[0] || w % a[0]) { cout << -1 << '\n'; return 0; } reverse(a.begin(), a.end()); cout << dp(h, w, a) << '\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 38 39 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif ll dp(int h, int w, const vector<int>& a) { for (int d : a) { if (d <= min(h,w)) { return (ll)(w/d)*(h/d) + dp(h,w%d,a) + dp(h%d,w-w%d,a); } } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; int n; cin >> n; vector<int> a(n); for (int& x : a) cin >> x; if (h % a[0] || w % a[0]) { cout << -1 << '\n'; return 0; } reverse(a.begin(), a.end()); cout << dp(h, w, a) << '\n'; } |