<|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
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int64_t x1v, x2v, p, k;

void read() { cin >> x1v >> x2v >> p >> k; }

int64_t extended_gcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }

    int64_t x1, y1;
    int64_t g = extended_gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return g;
}

void solve() {
    // We need P1*x1 - N1*x1 + P2*x2 - N2*x2 = P with P1+N1+P2+N2 = K and all
    // four counts non-negative. Let a = P1 - N1 and b = P2 - N2; then
    // a*x1 + b*x2 = P, which is solvable via the extended Euclid only when
    // gcd(x1,x2) divides P. Among all solutions (shifting a by x2/g and b by
    // x1/g) we slide to the one minimising |a| + |b|, the smallest number of
    // jumps the net displacement forces.
    //
    // The leftover jumps last = K - (|a| + |b|) must be spent in
    // cancelling pairs (one positive plus one negative of the same length),
    // so last has to be non-negative and even. If it is odd we try shifting
    // by one (a +/- x2/g, b -/+ x1/g): this changes the parity of |a| + |b|
    // only when x1/g + x2/g is odd, otherwise no solution exists. Finally we
    // split each net count into positive and negative jumps and add half of
    // the leftover to a matching positive/negative pair.

    int64_t p1, p2;
    int64_t g = extended_gcd(x1v, x2v, p1, p2);

    if(p % g != 0) {
        cout << "NO\n";
        return;
    }

    int64_t dx = x2v / g;
    int64_t dy = x1v / g;
    p1 *= p / g;
    p2 *= p / g;

    while(llabs(p1 + dx) + llabs(p2 - dy) < llabs(p1) + llabs(p2)) {
        p1 += dx;
        p2 -= dy;
    }

    while(llabs(p1 - dx) + llabs(p2 + dy) < llabs(p1) + llabs(p2)) {
        p1 -= dx;
        p2 += dy;
    }

    if(llabs(p1) + llabs(p2) > k) {
        cout << "NO\n";
        return;
    }

    int64_t n1 = 0, n2 = 0;
    int64_t last = k - llabs(p1) - llabs(p2);
    if(last % 2 == 0) {
        if(p1 < 0) {
            n1 = -p1;
            p1 = 0;
        }

        if(p2 < 0) {
            n2 = -p2;
            p2 = 0;
        }

        p1 += last / 2;
        n1 += last / 2;
    } else {
        if((dx + dy) % 2 == 0) {
            cout << "NO\n";
            return;
        }

        if(llabs(p1 + dx) + llabs(p2 - dy) < llabs(p1 - dx) + llabs(p2 + dy)) {
            p1 += dx;
            p2 -= dy;
        } else {
            p1 -= dx;
            p2 += dy;
        }

        if(llabs(p1) + llabs(p2) > k) {
            cout << "NO\n";
            return;
        }

        last = k - llabs(p1) - llabs(p2);
        if(p1 < 0) {
            n1 = -p1;
            p1 = 0;
        }

        if(p2 < 0) {
            n2 = -p2;
            p2 = 0;
        }

        p1 += last / 2;
        n1 += last / 2;
    }

    cout << "YES\n";
    cout << p1 << ' ' << n1 << ' ' << p2 << ' ' << n2 << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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()
```
