C - The Football Season
先考虑求解
[x imes w + y imes d=p
]
若存在一组解
[egin{cases}
x_0\
y_0 = kw + v & (0<v<w)\
end{cases}
]
则
[x_0 imes w + y_0 imes d = p\
Rightarrow x_0 imes w + (kw+v) imes d = p\
Rightarrow x_0 imes w + k imes w imes d + v imes d = p\
Rightarrow (x_0+k imes d) imes w + v imes d = p ~~~~~
]
所以一定存在一组解
[egin{cases}
x = x_0 + k imes d\
y = v
end{cases}
]
并且由于(w> d)
有
[x + y = x_0 + k imes d + v < x_0 + k imes w + v
]
前者比后者更有可能成为答案
所以直接枚举所有的v即可
#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 v=0;v<w;v++){
if((p - v * d) % w == 0){
ll x = (p - v * d) / w;
if(x >= 0 && x + v <= n){
printf("%lld %lld %lld
",x,v,n-x-v);
return 0;
}
}
}
puts("-1");
return 0;
}