#include <bits/stdc++.h> using namespace std; long long f(long long h, long long w, long long i, vector<long long> &d) { if(i==d.size()-1) return ((w/d[i])*(h/d[i])); long long x=h/d[i+1]; long long y=w/d[i+1]; long long h1=x*d[i+1]; long long w1=y*d[i+1]; return ((((h*w-h1*w1)/(d[i]*d[i]))+f(h1,w1,i+1,d))); } int main() { long long h,w,n; cin>>h>>w>>n; vector<long long> d(n); for(int i=0;i<n;i++) cin>>d[i]; if(w%d[0]!=0 || h%d[0]!=0) { cout<<-1<<endl; return 0; } cout<<f(h,w,0,d)<<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 | #include <bits/stdc++.h> using namespace std; long long f(long long h, long long w, long long i, vector<long long> &d) { if(i==d.size()-1) return ((w/d[i])*(h/d[i])); long long x=h/d[i+1]; long long y=w/d[i+1]; long long h1=x*d[i+1]; long long w1=y*d[i+1]; return ((((h*w-h1*w1)/(d[i]*d[i]))+f(h1,w1,i+1,d))); } int main() { long long h,w,n; cin>>h>>w>>n; vector<long long> d(n); for(int i=0;i<n;i++) cin>>d[i]; if(w%d[0]!=0 || h%d[0]!=0) { cout<<-1<<endl; return 0; } cout<<f(h,w,0,d)<<endl; } |