#include <bits/stdc++.h>
using namespace std;
long long solve(int h,int w,vector<int>d){
while(d.empty()==false&&(d.back()>h||d.back()>w)){
d.pop_back();
}
if(d.empty()==false){
int a=d.back();
int pom1=h/a;
int pom2=w/a;
long long pom3=0;
long long pom4=0;
if(h%a!=0&&w!=0){
pom3=solve(h%a,w,d);
}
if(h-h%a!=0&&w%a!=0){
pom4=solve(h-h%a,w%a,d);
}
long long sol=(long long)pom1*(long long)pom2;
sol+=(long long)pom3;
sol+=(long long)pom4;
return sol;
}
else{
return 0;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int h,w;
cin>>h>>w;
int n;
cin>>n;
vector<int>d;
for(int i=0;i<n;i++){
int k;
cin>>k;
d.push_back(k);
}
if(h%d[0]!=0||w%d[0]!=0){
cout<<"-1"<<'\n';
}
else{
long long ans=solve(h,w,d);
cout<<ans<<'\n';
}
}
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 | #include <bits/stdc++.h> using namespace std; long long solve(int h,int w,vector<int>d){ while(d.empty()==false&&(d.back()>h||d.back()>w)){ d.pop_back(); } if(d.empty()==false){ int a=d.back(); int pom1=h/a; int pom2=w/a; long long pom3=0; long long pom4=0; if(h%a!=0&&w!=0){ pom3=solve(h%a,w,d); } if(h-h%a!=0&&w%a!=0){ pom4=solve(h-h%a,w%a,d); } long long sol=(long long)pom1*(long long)pom2; sol+=(long long)pom3; sol+=(long long)pom4; return sol; } else{ return 0; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int h,w; cin>>h>>w; int n; cin>>n; vector<int>d; for(int i=0;i<n;i++){ int k; cin>>k; d.push_back(k); } if(h%d[0]!=0||w%d[0]!=0){ cout<<"-1"<<'\n'; } else{ long long ans=solve(h,w,d); cout<<ans<<'\n'; } } |
English