We need to find non-negative integers x, y, z such that:
x + y + z = n
w * x + d * y = p
Given n up to 1e12, p up to 1e17, w and d up to 1e5, with w > d.
We need to output any valid triple or -1.
We need to handle huge numbers. Brute force on x up to n is impossible. But note that w and d are small relative to n and p. We can try to solve the linear Diophantine equation.
Let’s denote:
xw + yd = p
x + y <= n (since z >= 0)
We need x >= 0, y >= 0, z = n - x - y >= 0.
We can treat y = (p - w*x)/d, and must be integer and non-negative.
Alternatively, we can solve for x mod d.
Since wx ≡ p (mod d). Because y = (p - wx)/d integer.
Let’s denote g = gcd(w, d). Then equation has solution iff p % g == 0. But also we need non-negative integers.
Given constraints, w and d up to 1e5, we can iterate over possible x modulo d (or up to d). Actually, note that since y must be integer, x must satisfy (p - w*x) % d == 0.
Because d is up to 1e5, we can iterate x from 0 to d-1 to find the smallest x0 such that p ≡ wx (mod d). Then all solutions for x are of the form x = x0 + k(d/g). But careful: The step size for x solutions is d / g, where g = gcd(w, d). Because w*(x + d/g) = wx + wd/g. Since wd/g is multiple of d because w/g is integer, so wd/g = d * (w/g). So incrementing x by d/g changes wx by wd/g, which is divisible by d, so y changes accordingly.
We need x >=0, y >=0, x+y <= n.
Given that w > d, and potentially huge n, we might try to find a solution by iterating over y instead? Since d is small, maybe we can iterate over y from 0 to w-1 (or up to something). Because w and d are small, we can consider that the number of draws y cannot be too large due to p constraint. But p can be huge, so x can be huge. However, we can use modular reasoning.
Observation: Since d is relatively small, we can find the minimal x (mod d). But we can also iterate over y from 0 to w-1, because wx = p - dy, and we need x integer. Since w is up to 1e5, we can loop y from 0 to w-1 (max 1e5) and check if (p - dy) % w == 0 and x >=0 and then compute x and check x + y <= n. This is a common approach: because if we increase y by w, then (p - d(y+w)) = p - dy - dw, and x would change by -dw/w = -d, integer. So solutions in y repeat with period w/g? Actually step for y is w/g. But iterating y from 0 to w-1 covers all residues modulo w. Since we need x integer, (p - dy) must be divisible by w. For each y in that range, compute x = (p - d*y) / w. Then check if x >=0 and y >=0 and x + y <= n. Since w <= 1e5, this is feasible.
But careful: p up to 1e17, so (p - dy) might be negative, but we only consider y such that p - dy >= 0 => y <= p/d. However, we limit y to at most w-1, which is at most 1e5, so that's fine. But what if p is small? For example, p=0, w=5, d=3, y from 0 to 4: y=0 gives x=0 okay; y=3 gives p-9=-9 negative, skip. So it's fine.
Thus algorithm: For y = 0 to w-1 (or maybe min(w-1, p/d)? but p/d can be huge, but we only need up to w-1 because if y >= w, we can rewrite: Let y' = y mod w? But careful: Condition for integer x is (p - dy) % w == 0. If we increase y by w, then p - d(y+w) = p - dy - dw. Since dw is divisible by w? Not necessarily. dw is divisible by w? dw / w = d, integer. Actually dw is obviously divisible by w because w * d / w = d. So (p - dy - dw) mod w = (p - dy) mod w - (dw) mod w = (p - dy) mod w - 0 = (p - dy) mod w. So indeed, if (p - dy) % w == 0, then for y' = y + w, also (p - dy') % w == 0. Thus there is a periodicity: If y0 is a valid y modulo w, then all y = y0 + kw are valid. So we only need to check y from 0 to w-1 that satisfy (p - dy) >= 0? Actually we need also (p - dy) >= 0 to have x >=0. But if (p - dy) is negative, then x negative; we can skip. However, if for a given y0 modulo w, the smallest nonnegative y that gives x >=0 may be larger than w-1. For example, p is small so that y0 (mod w) leads to negative x. But if we consider y = y0 + kw for large k, (p - dy) becomes even more negative. So we only need to consider y such that p - dy >= 0, i.e., y <= floor(p/d). So we can loop y from 0 to min(w-1, floor(p/d)). But floor(p/d) could be huge (p up to 1e17, d as low as 1, so up to 1e17). But we only need to consider y up to w-1, because if a valid y exists with y >= w, we can reduce it by a multiple of w to get a y' in [0, w-1] that also gives integer x? Not necessarily, because if we reduce by w, then (p - d(y-w)) = p - dy + dw. Since p - dy is divisible by w, p - dy = wx, then p - d(y-w) = wx + dw = w*(x+d). So it's also divisible by w. And x' = x+d is integer and non-negative if x>=0. However, we need to ensure that y' = y-w is non-negative? If y >= w, then y' = y-w >= 0. But y' might be still large? Actually if y is valid and y >= w, then y' = y - w is also valid (since subtract w) and yields x' = x + d. Both non-negative if y>=w and x>=0. So we can keep subtracting w until y' < w. But careful: Could it be that y' becomes negative? If y >= w, subtracting w enough times will eventually get to a number between 0 and w-1. So there always exists a valid y in [0, w-1] that yields x' = x + k*d for some k, which is non-negative. However, we also need to satisfy x' + y' <= n? That might not hold even if original (x,y) satisfies x+y <= n. Because when we subtract w from y and add d to x, x+y changes: (x+d) + (y-w) = x+y + (d-w). Since d < w, d-w is negative, so x'+y' = x+y - (w-d). So it decreases by (w-d). So if original x+y <= n, then x'+y' may also be <= n? Actually it might become less, so still <= n if original was <= n. But it's possible that x'+y' may become negative? No, still non-negative. So if there exists a solution (x,y,z) with x+y <= n, then by repeatedly applying the transformation (y -> y - w, x -> x + d) we can get another solution with y in [0,w-1] and still x+y <= n? Wait: The transformation reduces y by w and increases x by d, so x+y decreases by (w-d). Since w-d > 0, x+y decreases. So if original x+y <= n, then certainly after transformation x'+y' <= n. So we are safe: existence of a solution implies existence of a solution with y in [0, w-1]. However, we also need to ensure that after transformation, x and y are non-negative: y must be >=0, so we stop when y < w, and since we subtract w repeatedly, we eventually get y in [0, w-1]. Provided that the original y >=0; if y < w already, it's already in that range. So indeed we can restrict search to y in [0, w-1] without loss of generality. That's a key insight.
But there is a subtlety: The transformation might need to be applied many times, but we don't need to simulate; we can just check each y in 0..w-1 for which (p - dy) % w == 0 and (p - dy) >= 0, and then compute x = (p - d*y) / w. Then check if x + y <= n. This yields a candidate. If such candidate exists, we output (x, y, n-x-y). If none exists, output -1.
But is this sufficient? Let's test with an example: n=30, p=60, w=3, d=1. y from 0 to 2? w=3 => 0..2. Check y=0: p-d*y=60, %3=0, x=20, x+y=20 <=30 => candidate (20,0,10). But the example output is (17,9,4). Both are valid. So it works.
Second example: n=10, p=51, w=5, d=4. w=5, iterate y=0..4. y=0: (51) %5=1, no. y=1: 51-4=47 %5=2, no. y=2: 51-8=43 %5=3, no. y=3: 51-12=39 %5=4, no. y=4: 51-16=35 %5=0, x=7, but x+y=7+4=11 > n=10, so invalid. No other y, so answer -1. Good.
Third example: n=20, p=0, w=15, d=5. w=15, y=0..14. y=0: (0)%15=0, x=0, x+y=0 <=20 => output (0,0,20). Works.
Edge cases: p may be huge, but we only compute modulo and division with 64-bit integers (Python can handle big integers). In C++ we'd need 128-bit but here we are reasoning in Python, so it's fine.
But wait: The transformation argument assumed that we can reduce y by w while increasing x by d. This is valid because wx + dy = p. If we subtract w from y and add d to x: w*(x+d) + d*(y-w) = wx + wd + dy - dw = wx + dy = p. So it's indeed an equivalent solution. However, we must ensure that after transformation, x and y are non-negative. If we start from a solution with y >= w, we can apply the transformation to get y' = y - w (>=0) and x' = x + d (>=d>0). So y' remains non-negative and x' becomes larger. This is fine. So any solution with y >= w yields another solution with smaller y. So there is always a solution with y < w if any solution exists at all. Provided that the transformation doesn't make x negative. Since we add d to x, x' >= d >= 1 > 0, so positive. So it's safe. But what if the only solution has y < w but x negative? That can't happen because x must be >=0. So we only need to check y in [0, w-1] that give x >=0.
One more nuance: The step of subtracting w from y and adding d to x reduces y by w and increases x by d. This transformation works because d and w are positive. But we also need to consider the possibility that the only solution has y < w but x + y > n? Could there be a solution with y >= w that satisfies x+y <= n but the reduced version with y < w might also satisfy? As argued, reduced version reduces total by (w-d), so if original total <= n, reduced total is even smaller, so still <= n. So if any solution exists with x+y <= n, then there exists a solution with y < w and x+y <= n. So our search should find it. Good.
But wait: Could there be a solution where y < w but x is negative? Not valid. So we only consider y where (p - d*y) is non-negative and divisible by w.
One might also consider that w can be as small as 2, d as 1, but our loop up to w-1 is at most 1e5, which is fine.
Potential pitfalls: p is up to 1e17, w up to 1e5, so the loop of 1e5 is fine. However, we also need to consider the possibility that w could be as large as 1e5, so loop of 1e5 is fine in Python.
Edge case: p may be negative? No, p >=0.
Now, consider scenario where there is a solution but the y that works in [0,w-1] gives x such that x+y > n, but maybe another solution with y >= w (but same residue) could have a larger total? Wait, the transformation we described reduces total by (w-d). So if we go in the opposite direction: from a candidate with y in [0,w-1] we can increase y by w and decrease x by d (provided x >= d) to get another solution. That would increase total by (w-d). So if we have a candidate from the small y that yields x+y > n, perhaps we can increase y (and decrease x) to get a solution with smaller total? Actually total = x+y. If we increase y by w and decrease x by d, the total becomes (x - d) + (y + w) = x+y + (w-d). So total increases, not decreases. So that doesn't help if x+y > n because we need total <= n; we need to reduce total, not increase. So if the candidate with small y already has x+y > n, then any other solution obtained by adding multiples of w to y (and subtracting d from x) would have even larger total, because total increases by (w-d) each step. Conversely, if we start from a solution with y >= w, we can reduce total. So if the only solution with y < w yields x+y > n, then there might exist a solution with y >= w that yields a smaller total? Let's examine: Suppose we have a solution with y in [0,w-1] such that x+y > n. Could there exist another solution with a larger y but smaller total? Let's see: For a given residue class modulo w (say y ≡ r mod w), the general solution is y = r + kw, x = (p - dy)/w. Then total T(k) = x + y = (p - d*(r + kw))/w + (r + kw) = p/w - (d/w)(r + kw) + r + kw = p/w + r(1 - d/w) + kw(1 - d/w) = (p + r*(w - d) + kw(w - d)) / w? Let's compute properly:
x = (p - dy)/w.
T = x + y = (p - dy)/w + y = p/w + y*(1 - d/w) = p/w + y*( (w-d)/w ).
Since p/w might not be integer, but when y is a solution, p - dy is divisible by w, so p/w - (dy)/w is integer. But the expression: T = (p - dy + wy)/w = (p + y*(w - d))/w.
Thus T = (p + (w-d)*y)/w.
Since w-d > 0, T is an increasing linear function of y (because numerator increases as y increases). So for a given residue class, as y increases, total T increases. Conversely, as y decreases, T decreases. So the minimal T for that residue class is achieved at the smallest y that yields a valid solution with x >=0. Since y must be >= r (the residue) and >=0, the minimal y is the smallest non-negative y in that residue that makes x non-negative (since x = (p - d*y)/w >= 0 => y <= p/d). So if we have a solution with y = r (the minimal residue), then it gives the smallest total for that residue. So if that minimal total > n, then all other solutions in that residue class have larger totals, so none can satisfy T <= n. So checking y in [0,w-1] (the minimal y for each residue) is sufficient. Because if the minimal total for a residue class exceeds n, then no solution in that class can work. Conversely, if the minimal total is <= n, then that solution itself works (since it's minimal y and thus minimal total, but wait minimal total may be <= n, but is that solution valid? Yes, it gives x>=0, y>=0, total <= n). So we are good.
Thus algorithm: Loop y from 0 to w-1, compute if (p - dy) % w == 0 and (p - dy) >= 0. If yes, compute x = (p - d*y) // w, then if x + y <= n, we have solution: output x, y, n-x-y. If after loop no such y, output -1.
But we must also consider that p could be so large that p - d*y may overflow? Python handles big ints.
Complexity O(w) which is up to 1e5, fine.
But wait: There might be an issue if w is large (1e5) and we loop w times, but p is huge, but each iteration constant, fine.
Edge case: w could be as high as 1e5, but we also have to consider that maybe p/d is small, so many y in 0..w-1 will yield negative p - dy. We can break early if dy > p? Since dy increases with y, we could break when dy > p. But w can be 1e5 and d could be 1, p could be 0, then only y=0 works, but we loop 1e5 anyway, that's okay. 1e5 loops is trivial.
But consider the possibility that p is huge, 1e17, and w=1e5, d=1e5-1? We loop 1e5, fine.
Thus solution is straightforward.
But wait: Could there be a scenario where the minimal y for a residue is > w-1? Actually, for each residue r mod w, the smallest non-negative integer y such that y ≡ r mod w is r itself, which is between 0 and w-1. However, that y may not satisfy x >= 0 if p - dy < 0. But if p - dy < 0, then the minimal y that gives non-negative x might be larger than r (adding w) but that would increase y and thus total T. So if for a given residue r, the minimal y that yields non-negative x is actually r + kw for some k>0, then the total T for that y is larger than if we had used y=r (but y=r gives negative x, invalid). Could it be that for some residue class, the smallest y that yields x>=0 is > w-1, but still the total T might be <= n while any solution with y in [0,w-1] (which are of different residues) maybe not working? Wait, each residue class corresponds to a specific remainder modulo w. The residues r from 0 to w-1 cover all possible residues. If for a given residue r, the smallest y with that residue that yields x>=0 is y = r + tw where t>=1 (because y=r gave negative x). That means p - dr < 0, i.e., dr > p. Since p is non-negative, this implies r > p/d. Since r <= w-1, and p/d might be less than w-1. So indeed, for residues r > floor(p/d), the minimal y that yields x>=0 is > r? Actually, if p - dr < 0, then we cannot use y=r because x would be negative. To get x>=0, we need y <= p/d. So if r > p/d, then y=r is invalid. The next candidate in that residue class is y = r + w, but that would be even larger, and d(r+w) > dr > p, so still p - dy is even more negative? Wait: If r > p/d, then dr > p. Adding w increases y, so d(r+w) > dr > p, so p - dy is still negative. Actually, if dr > p, then for any y > r, dy > dr > p, so p - dy is even more negative. So there is no y in that residue class that yields x>=0 because all y in that residue class are >= r, and since r > p/d, all are > p/d, so x negative. Therefore, residues r > floor(p/d) yield no valid solution at all. So we only need to consider y up to min(w-1, floor(p/d)). So we can limit loop to y <= min(w-1, p//d). That reduces iterations if p/d is small.
But it's optional.
Thus algorithm works.
Double-check: Could there be a solution with y in [0,w-1] that yields x + y <= n but x is not integer? We only consider those where (p - d*y) % w == 0. So it's fine.
Now verify with some tricky cases.
Case 1: n=1e12, p=1e17, w=100000, d=99999. We loop up to w=100000. For each y we compute mod. That's fine.
Case 2: Very large n but small p, we might find solution with y small, x small, z large.
Edge: p may not be divisible by g = gcd(w,d). But our loop will not find any y because (p - dy) % w == 0 means p ≡ dy mod w. Since y runs over 0..w-1, we might find a y such that d*y mod w equals p mod w. But existence requires p mod gcd(w,d) to be 0. If not, loop will not find any. That's correct.
Thus solution is simple.
But note: The constraints: n up to 1e12, p up to 1e17, w and d up to 1e5. Our loop is 1e5, fine.
Implementation details:
Read n, p, w, d as integers.
Loop for y in range(0, w): (or range(min(w, p//d + 1))? But p//d may be 0 and w maybe 100000, still fine. But if p is huge, p//d may be huge, but we only go up to w-1 anyway. So just loop for y in range(w). However, we must be careful that for large p, (p - d*y) might overflow? Python unlimited int, fine. But in C++ we'd use long long and careful.
Additionally, we can early break when dy > p, because then (p - dy) negative and will remain negative for larger y. Since y increases, we can break. But note that in Python, loop to w is fine.
Thus:
for y in range(w):
if dy > p:
break
if (p - dy) % w == 0:
x = (p - d*y) // w
if x + y <= n:
z = n - x - y
print(x, y, z)
return
print(-1)
But wait: What if p - dy is negative? We break when dy > p, that's fine. But we need to consider the possibility that p is exactly 0 and d>0, then only y=0 passes; for y=0, d*y=0, not > p, so condition works.
But what if w is 0? No, w>=1.
But also what if w is 1? Then loop from 0 to 0 only, y=0. That's okay.
But what about d = 0? The problem states d >= 1, so fine.
But there is nuance: In the condition (p - dy) % w == 0, we compute modulo. For negative numbers, Python's % gives non-negative remainder, but if p - dy is negative, we skip because we break when d*y > p. So we only consider non-negative.
Now consider possibility that for some y where p - dy is non-negative but (p - dy) % w == 0, but x = (p - d*y) // w might be huge, but x+y <= n? We check that.
Now, is it always safe to loop only up to w-1? Let's provide formal reasoning:
We want to find integers x>=0, y>=0 such that wx + dy = p and x+y <= n. Let (x,y) be any solution. Consider y mod w. Let r = y mod w, 0 <= r < w. Then define y0 = r, and x0 = (p - dy0)/w? But is x0 integer? Since y ≡ y0 (mod w), we have dy ≡ dy0 (mod w) because dw is multiple of w? Actually dy mod w = d(r + kw) mod w = dr mod w (since dkw ≡ 0 mod w). So if wx = p - dy, then p - dy ≡ 0 mod w, so p ≡ dy mod w => p ≡ dr mod w. Therefore p - dr is divisible by w. So x0 = (p - dr)/w is integer. Also, since y = r + kw, we have x = (p - d*(r + kw))/w = (p - dr)/w - dk = x0 - dk. Since x >= 0, we need x0 - dk >= 0 => k <= x0/d. But we don't need to find k; we can just note that for the residue r, the solution with minimal y (i.e., y=r) gives x0 potentially larger than x. But is x0 necessarily non-negative? Since p - dr might be negative? Could it? If p - dr < 0, then r > p/d, but then p ≡ dr mod w and r > p/d, but then also for any k>=0, y=r + kw > r > p/d => p - dy <0, so x would be negative, impossible. So for a solution to exist, we must have p - dr >= 0, i.e., r <= p/d. Hence r is within [0, floor(p/d)] and also within [0, w-1]. So the residue r that works must satisfy p - dr >= 0. Thus if we loop y from 0 to w-1, we will encounter y = r (since r < w) such that p - dy >= 0 and (p - dy) % w == 0 (by construction). And then compute x = (p - dy)/w. This x might be larger than the x in the original solution? Possibly, but it's non-negative and gives a valid (x,y) pair. Now, we need to ensure that x + y <= n. For the original solution we had x + y <= n. But the candidate (x0,y0) from the minimal y may have x0 + y0 =? Since y0 = r <= y (original y) and x0 = x + dk (since x = x0 - dk, so x0 = x + dk). So x0 + y0 = (x + dk) + (y - kw) = x + y + k*(d - w). Since d - w is negative, this sum is less than x + y. Because k >= 1? Actually if original y = r + kw with k>=0, then k = (y - r)/w. If original y >= w, then k>=1. Then x0 + y0 = x + y - k(w - d). Since w - d >0, x0 + y0 <= x + y. Therefore, if original (x,y) satisfies x+y <= n, then (x0,y0) satisfies x0 + y0 <= n as well. So the minimal residue solution will also satisfy the sum constraint. Therefore, if any solution exists, there exists one with y in [0,w-1] that also meets sum constraint. Hence our search is complete.
Thus algorithm is correct.
Now, note: There is also a potential alternative: iterate x from 0 to d-1 similarly because w and d are small. But we already have a simple loop.
Now, we must ensure we use Python's arbitrary precision ints.
Edge Cases:
-
p = 0: Then only solution is x=0, y=0, provided that n>=0. Our loop: y=0 gives (p - 0) % w = 0 % w = 0, x=0, check x+y=0 <= n => output (0,0,n). If n=0? n>=1 per constraints? n>=1? Actually n>=1 (1 ≤ n). So it's fine.
-
w and d large but w=100000, d=1, p huge. Loop 100000, each mod operation fast.
-
n very large, but x+y <= n check may overflow? In Python fine.
Thus solution seems trivial.
But wait, there is a twist: The problem statement: "If there is no answer, print -1." It also says "The numbers should meet the following conditions: x ⋅ w + y ⋅ d = p, x + y + z = n."
We output three non-negative integers.
Now, potential pitfalls: Our loop only goes up to w-1. However, what if w is very small, like w=2, d=1, but p is huge and n is huge. Loop y=0,1. For y=0: if p even, x=p/2; y=0. Check x+y <= n? Possibly. For y=1: if p-1 even? Actually p-1 must be divisible by 2: p odd => p-1 even. So both residues covered. Good.
But consider case where p mod gcd(w,d) != 0. Then no solution exists. Our loop will find no y, output -1.
Now test some random cases:
Case: n=5, p=10, w=4, d=2. Loop y=0..3.
y=0: 10%4=2 !=0
y=1: 10-2=8%4=0, x=2, x+y=3 <=5 => output (2,1,2) valid? 24+12=8+2=10, sum=5. Good.
Alternative: y=2: 10-4=6%4=2; y=3: 10-6=4%4=0, x=1, x+y=4 <=5 => (1,3,1) also valid. So multiple.
Now case: n=5, p=9, w=4, d=2. y=0:9%4=1; y=1:7%4=3; y=2:5%4=1; y=3:3%4=3. None => -1. Indeed 4x+2y=9 has no integer non-negative solution because left even, right odd.
Now case: n=100, p=1e17, w=100000, d=99999. Need to check if there is a solution. w and d are coprime? gcd(100000,99999)=1. Then there always exists a solution mod w? Actually p mod 1 =0 always, so solvable. But need to find y such that (p - d*y) % w ==0. Since d and w are coprime, we can compute modular inverse, but we just loop.
Potential performance: Loop up to 100000 is fine.
But consider w=100000, d=99999. p = something huge. For y from 0 to 99999, compute (p - d*y) % w. That's 100k operations, fine.
Now, memory: O(1).
Now, is there any scenario where w is huge but d is also huge? w,d up to 1e5. So it's fine.
Thus solution is straightforward.
But wait, there is also constraint that w > d. So d < w. Good.
Now, could there be a situation where w is 1e5 and d is 1, and p is 1e17, and n is small such that x+y <= n fails for all residues? For example, n=1, p=1e17, w=100000, d=1. Then we need x+y <=1. But x is huge if any solution exists. For p=100000x + 1y = huge. The minimal total T = (p + (w-d)*y)/w. Since w-d = 99999, with y minimized (0) we get T = p/w ≈ 1e12, far > n=1. So no solution. Our loop: y=0: x = p/w = 1e12, x+y>1 -> no; y=1: x = (p-1)/w ≈ 1e12-? still huge; all y up to w-1 give x roughly p/w, so total >1. So output -1. That's fine.
Now, potential overflow in languages like C++ but we are using Python.
Now, we need to write code accordingly.
But there is another nuance: In some cases, the loop may be too slow if w = 100000 and we do many operations? 100k is trivial.
Thus solution is final.
But wait, we must be careful: The loop from 0 to w-1 may include y where (p - dy) is negative? We break if dy > p. That's fine.
One more nuance: What if w is 0? Not possible.
Thus we can implement.
But before finalizing, let's double-check the logic with more edge cases.
Edge case: p is not divisible by gcd(w,d). Then no y will work because (p - dy) % w == 0 implies p ≡ dy (mod w). The set of possible d*y mod w is multiples of g. So if p mod g != 0, impossible. Our loop will simply find none.
Edge case: p = 0. Then only solution is x=0, y=0, z=n. Our loop: y=0: (0 - 0) % w = 0, x=0, check x+y=0 <= n, ok. So output.
Edge case: n=0? But n≥1.
Edge case: d = w-1, w large. Fine.
Now, could there be a scenario where our loop finds a y that gives x such that x+y <= n, but x might be negative? We check (p - dy) >= 0 before mod? Actually we break when dy > p, but if dy == p, then p - dy = 0, x=0, non-negative. So it's fine.
But what if p - d*y is positive but not divisible by w, we skip.
Now, confirm with sample:
Sample1: n=30,p=60,w=3,d=1. Loop y=0..2.
y=0: (60)%3=0 => x=20, x+y=20<=30 -> output (20,0,10). That's valid but not the given. But we output any.
Our code would output first found (20,0,10) if we loop from 0. That's accepted. Or if we break early after finding, it's fine. So we don't need to match exact sample.
Sample2: as before gives -1.
Sample3: as before gives (0,0,20).
Now test custom: n=10, p=31, w=5, d=2. w=5,d=2. Loop y=0..4.
y=0:31%5=1 no
y=1:29%5=4 no
y=2:27%5=2 no
y=3:25%5=0 -> x=5, x+y=8 <=10 => output (5,3,2). Check:55+32=25+6=31, sum=10 => valid.
All good.
Now think about potential pitfalls: The loop goes up to w-1. But what if w is 100000 and p/d is less than w-1? Then we break early when d*y > p. That's efficient.
But what if d = 0? Not allowed.
Now, note: There is also the possibility that p is huge but w is small, so loop of w is fine.
Thus implement.
But sometimes in these problems, they expect a solution using extended Euclid to avoid O(w) due to w up to 1e5, but O(1e5) is fine. However, note that w can be 1e5 but we also have multiple test cases? The problem is a single test case. So O(1e5) is fine.
Now, ensure we use integer division //.
Now, potential integer division and modulo with negative numbers: we avoid negative.
Now, code structure:
def solve():
import sys
n,p,w,d = map(int, sys.stdin.readline().split())
for y in range(min(w, p//d + 1)): # but p//d may be huge, but min with w is w. Actually p//d+1 may be > w, but we only need up to w-1. So range(min(w, p//d + 1)) is safe.
But careful: If p is 0, p//d = 0, +1 = 1, range(1) gives y=0 only. Good.
If p is huge and p//d + 1 > w, then min(w, ...) = w, so loop up to w-1. So use range(w) but break when d*y > p.
Simplify: for y in range(w):
if dy > p:
break
if (p - dy) % w == 0:
x = (p - d*y) // w
if x + y <= n:
print(x, y, n - x - y)
return
print(-1)
But note: The loop condition using break only works if we guarantee that d*y increases monotonically, which it does. So it's fine.
Now, consider the case where d = 0? Not allowed.
Now, what about the scenario where p is large and w is small but d is also small, p/d may be > w. Then we loop full w times. That's okay.
Now, confirm correctness with mathematical reasoning.
Alternate method: Since w and d are up to 1e5, we could also iterate x up to d-1, similar logic. But our y iteration is fine.
Now, check for potential off-by-one: range(w) yields 0..w-1 inclusive? In Python, range(w) yields 0 to w-1. That's correct.
Now, what about the case where w is 1e5 and p/d is large, but we break early? Actually we break when dy > p. But if d is small, say d=1, p=1e17, then dy > p won't happen until y > 1e17, but we only loop up to w-1=99999, so we never break (since d*y at y=99999 is 99999, far less than p). So we loop full 100k times. That's fine.
Now, complexity: 100k iterations, each with a modulo operation, and possibly large integers (p up to 1e17, subtracting dy which is up to 1e51e5? Actually dy <= d(w-1) <= 1e5*1e5 = 1e10, which is less than 1e17, so fine). Modulo w (up to 1e5) is cheap.
Thus the solution is efficient.
Now, let's double-check if there could be a situation where the minimal y (the residue) yields x + y > n, but some larger y in the same residue class yields x+y <= n? Earlier we argued that T = (p + (w-d)*y)/w is increasing in y. Because w-d>0. So larger y gives larger total. So if the minimal y yields total > n, then any larger y yields even larger total > n. So no.
But wait, is T strictly increasing? T = (p + (w-d)*y)/w. Since w-d > 0, as y increases, numerator increases, denominator constant, so T increases. So yes.
Thus minimal y gives minimal total for that residue class.
Thus checking only y in [0,w-1] is sufficient.
Now, think about the case where the solution with minimal y may have x negative? But we ensure p - dy >=0, so x non-negative. If p - dy negative, then no solution for that residue because y must be at least that residue, and any larger y in that class makes it even more negative. So those residues are invalid.
Thus done.
Now, edge case: w and d may not be coprime, but that doesn't affect.
Now, verify with random brute force for small numbers to ensure code works.
Let's do mental test: n=5, p=7, w=4, d=3. Loop y=0..3.
y=0: 7%4=3 -> no.
y=1: 7-3=4%4=0 -> x=1, x+y=2 <=5 => output (1,1,3). Check: 14+13=7, sum=5 -> valid.
So code returns (1,1,3).
Now test where no solution due to sum constraint: n=1, p=7, w=4, d=3. y=1: x=1, sum=2 >1; no other y. Output -1. Actually there is no solution.
Now test where multiple residues work but only some satisfy sum: n=2, p=7, w=4, d=3. y=1: x=1, sum=2 <=2 => (1,1,0) valid.
Now test where p is exactly n*d? n=5, p=15, w=5, d=3. y=0:15%5=0, x=3, sum=3<=5 => (3,0,2). Also y=5? But loop only y up to 4. y=5 not considered but y=0 works.
Now test where d=w-1 and p = something like p = n*w? Actually p can be up to 1e17.
Now, consider case where w is 100000, d=99999, p = 99999* n? etc.
Now, potential issue: In some languages, we need to be careful with integer overflow when computing d*y, but Python handles.
Now, code will be short.
But there is a nuance: In some problems, they might set w up to 1e5 but n up to 1e12, and our algorithm uses O(w) which is fine. However, there is an even more efficient approach using extended Euclidean algorithm to solve directly, but not needed.
Thus final answer: Implement as described.
Now, let's consider if there is any scenario where the loop up to w-1 might not capture a solution because the solution requires y >= w? But we argued that if any solution exists, there exists one with y < w. Let's prove: Suppose (x,y) is a solution with y >= w. Then we can replace (x,y) with (x+d, y-w). Check: w*(x+d) + d*(y-w) = wx + wd + dy - dw = wx + dy = p. And x+d >= d >= 1 >0, y-w >=0. So new y' = y-w is smaller. Repeat until y' < w. So we get a solution with y' in [0, w-1]. Also x' = x + kd for some k, which is >= x. So if original satisfied x+y <= n, then x'+y' = x+y - k(w-d) <= x+y <= n. So new solution also satisfies sum constraint. So indeed there exists a solution with y < w. Good.
Now, what if the transformation leads to negative x? Since we only subtract w from y when y >= w, we add d to x. x may become larger, never negative. So fine.
Thus algorithm is correct.
Now, one more nuance: The problem states x, y, z should be non-negative integers. Our computed x and y are non-negative. And z = n - x - y, which could be negative if x+y > n, so we check that.
Now, implementation details: We must use Python's fast I/O? Not necessary for single input.
Now, test a tricky case: n=1000000000000, p=5000000000000, w=100000, d=99999. We need to check if any solution. Let's compute modulo: we need (p - 99999y) % 100000 == 0 => p mod 100000 =? p=5e12 = 5000000000000, mod 100000 = 0? 5e12 / 100000 = 5e7 exactly? 100000 * 5e7 = 5e12, yes so p mod w = 0. Then we need y such that 99999y ≡ 0 mod 100000. Since 99999 ≡ -1 mod 100000, so we need -y ≡ 0 mod 100000 => y ≡ 0 mod 100000. So y must be multiple of 100000. Minimal y is 0. Then x = (p - 0)/w = 5e12/1e5 = 5e7 = 50000000. x+y = 50000000 <= n=1e12? 5e7 < 1e12, so valid. So output (50000000,0,950000000000). Works.
Our loop: y from 0 to 99999. y=0: (p - 0)%100000 == 0 -> x=5e7, sum ok => output.
Now, consider a case where p is not divisible by w but divisible by d? e.g., n=10, p=6, w=4, d=2. y from 0..3.
y=0: 6%4=2 not 0
y=1: 4%4=0 -> x=1, sum=2 <=10 => (1,1,8). Good.
Now, consider case where w and d are not coprime and p is divisible by g but still we need to find y. It works.
Now, consider case where p is 0, w=3, d=2, n=5. y=0: 0%3=0, x=0, sum=0<=5 => (0,0,5). Works.
Now, consider case where p is huge and w is huge but y loop still works.
Now, check for potential time: 100000 iterations, each modulo operation on big integers up to 1e17, trivial.
Now, one might ask: Could w be up to 10^5 and we need to do 10^5 modulo operations, each O(1) for small ints? Actually Python's big int modulo operation for numbers up to 1e17 is O(1) because they fit in 64-bit? Actually Python uses arbitrary precision, but numbers up to 1e17 fit within 64 bits, so operations are fast.
Thus solution is good.
Now, I'll produce final answer.
But wait, there is an alternative: we could also iterate x up to d-1. But the y approach is fine.
Now, we need to output -1 if no solution.
Now, check if any scenario where our loop finds a solution but z becomes negative? We check x+y <= n.
Now, one more thought: The condition x+y+z = n with z >= 0. We compute z = n - x - y. That's integer, non-negative if condition holds.
Now, we might consider the possibility that n is extremely large, but x+y might be extremely large too. x computed as (p - d*y)//w, could be up to p/w which can be up to 1e17/1 = 1e17, and n is up to 1e12, so x could be >> n, causing x+y > n. That's why we check.
Now, I'll implement.
But be careful: The input numbers can be up to 10^12 and 10^17, which fit in Python int.
Now, the code:
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
n, p, w, d = map(int, data)
# loop y from 0 to w-1
for y in range(w):
if d * y > p:
break
if (p - d * y) % w == 0:
x = (p - d * y) // w
if x + y <= n:
print(x, y, n - x - y)
return
print(-1)
if name == "main":
solve()
But we must consider that w could be as high as 100000, and we use range(w). That's fine.
Now, verify with sample:
Input: "30 60 3 1" -> output first match: y=0 -> x=20, sum=20 <=30 -> prints "20 0 10". Acceptable.
But the sample output is "17 9 4". Both are correct. So fine.
Now, any test where y=0 yields solution but y>0 yields also solution but we output the first.
Now, what about potential integer overflow in d*y? d and y up to 1e5, product up to 1e10, within Python int.
Now, check if we need to use faster I/O? Not needed.
Now, but there's a nuance: The problem constraints: n up to 1e12, p up to 1e17, w and d up to 1e5. Our loop is O(1e5), fine.
However, some people might think of using extended Euclid to solve in O(log w) time. But O(1e5) is simpler.
Now, confirm with borderline: w=100000, d=1, p=10^17. Loop runs 100k times, each iteration compute d*y (up to 100k) and mod. That's fine.
Now, but consider a scenario where p is so large that p - dy might be negative for early y? Actually dy starts from 0, so initial y are fine. For y near w-1, dy up to d(w-1) <= 1e5*1e5 = 1e10, which is far less than 1e17, so still positive. So we will never break early if p is large. So loop always runs full w times. That's 100k iterations, fine.
Now, could there be a scenario where w is larger than 1e5? No, max 1e5.
Thus solution passes.
Now, I'll produce final answer with explanation.
But hold on: The problem states that w and d are up to 10^5, but they also said 1 ≤ d < w ≤ 10^5. So w max 100000.
Thus our solution is O(w) time, O(1) memory.
Now, one more nuance: The input may have trailing spaces, read fine.
Now, I'll craft final answer.
But before final, test with random brute force for small numbers to ensure no missed cases. Let's simulate mentally: n=7, p=13, w=5, d=3. Loop y=0..4.
y=0: 13%5=3 no
y=1:10%5=0 => x=2, sum=3 <=7 -> (2,1,4). Works: 25+13=10+3=13, sum=2+1=3 <=7, z=4.
Now, try n=7, p=14, w=5,d=3. y=0:14%5=4; y=1:11%5=1; y=2:8%5=3; y=3:5%5=0 -> x=1, sum=4 <=7 -> (1,3,3) works: 5+9=14, sum=4 <=7.
Now, n=7, p=15, w=5,d=3. y=0:15%5=0 -> x=3, sum=3 <=7 -> (3,0,4) works.
Now, n=7, p=16, w=5,d=3. y=0:16%5=1; y=1:13%5=3; y=2:10%5=0 -> x=2, sum=4 <=