#include <cstdio> #include <utility> using namespace std; int kaf[40]; int base; unsigned long long int wypelnij(int a, int b, int m) { if(!b) return 0; unsigned long long int res = 0; while(b / kaf[m] == 0) m--; int r_a = a % kaf[m]; int r_b = b % kaf[m]; unsigned long long int z_a = a / kaf[m]; unsigned long long int z_b = b / kaf[m]; res += z_a * z_b; if(r_a < r_b) { swap(r_a, r_b); swap(z_a, z_b); swap(a, b); } return res + wypelnij(b, r_a, m) + wypelnij(a - r_a, r_b, m); } int main() { int h, w; scanf("%d %d", &h, &w); int n; scanf("%d", &n); scanf("%d", &base); kaf[0] = 1; for(int i = 1; i < n; i++) { scanf("%d", &kaf[i]); kaf[i] /= base; } if(h % base || w % base) { printf("-1\n"); return 0; } h /= base; w /= base; if(h < w) swap(h, w); printf("%llu\n", wypelnij(h, w, n - 1)); }
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 59 60 61 62 63 64 | #include <cstdio> #include <utility> using namespace std; int kaf[40]; int base; unsigned long long int wypelnij(int a, int b, int m) { if(!b) return 0; unsigned long long int res = 0; while(b / kaf[m] == 0) m--; int r_a = a % kaf[m]; int r_b = b % kaf[m]; unsigned long long int z_a = a / kaf[m]; unsigned long long int z_b = b / kaf[m]; res += z_a * z_b; if(r_a < r_b) { swap(r_a, r_b); swap(z_a, z_b); swap(a, b); } return res + wypelnij(b, r_a, m) + wypelnij(a - r_a, r_b, m); } int main() { int h, w; scanf("%d %d", &h, &w); int n; scanf("%d", &n); scanf("%d", &base); kaf[0] = 1; for(int i = 1; i < n; i++) { scanf("%d", &kaf[i]); kaf[i] /= base; } if(h % base || w % base) { printf("-1\n"); return 0; } h /= base; w /= base; if(h < w) swap(h, w); printf("%llu\n", wypelnij(h, w, n - 1)); } |