#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
const int N = 30 + 9;
int nextInt() { int n; scanf("%d", &n); return n; }
int a[N];
long long solve() {
int x = nextInt();
int y = nextInt();
int n = nextInt();
for (int i = 0; i < n; ++i)
a[i] = nextInt();
if (x % a[0] != 0) return -1;
if (y % a[0] != 0) return -1;
long long res = 0;
int idx = 0;
while (x > 0 && y > 0) {
// assert( x % a[idx] == 0 && y % a[idx] == 0 );
if (idx == n - 1) {
res += 1LL * (x / a[idx]) * (y / a[idx]);
break;
}
int xx = x % a[idx + 1];
int yy = y % a[idx + 1];
if (xx != 0) {
res += 1LL * (xx / a[idx]) * (y / a[idx]);
x -= xx;
}
if (yy != 0) {
res += 1LL * (yy / a[idx]) * (x / a[idx]);
y -= yy;
}
++idx;
}
return res;
}
int main() {
long long res = solve();
printf("%lld\n", res);
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 <cstdio> #include <vector> #include <map> #include <set> #include <queue> #include <unordered_map> #include <unordered_set> #include <algorithm> using namespace std; const int N = 30 + 9; int nextInt() { int n; scanf("%d", &n); return n; } int a[N]; long long solve() { int x = nextInt(); int y = nextInt(); int n = nextInt(); for (int i = 0; i < n; ++i) a[i] = nextInt(); if (x % a[0] != 0) return -1; if (y % a[0] != 0) return -1; long long res = 0; int idx = 0; while (x > 0 && y > 0) { // assert( x % a[idx] == 0 && y % a[idx] == 0 ); if (idx == n - 1) { res += 1LL * (x / a[idx]) * (y / a[idx]); break; } int xx = x % a[idx + 1]; int yy = y % a[idx + 1]; if (xx != 0) { res += 1LL * (xx / a[idx]) * (y / a[idx]); x -= xx; } if (yy != 0) { res += 1LL * (yy / a[idx]) * (x / a[idx]); y -= yy; } ++idx; } return res; } int main() { long long res = solve(); printf("%lld\n", res); return 0; } |
English