#include<bits/stdc++.h>
using namespace std;
vector<int> v;
long long solve(int a, int b)
{
if(a > b)
swap(a, b);
if(a == 0)
return 0;
int pom = v[0];
for(auto x : v)
if(x <= a)
pom = x;
long long wym1 = a / pom, wym2 = b / pom;
long long pole = wym1 * wym2;
return pole + solve(a - wym1 * pom, wym2 * pom) + solve(b - wym2 * pom, a);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int a, b;
cin>>a>>b;
if(a > b)
swap(a, b);
int c;
cin>>c;
while(c--)
{
int d;
cin>>d;
v.push_back(d);
}
if(a % v[0] || b % v[0])
{
cout<<-1;
return 0;
}
cout<<solve(a, b);
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 | #include<bits/stdc++.h> using namespace std; vector<int> v; long long solve(int a, int b) { if(a > b) swap(a, b); if(a == 0) return 0; int pom = v[0]; for(auto x : v) if(x <= a) pom = x; long long wym1 = a / pom, wym2 = b / pom; long long pole = wym1 * wym2; return pole + solve(a - wym1 * pom, wym2 * pom) + solve(b - wym2 * pom, a); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin>>a>>b; if(a > b) swap(a, b); int c; cin>>c; while(c--) { int d; cin>>d; v.push_back(d); } if(a % v[0] || b % v[0]) { cout<<-1; return 0; } cout<<solve(a, b); return 0; } |
English