#include <bits/stdc++.h> using namespace std; typedef long long int LL; int n; LL d[40]; LL solve(LL h, LL w, int sIdx) { if(h == 0 || w == 0) { return 0; } while(d[sIdx] > h || d[sIdx] > w) { ++sIdx; } LL a = d[sIdx++]; LL wa = w/a; LL ha = h/a; LL res = wa * ha; LL subResLeft = solve(h, w%a, sIdx); LL subResRight = solve(h%a, a*wa, sIdx); return res + subResLeft + subResRight; } int main() { ios::sync_with_stdio(0); LL h, w; cin >> h >> w; cin >> n; for(int i = n-1; i >= 0; --i) { cin >> d[i]; } if ((h % d[n-1] != 0) || (w % d[n-1] != 0)) { cout << -1 << endl; return 0; } cout << solve(h, w, 0) << 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 | #include <bits/stdc++.h> using namespace std; typedef long long int LL; int n; LL d[40]; LL solve(LL h, LL w, int sIdx) { if(h == 0 || w == 0) { return 0; } while(d[sIdx] > h || d[sIdx] > w) { ++sIdx; } LL a = d[sIdx++]; LL wa = w/a; LL ha = h/a; LL res = wa * ha; LL subResLeft = solve(h, w%a, sIdx); LL subResRight = solve(h%a, a*wa, sIdx); return res + subResLeft + subResRight; } int main() { ios::sync_with_stdio(0); LL h, w; cin >> h >> w; cin >> n; for(int i = n-1; i >= 0; --i) { cin >> d[i]; } if ((h % d[n-1] != 0) || (w % d[n-1] != 0)) { cout << -1 << endl; return 0; } cout << solve(h, w, 0) << endl; return 0; } |