#include <cstdio> typedef long long int i64; i64 compute(i64 w, i64 h, int S[], int idx) { if (w == 0 || h == 0) { return 0; } while (S[idx] > w || S[idx] > h) { --idx; } return (w / S[idx]) * (h / S[idx]) + compute(w - w % S[idx], h % S[idx], S, idx) + compute(w % S[idx], h, S, idx); } int main() { int w, h, c; int S[33]; scanf("%d%d%d", &w, &h, &c); for (int i = 0; i < c; ++i) { scanf("%d", &S[i]); } if (w % S[0] != 0 || h % S[0] != 0) { puts("-1"); } else { printf("%lld\n", compute(w, h, S, c - 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 | #include <cstdio> typedef long long int i64; i64 compute(i64 w, i64 h, int S[], int idx) { if (w == 0 || h == 0) { return 0; } while (S[idx] > w || S[idx] > h) { --idx; } return (w / S[idx]) * (h / S[idx]) + compute(w - w % S[idx], h % S[idx], S, idx) + compute(w % S[idx], h, S, idx); } int main() { int w, h, c; int S[33]; scanf("%d%d%d", &w, &h, &c); for (int i = 0; i < c; ++i) { scanf("%d", &S[i]); } if (w % S[0] != 0 || h % S[0] != 0) { puts("-1"); } else { printf("%lld\n", compute(w, h, S, c - 1)); } } |