C - The Football Season
Consider solving
\ [x \ times w + y
\ times d = p \] If a group Solutions
\ [\ begin {cases} x_0 \\ y_0 = kw + v & (0 <v <w) \\ \ end {cases} \]
is
\ [x_0 \ times w + y_0 \ times d = p \\ \ Rightarrow x_0 \ times w + (kw + v) \ times d = p \\ \ Rightarrow x_0 \ times w + k \ times w \ times d + v \ times
d = p \\ \ Rightarrow (x_0 + k \ times d) \ times w + v \ times d = p ~~~~~ \] Therefore, there must be a set of solutions
\ [\ the begin {cases} x = x_0 + k
\ times d \\ y = v \ end {cases} \] and since \ (w> d \)
There
\ [x + y = x_0 +
k \ times d + v <x_0 + k \ times w + v \] former than the latter is more likely to be the answer
So you can directly enumerate all of the 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\n",x,v,n-x-v);
return 0;
}
}
}
puts("-1");
return 0;
}