#include <bits/stdc++.h> using namespace std; typedef long long ll; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll i; ll a,b; cin>>a>>b; ll n; cin>>n; ll tab[n]; for (i = 0; i < n; i++) cin>>tab[i]; if (a%tab[0] || b%tab[0]){ cout<<"-1\n"; return 0; } ll wy = 0; while (a > 0 && b > 0){ if (a > b) swap(a,b); ll naj = -1; ll x = a; ll akt = 0; for (i = n-1 ; i >= 0; i--){ if (tab[i] <= x){ if (naj == -1) naj = tab[i]; ll ile = x/tab[i]; x %= tab[i]; akt += (naj/tab[i])*ile; } } //cout<<a<<" "<<b<<" # "<<naj<<"\n"; ll ile = b/naj; wy += ile*akt; b %= naj; } cout<<wy<<"\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll i; ll a,b; cin>>a>>b; ll n; cin>>n; ll tab[n]; for (i = 0; i < n; i++) cin>>tab[i]; if (a%tab[0] || b%tab[0]){ cout<<"-1\n"; return 0; } ll wy = 0; while (a > 0 && b > 0){ if (a > b) swap(a,b); ll naj = -1; ll x = a; ll akt = 0; for (i = n-1 ; i >= 0; i--){ if (tab[i] <= x){ if (naj == -1) naj = tab[i]; ll ile = x/tab[i]; x %= tab[i]; akt += (naj/tab[i])*ile; } } //cout<<a<<" "<<b<<" # "<<naj<<"\n"; ll ile = b/naj; wy += ile*akt; b %= naj; } cout<<wy<<"\n"; return 0; } |