#include <cstdio>
#include <cstdlib>
using namespace std;
long long d[31];
struct filling_res {
long long blocks;
long long remainder;
};
filling_res fill(long long total_len, long long blocksize) {
long long n_blocks = total_len / blocksize;
long long r_blocks = total_len % blocksize;
return filling_res{n_blocks, r_blocks};
}
long long fill_rec_aux(long long, long long, long long);
long long fill_rec(long long w, long long h, long long maxsquare) {
if (w < h) {
long long temp = w;
w = h;
h = temp;
}
// w >= h
long long maxfit_idx = 0;
for (long long i = maxsquare; i >= 0; --i) {
if (d[i] <= h) {
maxfit_idx = i;
break;
}
}
// printf("FILLING %lldx%lld with sq size %lld\n", w, h, d[maxfit_idx]);
filling_res initrem = fill(h, d[maxfit_idx]);
long long add_res = 0;
if (initrem.remainder != 0) {
add_res = fill_rec(w, initrem.remainder, maxfit_idx - 1);
}
return fill_rec_aux(w, h, maxfit_idx) + add_res;
}
long long fill_rec_aux(long long h, long long w, long long sq) {
// printf("AUXING %lldx%lld with sq size %lld\n", h, w, d[sq]);
// d[sq] is initial w
long long rem = h;
long long multiplier = w / d[sq];
long long totalblocks = 0;
while(rem != 0) {
filling_res res = fill(rem, d[sq]);
rem = res.remainder;
totalblocks += res.blocks * multiplier;
// printf("T: %lld, REM: %lld, S: %lld\n", totalblocks, rem, d[sq]);
if (sq != 0) {
// printf("SQ: %lld, DSQ: %lld, DSQ1: %lld\n", sq, d[sq], d[sq-1]);
// printf("Multiplying multiplier by %lld\n", d[sq] / d[sq-1]);
multiplier *= d[sq] / d[sq-1];
}
sq--;
}
// printf("AUXED %lld\n", totalblocks);
return totalblocks;
}
int main() {
long long h, w, n;
scanf("%lld %lld", &h, &w);
scanf("%lld", &n);
for (long long i = 0; i < n; ++i) {
scanf("%lld", &d[i]);
}
if (h % d[0] != 0 || w % d[0] != 0) {
printf("-1\n");
return 0;
}
printf("%lld\n", fill_rec(w, h, 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 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <cstdio> #include <cstdlib> using namespace std; long long d[31]; struct filling_res { long long blocks; long long remainder; }; filling_res fill(long long total_len, long long blocksize) { long long n_blocks = total_len / blocksize; long long r_blocks = total_len % blocksize; return filling_res{n_blocks, r_blocks}; } long long fill_rec_aux(long long, long long, long long); long long fill_rec(long long w, long long h, long long maxsquare) { if (w < h) { long long temp = w; w = h; h = temp; } // w >= h long long maxfit_idx = 0; for (long long i = maxsquare; i >= 0; --i) { if (d[i] <= h) { maxfit_idx = i; break; } } // printf("FILLING %lldx%lld with sq size %lld\n", w, h, d[maxfit_idx]); filling_res initrem = fill(h, d[maxfit_idx]); long long add_res = 0; if (initrem.remainder != 0) { add_res = fill_rec(w, initrem.remainder, maxfit_idx - 1); } return fill_rec_aux(w, h, maxfit_idx) + add_res; } long long fill_rec_aux(long long h, long long w, long long sq) { // printf("AUXING %lldx%lld with sq size %lld\n", h, w, d[sq]); // d[sq] is initial w long long rem = h; long long multiplier = w / d[sq]; long long totalblocks = 0; while(rem != 0) { filling_res res = fill(rem, d[sq]); rem = res.remainder; totalblocks += res.blocks * multiplier; // printf("T: %lld, REM: %lld, S: %lld\n", totalblocks, rem, d[sq]); if (sq != 0) { // printf("SQ: %lld, DSQ: %lld, DSQ1: %lld\n", sq, d[sq], d[sq-1]); // printf("Multiplying multiplier by %lld\n", d[sq] / d[sq-1]); multiplier *= d[sq] / d[sq-1]; } sq--; } // printf("AUXED %lld\n", totalblocks); return totalblocks; } int main() { long long h, w, n; scanf("%lld %lld", &h, &w); scanf("%lld", &n); for (long long i = 0; i < n; ++i) { scanf("%lld", &d[i]); } if (h % d[0] != 0 || w % d[0] != 0) { printf("-1\n"); return 0; } printf("%lld\n", fill_rec(w, h, n-1)); return 0; } |
English