#include <iostream> #include <vector> #include <algorithm> using namespace std; #define int long long vector <int> V; int f(int h, int w, int j) //height, width { if (h == 0 || w == 0) return 0; while (j < V.size() && (V[j] > h || V[j] > w)) j ++; return f(h % V[j], w, j) + f(h - (h % V[j]), w % V[j], j) + (h / V[j]) * (w / V[j]); } int32_t main() { int h, w, n; cin>>h>>w>>n; for (int i = 1; i <= n; i ++) { int d; cin>>d; V.push_back(d); } reverse(V.begin(), V.end()); if (h % V.back() != 0 || w % V.back() != 0) return cout<<"-1\n", 0; cout<<f(h, w, 0)<<'\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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; #define int long long vector <int> V; int f(int h, int w, int j) //height, width { if (h == 0 || w == 0) return 0; while (j < V.size() && (V[j] > h || V[j] > w)) j ++; return f(h % V[j], w, j) + f(h - (h % V[j]), w % V[j], j) + (h / V[j]) * (w / V[j]); } int32_t main() { int h, w, n; cin>>h>>w>>n; for (int i = 1; i <= n; i ++) { int d; cin>>d; V.push_back(d); } reverse(V.begin(), V.end()); if (h % V.back() != 0 || w % V.back() != 0) return cout<<"-1\n", 0; cout<<f(h, w, 0)<<'\n'; } |