#include <bits/stdc++.h> #define int long long using namespace std; vector<int> kwa; int w=0; void rek(int x,int y,int i){ //cout<<x<<" "<<y<<"\n"; if(x<=0||y<=0)return; if(i>kwa.size()-1){ cout<<"-1"; exit(0); } rek(x-(x%kwa[i]),y%kwa[i],i+1); rek(x%kwa[i],y%kwa[i],i+1); rek(x%kwa[i],y-(y%kwa[i]),i+1); //cout<<w+ y/kwa[i]*x/kwa[i]<<" "<<x<<" "<<y<<"\n"; w += (y/kwa[i])*(x/kwa[i]); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int x,y,n; cin>>x>>y>>n; for(int i=0;i<n;i++){ int a; cin>>a; kwa.push_back(a); } sort(kwa.begin(),kwa.end()); reverse(kwa.begin(),kwa.end()); rek(x,y,0); cout<<w; }
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 | #include <bits/stdc++.h> #define int long long using namespace std; vector<int> kwa; int w=0; void rek(int x,int y,int i){ //cout<<x<<" "<<y<<"\n"; if(x<=0||y<=0)return; if(i>kwa.size()-1){ cout<<"-1"; exit(0); } rek(x-(x%kwa[i]),y%kwa[i],i+1); rek(x%kwa[i],y%kwa[i],i+1); rek(x%kwa[i],y-(y%kwa[i]),i+1); //cout<<w+ y/kwa[i]*x/kwa[i]<<" "<<x<<" "<<y<<"\n"; w += (y/kwa[i])*(x/kwa[i]); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int x,y,n; cin>>x>>y>>n; for(int i=0;i<n;i++){ int a; cin>>a; kwa.push_back(a); } sort(kwa.begin(),kwa.end()); reverse(kwa.begin(),kwa.end()); rek(x,y,0); cout<<w; } |