思路:要找到满足
x+y<=n
x⋅w+y⋅d=p,
->xw+yd+wd-wd=w*(x+d)+d*(y-w)
因为w>d,令x1=x+d,y1=y-w,x1+y1<x+y
只要x⋅w+y⋅d=p成立,w*(x+d)+d*(y-w)也成立,一旦y>w就可以化为y1=y%w使得x1+y1更小的情况,所以只要枚举y=[0,w-1]的所有情况即可
Code:
#include<iostream>
typedef long long ll;
using namespace std;
const int Max = 1e6 + 5;
int a[Max], b[Max];
int gcd(int a, int b)
{
if (a == 0)return b;
return gcd(b % a, a);
}
int main()
{
ll n, p, w, d;
cin >> n >> p >> w >> d;
int f = 0;
for (int i = 0;i <= 1e5;i++)
{
if (i * d > p)break;
if ((p - i * d) % w == 0)
{
if (i + (p - i * d) / w <= n)
{
f = 1;cout << (p - i * d) / w << " " << i << " " << n - (p - i * d)/w - i;
break;
}
}
}
if (f == 0)cout << -1 << endl;
}