#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 3005; int h, w, n, i, tab[33]; LL get(int h, int w, int ind) { if (tab[ind] > h || tab[ind] > w) return get(h, w, ind - 1); LL res = (h / tab[ind]) * 1LL * (w / tab[ind]); if (h % tab[ind] != 0) res += get(h % tab[ind], w, ind - 1); if (w % tab[ind] != 0) res += get(h - h % tab[ind], w % tab[ind], ind - 1); return res; } int main() { scanf("%d %d %d", &h, &w, &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); if (h % tab[0] != 0 || w % tab[0] != 0) { printf("-1\n"); return 0; } printf("%lld\n", get(h, w, n - 1)); 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 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 3005; int h, w, n, i, tab[33]; LL get(int h, int w, int ind) { if (tab[ind] > h || tab[ind] > w) return get(h, w, ind - 1); LL res = (h / tab[ind]) * 1LL * (w / tab[ind]); if (h % tab[ind] != 0) res += get(h % tab[ind], w, ind - 1); if (w % tab[ind] != 0) res += get(h - h % tab[ind], w % tab[ind], ind - 1); return res; } int main() { scanf("%d %d %d", &h, &w, &n); for (i=0;i<n;i++) scanf("%d", &tab[i]); if (h % tab[0] != 0 || w % tab[0] != 0) { printf("-1\n"); return 0; } printf("%lld\n", get(h, w, n - 1)); return 0; } |