#include <bits/stdc++.h> using namespace std; constexpr int M = 35; long long tab[M]; long long ile; long long bs(long long v){ long long p = 1, k = ile+1, mid; while(p<k){ mid = (p+k)/2; if(tab[mid] <= v) p = mid + 1; else k = mid; } return tab[p-1]; } long long wynik = 0; bool czy = 1; void ileDla(long long n, long long m){ if(n > m) swap(n, m); long long akt = bs(n); if(akt==0){ czy = 0; return; } //cout<<n<<" "<<m<<endl; //cout<<"zwiekszamy wynik o: "<<m/akt<<" bo dopychamy: "<<akt<<endl; wynik += (m/akt) * (n/akt); if(m%akt!=0) ileDla(m%akt, (n-(n%akt))); if(n%akt!=0) ileDla(n%akt, (m-(m%akt))); if(n%akt!=0 && m%akt!=0) ileDla(n%akt, m%akt); } int main(){ cin.tie(0)->sync_with_stdio(0); long long n, m; cin>>n>>m; cin>>ile; for(int i=1; i<=ile; i++) cin>>tab[i]; ileDla(n, m); if(czy) cout<<wynik<<endl; else cout<<"-1"<<endl; 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 | #include <bits/stdc++.h> using namespace std; constexpr int M = 35; long long tab[M]; long long ile; long long bs(long long v){ long long p = 1, k = ile+1, mid; while(p<k){ mid = (p+k)/2; if(tab[mid] <= v) p = mid + 1; else k = mid; } return tab[p-1]; } long long wynik = 0; bool czy = 1; void ileDla(long long n, long long m){ if(n > m) swap(n, m); long long akt = bs(n); if(akt==0){ czy = 0; return; } //cout<<n<<" "<<m<<endl; //cout<<"zwiekszamy wynik o: "<<m/akt<<" bo dopychamy: "<<akt<<endl; wynik += (m/akt) * (n/akt); if(m%akt!=0) ileDla(m%akt, (n-(n%akt))); if(n%akt!=0) ileDla(n%akt, (m-(m%akt))); if(n%akt!=0 && m%akt!=0) ileDla(n%akt, m%akt); } int main(){ cin.tie(0)->sync_with_stdio(0); long long n, m; cin>>n>>m; cin>>ile; for(int i=1; i<=ile; i++) cin>>tab[i]; ileDla(n, m); if(czy) cout<<wynik<<endl; else cout<<"-1"<<endl; return 0; } |