#include<cstdio> #include<vector> //#include<algorithm> using namespace std; vector<long long> d; static vector<pair<long long, long long>> calc2(long long k) { long long find = -1; for (auto it = d.rbegin(); it != d.rend(); ++it) { if (k >= *it) { find = *it; break; } } //printf("k = %lld find = %lld\n", k, find); if (find != -1) { auto p = make_pair(find, k / find); if (k % find != 0) { //printf("case a\n"); vector<pair<long long, long long>> result; auto partial = calc2(k % find); if (partial.empty()) { return result; } result.push_back(p); result.insert(result.end(), partial.begin(), partial.end()); return result; } else { //printf("case b\n"); vector<pair<long long, long long>> result; result.push_back(p); return result; } } else { vector<pair<long long, long long>> result; return result; } } static pair<long long, long long> calc(long long k) { auto partial = calc2(k); //printf("k = %lld size = %lld\n", k, partial.size()); if (partial.empty()) { return make_pair(-1, -1); } //printf("p[0] = %lld %lld\n", partial[0].first, partial[0].second); long long result = 0; long long x = partial[0].first; for (auto it = partial.begin(); it != partial.end(); ++it) { //printf("### (%lld %lld)\n", it->first, it->second); result += (it->second) * (x / (it->first)); } //printf("$$$ %lld\n", result); return make_pair(partial[0].first, result); } int main() { long long h, w; int n; long long result = 0; scanf("%lld %lld", &h, &w); scanf("%d", &n); for (int i = 0; i < n; i++) { long long di; scanf("%lld", &di); d.push_back(di); } while (h > 0 && w > 0) { // h < w if (h > w) { swap(h, w); } auto partial = calc(h); //printf("=== %lld %lld %lld %lld\n", h, w, partial.first, partial.second); if (partial.first == -1) { printf("-1"); return 0; } long long k = w / partial.first; //long long k = w / h; result += k * partial.second; w = w % partial.first; //w = w % h; } printf("%lld", 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 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #include<cstdio> #include<vector> //#include<algorithm> using namespace std; vector<long long> d; static vector<pair<long long, long long>> calc2(long long k) { long long find = -1; for (auto it = d.rbegin(); it != d.rend(); ++it) { if (k >= *it) { find = *it; break; } } //printf("k = %lld find = %lld\n", k, find); if (find != -1) { auto p = make_pair(find, k / find); if (k % find != 0) { //printf("case a\n"); vector<pair<long long, long long>> result; auto partial = calc2(k % find); if (partial.empty()) { return result; } result.push_back(p); result.insert(result.end(), partial.begin(), partial.end()); return result; } else { //printf("case b\n"); vector<pair<long long, long long>> result; result.push_back(p); return result; } } else { vector<pair<long long, long long>> result; return result; } } static pair<long long, long long> calc(long long k) { auto partial = calc2(k); //printf("k = %lld size = %lld\n", k, partial.size()); if (partial.empty()) { return make_pair(-1, -1); } //printf("p[0] = %lld %lld\n", partial[0].first, partial[0].second); long long result = 0; long long x = partial[0].first; for (auto it = partial.begin(); it != partial.end(); ++it) { //printf("### (%lld %lld)\n", it->first, it->second); result += (it->second) * (x / (it->first)); } //printf("$$$ %lld\n", result); return make_pair(partial[0].first, result); } int main() { long long h, w; int n; long long result = 0; scanf("%lld %lld", &h, &w); scanf("%d", &n); for (int i = 0; i < n; i++) { long long di; scanf("%lld", &di); d.push_back(di); } while (h > 0 && w > 0) { // h < w if (h > w) { swap(h, w); } auto partial = calc(h); //printf("=== %lld %lld %lld %lld\n", h, w, partial.first, partial.second); if (partial.first == -1) { printf("-1"); return 0; } long long k = w / partial.first; //long long k = w / h; result += k * partial.second; w = w % partial.first; //w = w % h; } printf("%lld", result); return 0; } |