思路:x,y,z肯定不为负数xw+dy=p,直接枚举系数较小的y即可,y的范围:y<w,因为大于w的时候,不如去增加x,这样x+y的和还能保持尽可能小.
1 /* 2 3 x*w+y*d=p; 4 x*w+(Kw+y)*d 5 (x+d)*w+d; 6 7 x+y+z=n; 8 9 */ 10 #include<bits/stdc++.h> 11 12 using namespace std; 13 #define int long long 14 signed main(){ 15 int n,p,w,d;cin>>n>>p>>w>>d; 16 for(int y=0;y<=w;y++){ 17 int temp=p-y*d; 18 if(temp%w) 19 continue; 20 int x=temp/w; 21 if(x+y<=n&&x>=0&&y>=0&&(n-x-y)>=0){ 22 cout<<x<<" "<<y<<" "<<n-x-y; 23 return 0; 24 } 25 } 26 cout<<"-1"; 27 return 0; 28 }