#include <iostream>
#include <vector>
using namespace std;
int main() {
long width, height;
int n;
cin >> width >> height >> n;
long* sizes = new long[n];
for (int i = 0; i < n; i++) {
cin >> sizes[i];
}
unsigned long long result = 0;
for (int i = n - 1; i >= 0; i--) {
long tempWidth = width;
if (sizes[i] > height) {
continue;
}
for (int j = i; j >= 0; j--) {
if (sizes[j] > tempWidth) {
continue;
}
long tempHeight = j == i ? height : (height / sizes[i]) * sizes[i];
unsigned long long widthCount = tempWidth / sizes[j];
unsigned long long heightCount = tempHeight / sizes[j];
tempWidth = tempWidth % sizes[j];
result += widthCount * heightCount;
}
if (tempWidth > 0) {
cout << -1;
return 0;
}
height = height % sizes[i];
}
if (height > 0) {
cout << -1;
return 0;
}
cout << result;
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 | #include <iostream> #include <vector> using namespace std; int main() { long width, height; int n; cin >> width >> height >> n; long* sizes = new long[n]; for (int i = 0; i < n; i++) { cin >> sizes[i]; } unsigned long long result = 0; for (int i = n - 1; i >= 0; i--) { long tempWidth = width; if (sizes[i] > height) { continue; } for (int j = i; j >= 0; j--) { if (sizes[j] > tempWidth) { continue; } long tempHeight = j == i ? height : (height / sizes[i]) * sizes[i]; unsigned long long widthCount = tempWidth / sizes[j]; unsigned long long heightCount = tempHeight / sizes[j]; tempWidth = tempWidth % sizes[j]; result += widthCount * heightCount; } if (tempWidth > 0) { cout << -1; return 0; } height = height % sizes[i]; } if (height > 0) { cout << -1; return 0; } cout << result; return 0; } |
English