<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

141. Jumping Joe

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


Joe is a frog who likes to jump a lot. In fact, that's all he does: he jumps forwards and backwards on the integer axis (a straight line on which all the integer numbers, both positive and negative are marked). At first, Joe sits next to the point marked with 0. From here, he can jump in the positive or the negative direction a distance equal to either x1 or x2. From the point where he arrived, he can jump again a distance equal to x1 or x2, in the positive or the negative direction and so on.. Joe wants to arrive next to the point marked with the number P, after exactly K jumps. You have to decide whether such a thing is possible.


Input

The input will contain four integers: x1, x2 (0 < x1 , x2 < 40 000), P (-40 000 < P  < 40 000) and K (0 <= K < 2 000 000 000), separated by blanks.


Output

The first line of output will contain the word "YES", in case Joe can reach the point marked with P after exactly K jumps, or "NO", otherwise. In case the answer is "YES", the next line should contain four integers, separated by blanks: P1 , N1 , P2 and N2. P1 is the number of times Joe jumped in the positive direction a distance equal to x1. N1 is the number of times Joe jumped in the negative direction a distance equal to x1. P2 is the number of times Joe jumped in the positive direction a distance equal to x2. N2 is the number of times Joe jumped in the negative direction a distance equal to x2. In other words, you should find four non-negative integers, so that:

P1*x1 - N1*x1 + P2*x2 - N2*x2 = P
P1 + N1 + P2 + N2 = K

In case there are more quadruples (P1,N1,P2,N2) which are solutions for the problem, you may print any of them.


Sample Input

2 3 -1 12
Sample Output

YES
1 0 5 6
Author	: Mugurel Ionut Andreica
Resource	: SSU::Online Contester Fall Contest #2
Date	: Fall 2002

<|response|>
1. Abridged problem statement  
Given positive integers x1, x2 (≤ 4·10^4), a target integer P (|P| < 4·10^4) and a total jump count K (0 ≤ K < 2·10^9). You start at position 0 on the integer line. Each jump moves you by either +x1, –x1, +x2 or –x2. Decide if you can end exactly at P after exactly K jumps. If yes, output one quadruple (P1, N1, P2, N2) of non-negative integers so that  
  P1+N1+P2+N2 = K  
  P1·x1 – N1·x1 + P2·x2 – N2·x2 = P.  
Otherwise print NO.

2. Key observations  
- Let a = P1 – N1 (net number of x1-jumps) and b = P2 – N2. Then the displacement equation is  
  a·x1 + b·x2 = P.  
- Total jumps used by those net moves is |a|+|b|. The remaining R = K – (|a|+|b|) jumps must come in cancelling ± pairs (like +x1 then –x1) so as not to affect the net displacement. That is only possible if R ≥ 0 and R is even.  
- Therefore feasible (a,b) must satisfy  
  (1) a·x1 + b·x2 = P  
  (2) S = |a|+|b| ≤ K  
  (3) S ≡ K (mod 2).

3. Full solution approach  
Step A: Solve a·x1 + b·x2 = P.  
  Compute g = gcd(x1,x2) and one particular solution (a0,b0) via the extended Euclidean algorithm. If g ∤ P, no solution → print NO. Otherwise scale (a0,b0) by P/g.

Step B: Parameterize all integer solutions by t:  
  a(t) = a0 + t·(x2/g),  
  b(t) = b0 – t·(x1/g).

Step C: Minimize S(t) = |a(t)|+|b(t)| over integer t.  
  Since S(t) is convex‐like, do a local “hill‐descent”: repeatedly try t+1 and t–1 while S decreases.

Step D: Check feasibility.  
  Let S = |a|+|b| for the optimized (a,b). If S > K, print NO. Else let R = K – S.  
  If R is even, proceed. If R is odd, we need to flip parity of S by shifting t by ±1. Such a shift changes S by |a±dx|+|b∓dy| – S, and flips parity only if dx+dy is odd, where dx = x2/g, dy = x1/g. If (dx+dy) is even, parity cannot be flipped → NO. Otherwise shift in the direction that keeps S ≤ K, then recompute S and R. If still invalid → NO.

Step E: Construct P1, N1, P2, N2.  
  From final a,b:  
    if a ≥ 0 then P1 = a, N1 = 0 else P1 = 0, N1 = –a  
    if b ≥ 0 then P2 = b, N2 = 0 else P2 = 0, N2 = –b  
  Now S = |a|+|b|, R = K – S is even. Use R/2 cancelling +x1 and R/2 cancelling –x1 jumps:  
    P1 += R/2, N1 += R/2.  
  Total jumps = (|a|+|b|) + 2·(R/2) = K, and net displacement remains P.

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;

