#include <bits/stdc++.h> using namespace std; unsigned long long int wynik = 0; void f (unsigned long long int k, unsigned long long int h, unsigned long long int tab[], int n, int itr) { //cout << k << ' ' << h << ' ' << n << ' ' << itr << "\n"; while (tab[itr] > k || tab[itr] > h) itr--; wynik = wynik + (h / tab[itr]) * (k / tab[itr]); if (h % tab[itr] != 0) { f (tab[itr] * (k / tab[itr]), h % tab[itr], tab, n, itr); } if (k % tab[itr] != 0) { f (k % tab[itr], tab[itr] * (h / tab[itr]), tab, n, itr); } if (h % tab[itr] != 0 && k % tab[itr] != 0) { f (k % tab[itr], h % tab[itr], tab, n, itr); } return; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); unsigned long long int h, w; cin >> h >> w; int n; cin >> n; unsigned long long int tab[n]; for (int i = 0; i < n; i++) { cin >> tab[i]; } if (h % tab[0] != 0) { cout << -1; return 0; } if (w % tab[0] != 0) { cout << -1; return 0; } int itr = n-1; f(w, h, tab, n, itr); cout << wynik; 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 | #include <bits/stdc++.h> using namespace std; unsigned long long int wynik = 0; void f (unsigned long long int k, unsigned long long int h, unsigned long long int tab[], int n, int itr) { //cout << k << ' ' << h << ' ' << n << ' ' << itr << "\n"; while (tab[itr] > k || tab[itr] > h) itr--; wynik = wynik + (h / tab[itr]) * (k / tab[itr]); if (h % tab[itr] != 0) { f (tab[itr] * (k / tab[itr]), h % tab[itr], tab, n, itr); } if (k % tab[itr] != 0) { f (k % tab[itr], tab[itr] * (h / tab[itr]), tab, n, itr); } if (h % tab[itr] != 0 && k % tab[itr] != 0) { f (k % tab[itr], h % tab[itr], tab, n, itr); } return; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); unsigned long long int h, w; cin >> h >> w; int n; cin >> n; unsigned long long int tab[n]; for (int i = 0; i < n; i++) { cin >> tab[i]; } if (h % tab[0] != 0) { cout << -1; return 0; } if (w % tab[0] != 0) { cout << -1; return 0; } int itr = n-1; f(w, h, tab, n, itr); cout << wynik; return 0; } |