#include <bits/stdc++.h> using namespace std; const int L = 30; int dims[L] = { 0 }; void printArray(int array[]) { for (int i = 0; i < L; i++) { cout << array[i] << " "; } cout << endl; } struct Counter { long long int h; long long int w; int maxDimIndex; long long int totalCount = 0; Counter(long long int initH, long long int initW, int n) { this->h = initH; this->w = initW; this->maxDimIndex = n; while (h > 0 && w > 0) { process(); } } void process() { long long int maxIncludedLength = 0; for (int i = maxDimIndex; i >= 0; i--) { const long long int cDim = dims[i]; if (h >= cDim && w >= cDim) { maxIncludedLength = cDim; maxDimIndex = i - 1; break; } } long long int mulH = h / maxIncludedLength; h = h % maxIncludedLength; long long int mulW = w / maxIncludedLength; w = w % maxIncludedLength; totalCount += mulH * mulW; const int rectH = mulH * maxIncludedLength; const int rectW = mulW * maxIncludedLength; int restH = h, restW = w; for (int i = maxDimIndex; i >= 0; i--) { const int cDim = dims[i]; mulW = restW / cDim; totalCount += rectH / cDim * mulW; restW = restW % cDim; mulH = restH / cDim; totalCount += rectW / cDim * mulH; restH = restH % cDim; } } }; int main() { // ifstream cin("tests/0a.in"); // ifstream cin("tests/4.in"); cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n; long long int h, w; cin >> h; cin >> w; cin >> n; for (int i = 0; i < n; i++) { cin >> dims[i]; } // printArray(dims); if (h % dims[0] != 0 || w % dims[0] != 0) { cout << "-1" << endl; return 0; } Counter ctr(h, w, n - 1); // printArray(height); // printArray(width); cout << ctr.totalCount << endl; // prints 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 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <bits/stdc++.h> using namespace std; const int L = 30; int dims[L] = { 0 }; void printArray(int array[]) { for (int i = 0; i < L; i++) { cout << array[i] << " "; } cout << endl; } struct Counter { long long int h; long long int w; int maxDimIndex; long long int totalCount = 0; Counter(long long int initH, long long int initW, int n) { this->h = initH; this->w = initW; this->maxDimIndex = n; while (h > 0 && w > 0) { process(); } } void process() { long long int maxIncludedLength = 0; for (int i = maxDimIndex; i >= 0; i--) { const long long int cDim = dims[i]; if (h >= cDim && w >= cDim) { maxIncludedLength = cDim; maxDimIndex = i - 1; break; } } long long int mulH = h / maxIncludedLength; h = h % maxIncludedLength; long long int mulW = w / maxIncludedLength; w = w % maxIncludedLength; totalCount += mulH * mulW; const int rectH = mulH * maxIncludedLength; const int rectW = mulW * maxIncludedLength; int restH = h, restW = w; for (int i = maxDimIndex; i >= 0; i--) { const int cDim = dims[i]; mulW = restW / cDim; totalCount += rectH / cDim * mulW; restW = restW % cDim; mulH = restH / cDim; totalCount += rectW / cDim * mulH; restH = restH % cDim; } } }; int main() { // ifstream cin("tests/0a.in"); // ifstream cin("tests/4.in"); cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n; long long int h, w; cin >> h; cin >> w; cin >> n; for (int i = 0; i < n; i++) { cin >> dims[i]; } // printArray(dims); if (h % dims[0] != 0 || w % dims[0] != 0) { cout << "-1" << endl; return 0; } Counter ctr(h, w, n - 1); // printArray(height); // printArray(width); cout << ctr.totalCount << endl; // prints return 0; } |