// Extended Euclidean: returns g=gcd(a,b), and finds x,y so that a*x + b*y = g.
int64 ext_gcd(int64 a, int64 b, int64 &x, int64 &y) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    int64 x1, y1;
    int64 g = ext_gcd(b, a % b, x1, y1);
    // b*x1 + (a%b)*y1 = g
    // (a%b) = a - (a/b)*b
    x = y1;
    y = x1 - (a / b) * y1;
    return g;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int64 x1, x2, P, K;
    cin >> x1 >> x2 >> P >> K;

    // Step A: find one solution a0,b0 to a0*x1 + b0*x2 = P
    int64 a0, b0;
    int64 g = ext_gcd(x1, x2, a0, b0);
    if (P % g != 0) {
        cout << "NO\n";
        return 0;
    }
    // Scale up the particular solution
    a0 *= P / g;
    b0 *= P / g;

    // dx, dy are the shifts when parameter t increases by 1
    int64 dx = x2 / g;
    int64 dy = x1 / g;

    // Helper: current cost S = |a|+|b|
    auto S = [&](int64 a, int64 b){
        return llabs(a) + llabs(b);
    };

    // Step C: hill-descent in t to minimize S
    int64 a = a0, b = b0;
    while (true) {
        // try t+1
        int64 au = a + dx, bu = b - dy;
        if (S(au, bu) < S(a, b)) {
            a = au; b = bu;
        } else break;
    }
    while (true) {
        // try t-1
        int64 ad = a - dx, bd = b + dy;
        if (S(ad, bd) < S(a, b)) {
            a = ad; b = bd;
        } else break;
    }

    int64 sumAB = S(a, b);
    if (sumAB > K) {
        cout << "NO\n";
        return 0;
    }
    int64 rem = K - sumAB;  // leftover jumps

    // Step D: fix parity if needed
    if (rem % 2 != 0) {
        // can only flip parity if dx+dy is odd
        if ((dx + dy) % 2 == 0) {
            cout << "NO\n";
            return 0;
        }
        // shift t by +1 or -1, choosing valid one
        int64 a_up = a + dx, b_up = b - dy;
        if (S(a_up, b_up) <= K) {
            a = a_up; b = b_up;
        } else {
            a -= dx; b += dy;
        }
        sumAB = S(a, b);
        rem = K - sumAB;
        if (rem < 0 || rem % 2 != 0) {
            cout << "NO\n";
            return 0;
        }
    }

    // Step E: build P1,N1,P2,N2
    int64 P1, N1, P2, N2;
    if (a >= 0) { P1 = a; N1 = 0; }
    else        { P1 = 0; N1 = -a; }
    if (b >= 0) { P2 = b; N2 = 0; }
    else        { P2 = 0; N2 = -b; }

    // distribute rem/2 cancelling +x1 and -x1 jumps
    P1 += rem/2;
    N1 += rem/2;

    cout << "YES\n";
    cout << P1 << " " << N1 << " " << P2 << " " << N2 << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys
sys.setrecursionlimit(10**7)

def extended_gcd(a: int, b: int):
    # returns (g, x, y) with a*x + b*y = g = gcd(a,b)
    if b == 0:
        return (a, 1, 0)
    g, x1, y1 = extended_gcd(b, a % b)
    # b*x1 + (a%b)*y1 = g
    # a%b = a - (a//b)*b
    x = y1
    y = x1 - (a // b) * y1
    return (g, x, y)

def main():
    x1, x2, P, K = map(int, sys.stdin.readline().split())
    # Step A: solve a*x1 + b*x2 = P
    g, a0, b0 = extended_gcd(x1, x2)
    if P % g != 0:
        print("NO")
        return
    # scale particular solution
    a0 *= P // g
    b0 *= P // g

    dx = x2 // g  # a shift per +1 in parameter t
    dy = x1 // g

    # cost S = |a| + |b|
    def cost(a, b):
        return abs(a) + abs(b)

    # Step C: hill-descent to minimize cost
    a, b = a0, b0
    while True:
        au, bu = a + dx, b - dy
        if cost(au, bu) < cost(a, b):
            a, b = au, bu
        else:
            break
    while True:
        ad, bd = a - dx, b + dy
        if cost(ad, bd) < cost(a, b):
            a, b = ad, bd
        else:
            break

    S0 = cost(a, b)
    if S0 > K:
        print("NO")
        return
    rem = K - S0

    # Step D: fix parity if rem is odd
    if rem & 1:
        # can only flip if dx+dy odd
        if ((dx + dy) & 1) == 0:
            print("NO")
            return
        # shift in whichever direction remains feasible
        au, bu = a + dx, b - dy
        if cost(au, bu) <= K:
            a, b = au, bu
        else:
            a, b = a - dx, b + dy
        S0 = cost(a, b)
        rem = K - S0
        if rem < 0 or (rem & 1):
            print("NO")
            return

    # Step E: build non-negative jump counts
    if a >= 0:
        P1, N1 = a, 0
    else:
        P1, N1 = 0, -a
    if b >= 0:
        P2, N2 = b, 0
    else:
        P2, N2 = 0, -b

    # use rem//2 cancelling +x1 and -x1 jumps
    P1 += rem // 2
    N1 += rem // 2

    print("YES")
    print(P1, N1, P2, N2)

if __name__ == "__main__":
    main()
```