#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
constexpr int MAXN = 35;
ull sizes[MAXN];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
ull h, w, n;
cin >> h >> w >> n;
for (ull i = 0; i < n; i++)
cin >> sizes[i];
sort(sizes, sizes + n, greater<ull>());
if (h % sizes[n - 1] != 0 || w % sizes[n - 1] != 0)
{
cout << "-1\n";
return 0;
}
ull i = 0;
ull cnt = 0;
while (i < w) // przechodzimy przez całą szerokość
{
ull column = 0;
// ustalamy szerokość kolumny
ull it = 0;
while (sizes[it] > min(w - i, h))
it++;
ull it2 = it;
ull j = 0;
while (j < h) // przechodzimy przez całą wysokość
{
while (sizes[it2] > h - j)
it2++;
column += (h - j) / sizes[it2] * (sizes[it] / sizes[it2]);
j += (h - j) / sizes[it2] * sizes[it2];
}
cnt += (w - i) / sizes[it] * column;
i += (w - i) / sizes[it] * sizes[it];
}
cout << cnt << '\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 | #include <bits/stdc++.h> #define ull unsigned long long using namespace std; constexpr int MAXN = 35; ull sizes[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); ull h, w, n; cin >> h >> w >> n; for (ull i = 0; i < n; i++) cin >> sizes[i]; sort(sizes, sizes + n, greater<ull>()); if (h % sizes[n - 1] != 0 || w % sizes[n - 1] != 0) { cout << "-1\n"; return 0; } ull i = 0; ull cnt = 0; while (i < w) // przechodzimy przez całą szerokość { ull column = 0; // ustalamy szerokość kolumny ull it = 0; while (sizes[it] > min(w - i, h)) it++; ull it2 = it; ull j = 0; while (j < h) // przechodzimy przez całą wysokość { while (sizes[it2] > h - j) it2++; column += (h - j) / sizes[it2] * (sizes[it] / sizes[it2]); j += (h - j) / sizes[it2] * sizes[it2]; } cnt += (w - i) / sizes[it] * column; i += (w - i) / sizes[it] * sizes[it]; } cout << cnt << '\n'; return 0; } |
English