#include <stdio.h>
#include <iostream>
//#include <bits/stdc++.h>
using namespace std;
int h, w;
int obrazy[35];
short n;
long long fillVoid(int x, int y, short d) {
long long cnt = 0;
long long maxE = max(x, y);
long long minE = min(x, y);
int rest = 0;
//cout << maxE << ' ' << minE << '\n';
while (d > 0) {
if ((obrazy[d] <= minE) && (obrazy[d] <= maxE)) {
//cout << maxE << "->" << obrazy[d] << " = " << (minE / obrazy[d]) * ((maxE - (maxE % obrazy[d])) / obrazy[d]) << '\n';
cnt += (minE / obrazy[d]) * ((maxE - (maxE % obrazy[d])) / obrazy[d]);
maxE %= obrazy[d];
const short res = minE % obrazy[d];
if (res != 0) {
minE -= res;
rest = res;
}
}
else d--;
}
if (rest > 0) {
//cout << "rekurencja\n";
const long long res = fillVoid(rest, max(x, y), n);
if (res != -1) cnt += res;
else return -1;
//cout << "cnt: " << cnt << '\n';
}
// if (maxE == 0) cout << cnt << " git\n";
// else cout << "dupa\n";
return (maxE == 0 ? cnt : -1);
}
int main() {
scanf("%d%d%hd", &h, &w, &n);
for (short i = 1; i <= n; i++)
scanf("%d", obrazy + i);
printf("%lld\n", fillVoid(w, h, n));
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 | #include <stdio.h> #include <iostream> //#include <bits/stdc++.h> using namespace std; int h, w; int obrazy[35]; short n; long long fillVoid(int x, int y, short d) { long long cnt = 0; long long maxE = max(x, y); long long minE = min(x, y); int rest = 0; //cout << maxE << ' ' << minE << '\n'; while (d > 0) { if ((obrazy[d] <= minE) && (obrazy[d] <= maxE)) { //cout << maxE << "->" << obrazy[d] << " = " << (minE / obrazy[d]) * ((maxE - (maxE % obrazy[d])) / obrazy[d]) << '\n'; cnt += (minE / obrazy[d]) * ((maxE - (maxE % obrazy[d])) / obrazy[d]); maxE %= obrazy[d]; const short res = minE % obrazy[d]; if (res != 0) { minE -= res; rest = res; } } else d--; } if (rest > 0) { //cout << "rekurencja\n"; const long long res = fillVoid(rest, max(x, y), n); if (res != -1) cnt += res; else return -1; //cout << "cnt: " << cnt << '\n'; } // if (maxE == 0) cout << cnt << " git\n"; // else cout << "dupa\n"; return (maxE == 0 ? cnt : -1); } int main() { scanf("%d%d%hd", &h, &w, &n); for (short i = 1; i <= n; i++) scanf("%d", obrazy + i); printf("%lld\n", fillVoid(w, h, n)); return 0; } |
English