#include <bits/stdc++.h> #define MAX_N 37 using namespace std; int d, h, n; int t[MAX_N]; long long res, d1[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> d >> h >> n; for (int i = 0; i < n; i++) { cin >> t[i]; } sort(t, t + n); if (d % t[0] != 0 || h % t[0] != 0) { cout << "-1\n"; return 0; } for (int i = 0; i < n; i++) { int temph = h; long long tres = 0; for (int j = i; j >= 0; j--) { if (temph == 0) { break; } int cnt = temph / t[j]; temph %= t[j]; tres += (long long)cnt * (long long)(t[i] / t[j]); } d1[i] = tres; } for (int i = n - 1; i >= 0; i--) { if (d == 0) { break; } int cnt = d / t[i]; d %= t[i]; res += (long long)cnt * d1[i]; } cout << res << "\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 | #include <bits/stdc++.h> #define MAX_N 37 using namespace std; int d, h, n; int t[MAX_N]; long long res, d1[MAX_N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> d >> h >> n; for (int i = 0; i < n; i++) { cin >> t[i]; } sort(t, t + n); if (d % t[0] != 0 || h % t[0] != 0) { cout << "-1\n"; return 0; } for (int i = 0; i < n; i++) { int temph = h; long long tres = 0; for (int j = i; j >= 0; j--) { if (temph == 0) { break; } int cnt = temph / t[j]; temph %= t[j]; tres += (long long)cnt * (long long)(t[i] / t[j]); } d1[i] = tres; } for (int i = n - 1; i >= 0; i--) { if (d == 0) { break; } int cnt = d / t[i]; d %= t[i]; res += (long long)cnt * d1[i]; } cout << res << "\n"; return 0; } |