#include <bits/stdc++.h>
using namespace std;
long long n, m1, m2, t[40];
bool failed;
long long fit(long long a, long long b, long long s)
{
if (a > b) swap(a, b);
if (a == 0) return 0;
if (s == -1)
{
failed = 1;
return 0;
}
if (a < t[s]) return fit(a, b, s - 1);
else
{
long long x = a / t[s], y = b / t[s];
x *= y;
long long n1 = a % t[s], n2 = b % t[s];
return x + fit(n1, b, s - 1) + fit(n2, a - n1, s - 1);
}
}
int main()
{
scanf("%lld%lld%lld", &m1, &m2, &n);
for (int i = 0; i < n; ++i)
{
scanf("%lld", &t[i]);
}
long long int x = fit(m1, m2, n-1);
if (!failed) printf("%lld", x);
else printf("-1");
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 | #include <bits/stdc++.h> using namespace std; long long n, m1, m2, t[40]; bool failed; long long fit(long long a, long long b, long long s) { if (a > b) swap(a, b); if (a == 0) return 0; if (s == -1) { failed = 1; return 0; } if (a < t[s]) return fit(a, b, s - 1); else { long long x = a / t[s], y = b / t[s]; x *= y; long long n1 = a % t[s], n2 = b % t[s]; return x + fit(n1, b, s - 1) + fit(n2, a - n1, s - 1); } } int main() { scanf("%lld%lld%lld", &m1, &m2, &n); for (int i = 0; i < n; ++i) { scanf("%lld", &t[i]); } long long int x = fit(m1, m2, n-1); if (!failed) printf("%lld", x); else printf("-1"); return 0; } |
English