#include <iostream> #define ll long long using namespace std; ll tab[35]; int main() { ll h,w; cin>>h>>w; ll P=h*w,p=0,res=0; int n; cin>>n; for(int i = 0 ;i < n ;i ++) cin>>tab[i]; n--; while(tab[n]>w||tab[n]>h) { n--; if(n==-1) { cout<<-1; return 0; } } ll a,b; a=w%tab[n]; b=h%tab[n]; p+=tab[n]*tab[n]*(w/tab[n])*(h/tab[n]); res+=(w/tab[n])*(h/tab[n]); for(int i = n-1; i>=0 ;i --) { ll ile; ile=(h/tab[i])*(a/tab[i])+(w/tab[i])*(b/tab[i])-(a/tab[i])*(b/tab[i]); p+=ile*tab[i]*tab[i]; res+=ile; a%=tab[i]; b%=tab[i]; } if(p==P) cout<<res; else cout<<-1; }
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 | #include <iostream> #define ll long long using namespace std; ll tab[35]; int main() { ll h,w; cin>>h>>w; ll P=h*w,p=0,res=0; int n; cin>>n; for(int i = 0 ;i < n ;i ++) cin>>tab[i]; n--; while(tab[n]>w||tab[n]>h) { n--; if(n==-1) { cout<<-1; return 0; } } ll a,b; a=w%tab[n]; b=h%tab[n]; p+=tab[n]*tab[n]*(w/tab[n])*(h/tab[n]); res+=(w/tab[n])*(h/tab[n]); for(int i = n-1; i>=0 ;i --) { ll ile; ile=(h/tab[i])*(a/tab[i])+(w/tab[i])*(b/tab[i])-(a/tab[i])*(b/tab[i]); p+=ile*tab[i]*tab[i]; res+=ile; a%=tab[i]; b%=tab[i]; } if(p==P) cout<<res; else cout<<-1; } |