#include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <unordered_set> using namespace std; void dfs(int v, vector<unordered_set<int>> &edges, vector<bool> &vis, vector<int> &topo) { if (vis[v]) return; vis[v] = true; for (auto &u : edges[v]) { if (vis[u]) continue; if (edges[0].find(u) == edges[0].end()) { topo.push_back(u); } dfs(u, edges, vis, topo); } } long long dp(vector<int> d, int h, int w) { // cout << h << ", " << w << endl; if (h == 0 || w == 0) return 0; if (d.empty()) return -1; int di = d.back(); int a = h / di; int b = w / di; long long cnt = (long long) a * (long long) b; d.pop_back(); long long ret1 = dp(d, h, w % di); long long ret2 = dp(d, h % di, w / di * di); if (ret1 == -1 || ret2 == -1) return -1; return ret1 + ret2 + cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w, n; cin >> h >> w >> n; vector<int> d(n); for (int i = 0; i < n; ++i) { cin >> d[i]; } cout << dp(d, h, w) << "\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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_map> #include <unordered_set> using namespace std; void dfs(int v, vector<unordered_set<int>> &edges, vector<bool> &vis, vector<int> &topo) { if (vis[v]) return; vis[v] = true; for (auto &u : edges[v]) { if (vis[u]) continue; if (edges[0].find(u) == edges[0].end()) { topo.push_back(u); } dfs(u, edges, vis, topo); } } long long dp(vector<int> d, int h, int w) { // cout << h << ", " << w << endl; if (h == 0 || w == 0) return 0; if (d.empty()) return -1; int di = d.back(); int a = h / di; int b = w / di; long long cnt = (long long) a * (long long) b; d.pop_back(); long long ret1 = dp(d, h, w % di); long long ret2 = dp(d, h % di, w / di * di); if (ret1 == -1 || ret2 == -1) return -1; return ret1 + ret2 + cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w, n; cin >> h >> w >> n; vector<int> d(n); for (int i = 0; i < n; ++i) { cin >> d[i]; } cout << dp(d, h, w) << "\n"; } |