p141.ans1
======================
YES
1 0 5 6

=================
p141.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;
}

=================
p141.in1
======================
2 3 -1 12

=================
p141.py
======================
def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
    if b == 0:
        return a, 1, 0
    gcd, x1, y1 = extended_gcd(b, a % b)
    x = y1
    y = x1 - (a // b) * y1
    return gcd, x, y


def solve_problem(x1: int, x2: int, p: int, k: int) -> None:
    g, p1, p2 = extended_gcd(x1, x2)

    if p % g != 0:
        print("NO")
        return

    dx = x2 // g
    dy = x1 // g
    p1 *= p // g
    p2 *= p // g

    while abs(p1 + dx) + abs(p2 - dy) < abs(p1) + abs(p2):
        p1 += dx
        p2 -= dy
    while abs(p1 - dx) + abs(p2 + dy) < abs(p1) + abs(p2):
        p1 -= dx
        p2 += dy

    if abs(p1) + abs(p2) > k:
        print("NO")
        return

    n1, n2 = 0, 0
    last = k - abs(p1) - abs(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:
            print("NO")
            return
        if abs(p1 + dx) + abs(p2 - dy) < abs(p1 - dx) + abs(p2 + dy):
            p1 += dx
            p2 -= dy
        else:
            p1 -= dx
            p2 += dy
        if abs(p1) + abs(p2) > k:
            print("NO")
            return
        last = k - abs(p1) - abs(p2)
        if p1 < 0:
            n1 = -p1
            p1 = 0
        if p2 < 0:
            n2 = -p2
            p2 = 0
        p1 += last // 2
        n1 += last // 2

    print("YES")
    print(p1, n1, p2, n2)


def main():
    x1, x2, p, k = map(int, input().split())
    solve_problem(x1, x2, p, k)


if __name__ == "__main__":
    main()

=================
statement.txt
======================
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


=================
