求解目标: \(x*w+y*d=p\)
所以直接枚举所有的\(v\)就好,又因为\((0<v<w)\),所以最多枚举\(1e5\)次
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, p, w, d;
int main()
{
cin >> n >> p >> w >> d;
for(ll y = 0, x; y < w; y++){
if((p-d*y)%w==0) x = (p-d*y)/w;
if(x >= 0 && x + y <= n){
cout << x << " " << y << " " << n-x-y << endl;
return 0;
}
}
puts("-1");
return 0;
}
原文:https://www.cnblogs.com/zxytxdy/p/11670509.html