#P2520. Vector Sum Construction

    ID: 15790 Type: Default 1000ms 256MiB

Vector Sum Construction

Vector Sum Construction

You are given two integers a and b that define a base vector (a, b). From this base vector you can generate eight distinct vectors by independently flipping the sign of each coordinate and swapping the coordinates. In other words, you are allowed to use any of the following vectors:

[ { (a,b),\ (a,-b),\ (-a,b),\ (-a,-b),\ (b,a),\ (b,-a),\ (-b,a),\ (-b,-a) } ]

You can use these vectors arbitrarily many times. The problem is to decide whether it is possible to choose some (possibly repeated) vectors from this set so that their sum is exactly equal to a target vector (x, y).

Mathematical Explanation

You are allowed to form the sum

[ \sum_{i=1}^{n} v_i = (x,y), \quad v_i \in { (\pm a,\pm b),\ (\pm b,\pm a) } ]

Because the set of available vectors is symmetric under sign changes, this problem reduces to checking if (x,y) belongs to the lattice (i.e. the subgroup) generated by these eight vectors. It turns out that this lattice depends on the parity of a and b:

  • Case 1: If a and b have different parity (i.e. one is even and the other is odd), then the eight vectors span \(\mathbb{Z}^2\). In this case, any target (x,y) can be obtained.
  • Case 2: If a and b are both odd, then every allowed move has both coordinates odd. Consequently, the sum of an odd number of moves gives a vector with both coordinates odd, and the sum of an even number of moves gives a vector with both coordinates even. Thus, in this case, a necessary and sufficient condition is that x and y have the same parity.
  • Case 3: If a and b are both even, then every allowed vector has even coordinates. Hence, a target vector (x,y) can be obtained only if both x and y are even.

Your task is to print YES if it is possible to form (x,y) using these moves, and NO otherwise.

inputFormat

The input consists of a single line with four integers a, b, x, and y separated by spaces.

Constraints:

  • -109 ≤ a, b, x, y ≤ 109
  • (a, b) is not the zero vector.

outputFormat

Print a single line containing YES if it is possible to form the vector (x, y) using the moves described, or NO otherwise.

sample

1 2 1 0
YES