#include <iostream> #include <vector> #include <algorithm> using namespace std; long long g(long long h, long long m, vector<long long>&v, long long n, long long zs){ long long z=zs; while(z<n){ if(h>=v[z]){ return (m/v[z])*(h/v[z])+g(h%v[z],m,v,n,z); } z++; } return 0; } long long f(long long h, long long w, vector<long long>&v, long long n){ if(h>w){swap(h,w);} if(h==0){return 0;} long long z=0; long long r=0; while(z<n){ if(h>=v[z]){ r=h/v[z]+g(h%v[z],v[z],v,n,z); break; } z++; } //cout<<h<<" "<<w<<" "<<r<<" "<<v[z]<<endl; long long rr=0; rr=(w/v[z])*r; /*while(w>=v[z]){ rr+=r; w-=v[z]; }*/ w%=v[z]; //cout<<" "<<h<<" "<<w<<" "<<r<<" "<<rr<<" "<<v[z]<<endl; return rr+f(h,w,v,n); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long h,w; cin >> h >> w; long long n; cin >> n; vector<long long>v(n); for(auto&i:v){cin>>i;} if(h%v[0] || w%v[0]){cout<<-1<<endl;return 0;} reverse(v.begin(),v.end()); cout<<f(h,w,v,n)<<endl; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; long long g(long long h, long long m, vector<long long>&v, long long n, long long zs){ long long z=zs; while(z<n){ if(h>=v[z]){ return (m/v[z])*(h/v[z])+g(h%v[z],m,v,n,z); } z++; } return 0; } long long f(long long h, long long w, vector<long long>&v, long long n){ if(h>w){swap(h,w);} if(h==0){return 0;} long long z=0; long long r=0; while(z<n){ if(h>=v[z]){ r=h/v[z]+g(h%v[z],v[z],v,n,z); break; } z++; } //cout<<h<<" "<<w<<" "<<r<<" "<<v[z]<<endl; long long rr=0; rr=(w/v[z])*r; /*while(w>=v[z]){ rr+=r; w-=v[z]; }*/ w%=v[z]; //cout<<" "<<h<<" "<<w<<" "<<r<<" "<<rr<<" "<<v[z]<<endl; return rr+f(h,w,v,n); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); long long h,w; cin >> h >> w; long long n; cin >> n; vector<long long>v(n); for(auto&i:v){cin>>i;} if(h%v[0] || w%v[0]){cout<<-1<<endl;return 0;} reverse(v.begin(),v.end()); cout<<f(h,w,v,n)<<endl; } |