#include <iostream>
using namespace std;
int obr[30], n;
long long ile(int h, int w)
{
int act, rh, rw;
long long ret, x;
ret = 0; act = 0;
while (act < n && (obr[act] > h || obr[act] > w))
act++;
if (act == n) return -1;
ret = (h / obr[act]) * (long long)(w / obr[act]);
rh = h % obr[act]; rw = w % obr[act];
if (rh == 0 && rw == 0) return ret;
if (rh != 0)
{
x = ile(rh, w - rw);
if (x == -1) return -1;
ret += x;
}
if (rw != 0)
{
x = ile(h - rh, rw);
if (x == -1) return -1;
ret += x;
}
if (rh != 0 && rw != 0)
{
x = ile(rh, rw);
if (x == -1) return -1;
ret += x;
}
return ret;
}
int main()
{
int h, w;
int i;
cin >> h >> w >> n;
for (i = n - 1; i >= 0; i--) cin >> obr[i];
cout << ile(h, w);
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 | #include <iostream> using namespace std; int obr[30], n; long long ile(int h, int w) { int act, rh, rw; long long ret, x; ret = 0; act = 0; while (act < n && (obr[act] > h || obr[act] > w)) act++; if (act == n) return -1; ret = (h / obr[act]) * (long long)(w / obr[act]); rh = h % obr[act]; rw = w % obr[act]; if (rh == 0 && rw == 0) return ret; if (rh != 0) { x = ile(rh, w - rw); if (x == -1) return -1; ret += x; } if (rw != 0) { x = ile(h - rh, rw); if (x == -1) return -1; ret += x; } if (rh != 0 && rw != 0) { x = ile(rh, rw); if (x == -1) return -1; ret += x; } return ret; } int main() { int h, w; int i; cin >> h >> w >> n; for (i = n - 1; i >= 0; i--) cin >> obr[i]; cout << ile(h, w); return 0; } |
English