#include <bits/stdc++.h> using namespace std; long n; long A[30]; long long cos(long a,long b){ // cout<<a<<" "<<b<<"\n"; if(a==0 or b==0)return 0; long m=n; while(m){ m--; if(a<A[m] or b<A[m])continue; break; } if(a<A[m] or b<A[m])return -1; long as=a%A[m];long heses=b%A[m]; // if(as>heses)a+=as; // else b+=heses; long long p,q,r,s; p=cos(as,heses); q=cos(as,b-heses); r=cos(a-as,heses); s=(long long)(a/A[m])*(b/A[m]); if(p==-1 or q==-1 or r==-1)return -1; else return p+q+r+s; // return cos(as,heses)+cos(as,b-heses)+cos(a-as,heses)+(long)(a/A[m])*(b/A[m]); } int main(){ ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); long h,w;cin>>h>>w; cin>>n; for(long i=0;i<n;i++)cin>>A[i]; long long x=cos(h,w); if(x<0)cout<<"-1"; else cout<<x; cout<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; long n; long A[30]; long long cos(long a,long b){ // cout<<a<<" "<<b<<"\n"; if(a==0 or b==0)return 0; long m=n; while(m){ m--; if(a<A[m] or b<A[m])continue; break; } if(a<A[m] or b<A[m])return -1; long as=a%A[m];long heses=b%A[m]; // if(as>heses)a+=as; // else b+=heses; long long p,q,r,s; p=cos(as,heses); q=cos(as,b-heses); r=cos(a-as,heses); s=(long long)(a/A[m])*(b/A[m]); if(p==-1 or q==-1 or r==-1)return -1; else return p+q+r+s; // return cos(as,heses)+cos(as,b-heses)+cos(a-as,heses)+(long)(a/A[m])*(b/A[m]); } int main(){ ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); long h,w;cin>>h>>w; cin>>n; for(long i=0;i<n;i++)cin>>A[i]; long long x=cos(h,w); if(x<0)cout<<"-1"; else cout<<x; cout<<"\n"; return 0; } |