#include <bits/stdc++.h> using namespace std; vector<long long >g; long long suma=0; void f2(long long h,long long w,long long i) { if(h==0)return ; long long res1=h%g[i]; suma+=(w/g[i])*(h/g[i]); // cout<<suma<<endl; f2(res1,w,i-1); } void f1(long long h,long long w,long long i) { if(w==0)return ; long long res1=w%g[i],res2=h%g[i]; // cout<<res1<<" "<<res2<<"XD"<<endl; suma+=(w/g[i])*(h/g[i]); // cout<<suma<<endl; f2(res2,w-res1,i-1); f1(h,res1,i-1); } int main() { long long h,w,n,a; cin>>h>>w>>n; for(int i=0;i<n;i++) { cin>>a; g.push_back(a); } if(h%g[0]!=0 || w%g[0]!=0) { cout<<-1; return 0; } f1(h,w,n-1); cout<<suma; }
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 | #include <bits/stdc++.h> using namespace std; vector<long long >g; long long suma=0; void f2(long long h,long long w,long long i) { if(h==0)return ; long long res1=h%g[i]; suma+=(w/g[i])*(h/g[i]); // cout<<suma<<endl; f2(res1,w,i-1); } void f1(long long h,long long w,long long i) { if(w==0)return ; long long res1=w%g[i],res2=h%g[i]; // cout<<res1<<" "<<res2<<"XD"<<endl; suma+=(w/g[i])*(h/g[i]); // cout<<suma<<endl; f2(res2,w-res1,i-1); f1(h,res1,i-1); } int main() { long long h,w,n,a; cin>>h>>w>>n; for(int i=0;i<n;i++) { cin>>a; g.push_back(a); } if(h%g[0]!=0 || w%g[0]!=0) { cout<<-1; return 0; } f1(h,w,n-1); cout<<suma; } |