At a geometry lesson Gerald was given a task: to get vector B out of vector A. Besides, the teacher permitted him to perform the following operations with vector А:
Turn the vector by 90 degrees clockwise.
Add to the vector a certain vector C.
Operations could be performed in any order any number of times.
Can Gerald cope with the task?
Input
The first line contains integers x1 и y1 — the coordinates of the vector A ( - 108 ≤ x1, y1 ≤ 108). The second and the third line contain in the similar manner vectors B and C (their coordinates are integers; their absolute value does not exceed 108).
Output
Print "YES" (without the quotes) if it is possible to get vector B using the given operations. Otherwise print "NO" (without the quotes).
Examples
Input
0 0
1 1
0 1
Output
YES
Input
0 0
1 1
1 1
Output
YES
Input
0 0
1 1
2 2
Output
NO
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given vectors A, B, C. All are integer vectors in 2D, with coordinates up to 1e8 in absolute value. We can perform two operations any number of times in any order:
Rotate vector A by 90 degrees clockwise. This operation can be applied to the current vector (the vector we have after previous operations). Actually the problem says: "with vector A: turn the vector by 90 degrees clockwise. Add to the vector a certain vector C." So presumably we start with vector A, and we can repeatedly apply these operations. So after each operation, we have a current vector (call it v). We can rotate v by 90° clockwise (so v -> R(v) where R = (x,y) -> (y, -x) if clockwise rotation? Let's check: Rotating (x,y) by 90° clockwise gives (y, -x). For 90° counterclockwise it would be (-y, x). So we'll use clockwise: (x, y) -> (y, -x). We can also add vector C to v: v -> v + C.
We can apply operations any number of times, any order. We need to determine if we can reach exactly B starting from A.
We need to analyze the set of reachable vectors.
Let v0 = A. Operations: v -> R(v) and v -> v + C.
This is like we have an affine linear transformation system: we can apply rotation any number of times, and addition of C any number of times. Since rotation is invertible (R^4 = identity), the operations generate a group/semigroup. Since we can also add C, the set of reachable vectors from A is A + M, where M is the set of all vectors that can be generated by starting from 0 and applying sequences of operations? But careful: Starting from A, we can apply a sequence of operations. Represent the effect as a combination of rotations and additions. Since rotations are linear, we can think of the process as: v = R^k (A) + sum_{i} R^{e_i} C? Actually when we add C, we add it to the current vector. If later we rotate, the addition is rotated as well. For example: start with A, add C => A+C. Then rotate => R(A+C) = R(A) + R(C). Alternatively, rotate then add: R(A) then add C gives R(A)+C. So the order matters.
General form: We can apply a sequence of operations. Each operation is either multiply by R on the left (since rotation applies to current vector) or add C (which does not get multiplied by previous rotations but subsequent rotations will affect it). So we can think of the process as building an expression:
Let operations be o1, o2, ..., on. Starting v0 = A. For each operation: if it's rotate, v = R(v); if it's add, v = v + C.
We can represent final v as:
v = R^{k_n} ( A + sum_{j=1}^{m} R^{- ? Actually careful: We can push rotations to the front? There is known technique: Represent the sequence as: v = R^{k} A + sum_{i} R^{k_i} C, where the exponents depend on when additions are made relative to subsequent rotations.
We can think backwards: Starting from B, we can consider inverse operations? Since operations are reversible? Rotate is reversible (rotate 90° counterclockwise). Adding C is not directly reversible unless we can subtract C, but we only are allowed to add, not subtract. However, we can simulate subtraction by using rotations? No, addition of C is the only addition; we cannot subtract unless C is zero vector? But we can add C multiple times, so effectively we can add any integer multiple (non-negative?) of C? But wait, we can add C any number of times, but we cannot subtract C unless we can use rotations to create a negative multiple? Actually adding C is only positive direction; we cannot directly subtract. However, since we can rotate, maybe we can produce negative multiples by combining rotations? For example, if we add C, then rotate, then add something else? But subtraction is not directly available. However, note that we can add C any number of times, so we can add n*C for any non-negative integer n. But can we achieve negative multiples? Possibly if we can use rotation to change the direction and then add C to cancel? That might allow effectively any integer combination if we have enough freedom, but we need to see.
But wait, the operations are: Turn by 90° clockwise, and Add vector C. There is no restriction that we can only add C; we can add it multiple times. So the set of reachable vectors is: All vectors of the form:
v = R^{i0} A + (R^{i1} C + R^{i2} C + ... + R^{ik} C) for some non-negative integer k, and where the i's are exponents modulo 4, and the sequence of i's depends on when rotations occurred. More systematically, we can think of the process as a finite state machine: The rotation state changes as we rotate. At any point, we can add C, but the added C is affected by subsequent rotations. So if we consider that the current orientation (the number of rotations applied so far, modulo 4) defines how further rotations will act. If we add C when the current rotation count is r, then that C is effectively added as is at that moment. Later, if we rotate further, that added C will also be rotated. So the contribution of that addition after final rotations is R^{s} C, where s is the number of rotations applied after that addition.
Thus, if the final rotation count (total rotations applied) is t (mod 4), and we add C at a time when the number of rotations already applied is r (so before some later rotations), then after all operations, that C will be rotated by (t - r) more times: i.e., contribution = R^{t - r} C. So overall, final vector:
v = R^{t} A + sum_{j} R^{t - r_j} C, where each r_j is the rotation count at the time of addition (r_j ∈ {0,1,2,3} mod 4, and t is final total rotation count (mod 4). Rearranging: v = R^{t} A + sum_{d=0}^{3} (number of additions performed when rotation state = d) * R^{t-d} C.
Let n_d be the number of times we add C when the rotation state is d (d ∈ {0,1,2,3}). Then final v = R^{t} A + ∑_{d=0}^{3} n_d R^{t-d} C.
Since t and n_d are non-negative integers (n_d ≥ 0). Also t modulo 4 matters; t can be any integer (since we can rotate any number of times, but only mod 4 matters due to periodicity: R^4 = identity). So effectively t ∈ {0,1,2,3} (since rotating 4 times is identity, we can always reduce t mod 4, though we might need to consider large number of rotations? But if we rotate extra times, we just add extra multiples of 4 to t, but that's equivalent mod 4, and n_d counts can be arbitrarily large; rotating extra times without adding anything doesn't change vector because R^4 = identity and A is rotated accordingly? Actually if we rotate 4 times, A returns to original, but we also might have added C's that are rotated accordingly. However, if we rotate extra times with no additions, we just increase t mod 4 but also affect contributions? Let's see: Suppose we have a sequence that ends with total rotations t. If we add 4 extra rotations at the end, final vector becomes R^{t+4} A + sum_{j} R^{t+4 - r_j} C = R^{t} A + sum_{j} R^{t - r_j} C (since R^4 = identity). So it's the same as if we considered t mod 4. So we can restrict t ∈ {0,1,2,3}. However, note that we could also rotate extra times in the middle, changing r_j distribution? But we can always simulate any distribution with some t? Actually we can choose to rotate at any time, so we can produce any combination of n_d and t. Since we can insert rotations arbitrarily, we can achieve any vector of the form:
v = R^t A + (a0 * C + a1 * R(C) + a2 * R^2(C) + a3 * R^3(C)) (Equation 1)
where a0, a1, a2, a3 are non-negative integers, but with a subtlety: The coefficients in the sum correspond to contributions: from n_d, we have contributions: for each d, n_d * R^{t-d} C. Let’s define k_j = number of additions whose contribution is R^j C in the final expression. Since R^{t-d} C = R^{(t-d) mod 4} C, we can let j = (t-d) mod 4. Then d = (t - j) mod 4. So n_d = m_j where m_j = n_{(t - j) mod 4}. Then final v = R^t A + ∑{j=0}^{3} m_j R^j C, where m_j = n{(t - j) mod 4} are also non-negative integers. So indeed, for any t ∈ {0,1,2,3}, and any non-negative integers m0, m1, m2, m3, we can reach v = R^t A + m0C + m1R(C) + m2R^2(C) + m3R^3(C). The mapping between (n_d) and (m_j) is a bijection given t, so any non-negative integer m_j are achievable by appropriate n_d (since we can always set n_d = m_{(t-d) mod 4}). So yes, we can achieve any combination of non-negative integer coefficients for the four rotated versions of C, plus a rotated A.
But is there any constraint linking m_j? For instance, if we want a certain combination, we need to be able to choose n_d accordingly. Since n_d must be non-negative integers, and m_j = n_{(t-j) mod 4}, we can set n_d to any non-negative integer, so m_j can be any non-negative integers. So reachable set = { R^t A + M : t∈{0,1,2,3}, M ∈ (non-negative integer linear combinations of C, R(C), R^2(C), R^3(C) ) }.
But careful: Are we allowed to add C zero times? Yes. So any t and M = 0 is reachable (just rotate t times, no adds). So set is: ∪_{t=0..3} ( R^t A + S ) where S is the semigroup generated by C, R(C), R^2(C), R^3(C) under addition (i.e., all vectors that are linear combinations with non-negative integer coefficients). Since these generators are vectors in ℤ^2.
We need to check if B belongs to this set.
But is the semigroup indeed all vectors that can be expressed as non-negative integer combinations of these four vectors? In ℤ^2, the set of all integer combinations (with non-negative coefficients) of a set of vectors is a semigroup; it might not cover all integer lattice points, but we need to check membership.
But also note: The operations allow us to rotate and add C many times; but is there any restriction that the number of rotations t must be exactly the total number of rotate operations? But t can be any non-negative integer mod 4; we can have t up to arbitrarily large, but only mod 4 matters. So indeed we only care t mod 4.
Thus problem reduces to: For some t ∈ {0,1,2,3}, does there exist non-negative integers p,q,r,s such that:
B - R^t A = pC + qR(C) + rR^2(C) + sR^3(C) ?
Let’s denote D_t = B - R^t A. We need to check if D_t belongs to the semigroup S = ⟨C, R(C), R^2(C), R^3(C)⟩_{ℕ} (non-negative integer combinations). Since C is given, compute its rotated versions.
But is there any nuance? Could we also use rotations after all additions? That's already accounted for t.
Another nuance: The order of operations might allow also to effectively "subtract" C? For example, suppose we want to achieve a negative coefficient for C. Could we do something like: rotate, add, rotate, add, ... to cancel? Since we can only add, not subtract, we cannot directly subtract. However, maybe we can achieve negative multiples if C is not zero and we can use rotations to offset? For instance, we might add C, then later add R(C) etc., but still all contributions are added, not subtracted. So indeed coefficients must be non-negative. However, note that we could also rotate A multiple times, which could produce R^t A that may have negative components relative to A, but that's fine.
But what about the possibility of using rotations to produce a vector that is exactly B with some combination that includes negative coefficients? For example, if we can achieve B = R^t A + combination where some coefficients are negative, is that possible? Since we can only add C, we cannot undo additions. So unless C is zero vector (where adding zero doesn't change), we cannot subtract. Could we simulate subtraction by adding something else and then rotating to cancel? Suppose we add C, then later add R^2(C) maybe results in cancellation? But they are vectors; addition is commutative; if we add both, we get C+R^2(C). To achieve net negative coefficient for C, we would need to add some combination that sums to a vector that equals negative of some multiple of C. But all added vectors are in the cone spanned by the four rotated Cs with non-negative coefficients. So the sum is always in that cone. So negative coefficients not possible. However, could we effectively subtract by rotating A to negative direction? A is fixed; R^t A can be any of its four rotations. So if we need a negative coefficient for a generator, we might try to adjust t to change D_t. But D_t may have negative coordinates relative to generators.
So the condition is D_t ∈ S, where S is set of all non-negative integer combinations of the four rotated Cs.
But wait: Are we allowed to add C arbitrarily many times? Yes, any number, including zero. So S is the integer lattice points in the cone generated by the four vectors, intersected with the integer lattice? However, since we require integer coefficients, S is the semigroup of integer vectors in the cone.
But is it possible that S equals the whole ℤ^2? That would require that C and its rotations generate the lattice, e.g., if C = (1,0) then R(C)=(0,-1), R^2(C)=(-1,0), R^3(C)=(0,1). Their non-negative combinations produce only vectors where both coordinates are ≤ 0? Actually (1,0) and (0,1) are not both available with non-negative coefficients: we have (0,1) from R^3(C) = (0,1) if C=(1,0) then R^3(C)= (0,1)? Let's compute: C=(x,y). R(C) = (y, -x). R^2(C) = (-x, -y). R^3(C) = (-y, x). So if C = (1,0), then: C=(1,0); R(C)=(0,-1); R^2(C)=(-1,0); R^3(C)=(0,1). So the four vectors are (1,0), (0,-1), (-1,0), (0,1). Non-negative combinations of these can produce any integer vector? For example, to get (1,1): (1,0)+(0,1) = (1,1) requires coefficients for (0,1) = use R^3(C) coefficient 1, and C coefficient 1. That's allowed, both non-negative. To get (-1,-1): use R^2(C) + R(C) = (-1,0)+(0,-1) = (-1,-1). To get (2,-3): 2*(1,0)+3*(0,-1)= (2,-3). So yes, any integer vector can be expressed as non-negative combination of these four? Actually can we get any integer vector? Let's consider (1, -2): need (1,0) + 2*(0,-1) = (1,-2) works. ( -1,2): ( -1,0) + 2*(0,1) works. (0,0): all zero. For any (x,y) ∈ ℤ^2, we can represent as: if x ≥0, use x copies of (1,0); if x<0, use -x copies of (-1,0). Similarly for y: if y ≥0, use y copies of (0,1); if y<0, use -y copies of (0,-1). But careful: (0,1) is R^3(C), (0,-1) is R(C). So yes, we can decompose any integer vector into these basis vectors with non-negative coefficients because we have both positive and negative unit vectors in each coordinate. So S = ℤ^2. In that case, for any D_t, it is reachable. But that's only if the set {C, R(C), R^2(C), R^3(C)} spans ℤ^2 in the sense that every integer vector can be expressed as non-negative combination. Is that always true? Not necessarily. For example, if C = (2,0), then R(C) = (0,-2), R^2(C) = (-2,0), R^3(C) = (0,2). The generated semigroup consists of vectors where both coordinates are even. So only even integer vectors. Also vectors like (1,0) cannot be expressed with non-negative coefficients because the smallest non-zero x-coordinate achievable is ±2. So S is a sublattice of ℤ^2: all vectors where coordinates are multiples of gcd of coordinates? More precisely, the set of integer linear combinations (with integer coefficients, not necessarily non-negative) of these four vectors is the lattice generated by C and its rotations. But with non-negative coefficients, we only get the points in the intersection of that lattice with the cone (or maybe all points in the lattice that can be expressed with non-negative coefficients). However, due to having both positive and negative directions, the non-negative integer cone of the four vectors might actually equal the whole lattice if the vectors span the lattice and there is a way to represent any lattice point with non-negative coefficients. But is it always that if we have a generating set that includes both a vector and its negative, then the semigroup (non-negative combos) equals the whole lattice? Not exactly: If we have v and -v in the generating set, then any integer multiple of v can be expressed: for positive k, use k(-v). However, we must be careful: For a given lattice point w, we might need to combine multiple vectors. But if the generating set includes a basis of the lattice and also the negatives of those basis vectors, then any integer point can be expressed as non-negative combination? Let's think.
Suppose we have a set G of vectors in ℤ^n such that the integer span of G is the whole ℤ^n, and for each direction there exist both positive and negative vectors. However, non-negative integer combinations of G may not cover all of ℤ^n if there is some constraint like parity or something. But if G contains a basis of ℤ^n and also the negatives of those basis vectors, then any vector w can be expressed as w = ∑ a_i e_i with a_i ∈ ℤ. If we have e_i and -e_i in G, we can write w as sum of non-negative multiples: for each basis vector e_i, if coefficient a_i ≥0, use a_i copies of e_i; if a_i <0, use -a_i copies of -e_i. So indeed we can represent w as non-negative combination of the set {e_i, -e_i}. So if G contains a basis and the negatives of those basis vectors, then S = ℤ^n. But does our set {C, R(C), R^2(C), R^3(C)} contain such a basis? Not necessarily. For C = (2,0), the set contains (2,0), (0,-2), (-2,0), (0,2). This is essentially ±2 times the standard basis. But does it contain a basis of ℤ^2? The integer span of these vectors is all vectors with even coordinates, which is index 2 in ℤ^2. So it does not span ℤ^2. So the lattice generated is a sublattice. The semigroup will be contained in that sublattice. So B - R^t A must lie in that sublattice.
In general, the set of integer linear combinations (with integer coefficients, positive or negative) of C, R(C), R^2(C), R^3(C) is the lattice L = ⟨C, R(C)⟩_ℤ (since rotations are linear combinations). Actually R^2(C) = -C, R^3(C) = -R(C). So the set {C, R(C), -C, -R(C)}. So the integer lattice generated by C and R(C) is the same as the integer span of C and R(C). So L = { u * C + v * R(C) | u,v ∈ ℤ }. Since C and R(C) are perpendicular? Not necessarily; R(C) is orthogonal to C (since dot product (x,y)·(y,-x)= xy - xy =0). So they are orthogonal but not necessarily of equal length: |C| = sqrt(x^2+y^2) = |R(C)|. So they are orthogonal vectors of equal magnitude. So they form an orthogonal basis, but not necessarily of ℤ^2; they may not be primitive lattice vectors. The lattice they generate is a sublattice of ℤ^2. In fact, any integer vector can be expressed as integer combination of C and R(C) if and only if the determinant of the matrix formed by C and R(C) is ±1 times the gcd? The area of the fundamental parallelogram is |det(C, R(C))| = |x * (-x) - y * y? Actually det( (x,y), (y, -x) ) = x*(-x) - yy = -(x^2+y^2). So absolute determinant = x^2+y^2. So the index of the lattice L in ℤ^2 is |det| = x^2+y^2? Wait, the index of a sublattice generated by two integer vectors in ℤ^2 is |det| divided by the gcd of something? Actually if we have two linearly independent integer vectors v,w, the lattice ℤv+ℤw is a sublattice of ℤ^2. The index (the size of the quotient) is equal to the absolute value of the determinant of the matrix formed by v,w (since ℤ^2 has determinant 1). Provided that the lattice is full-rank. Indeed, the index = |det(v,w)|. So here det(C, R(C)) = -(x^2+y^2). So the index = x^2+y^2. That seems huge, e.g., C=(1,0) gives index =1, which is correct because C and R(C) generate ℤ^2? For (1,0) and (0,-1), det = -1, index=1. For (2,0) and (0,-2), det = -4, index=4. Actually (2,0) and (0,-2) generate all even coordinates, which index is 2? Wait, ℤ^2 modulo the sublattice of even coordinates has size 2? Actually the sublattice of vectors with both coordinates even has index 4? Let's compute: ℤ^2 / (2ℤ × 2ℤ) has 4 elements: (0,0), (1,0), (0,1), (1,1). So index=4. So indeed det=4. For C=(2,0), x^2+y^2 =4, so index=4. So that matches. So L is the lattice of all integer combinations of C and R(C). The semigroup S = non-negative combinations of C, R(C), -C, -R(C). Since we have both signs, S is actually the whole lattice L? Because any vector in L can be written as integer combination uC + vR(C). If u≥0, we use u copies of C; if u<0, we use -u copies of -C. Similarly for v. So S = L. So S is exactly the integer lattice generated by C and R(C). That is, S = { uC + vR(C) : u,v ∈ ℤ }? But wait, is it true that any integer combination can be represented as non-negative combination of C, R(C), -C, -R(C)? For given u,v ∈ ℤ, we can write: uC + vR(C) = (if u≥0 then uC else (-u)(-C)) + (if v≥0 then vR(C) else (-v)(-R(C))). Since -C = R^2(C) and -R(C) = R^3(C) are in the generating set, yes. So S = L. So S is exactly the lattice generated by C and R(C), i.e., all integer combinations of C and R(C). So the condition reduces to: For some t ∈ {0,1,2,3}, D_t = B - R^t A must belong to L, the lattice spanned by C and R(C). But is there any further constraint? Since S = L, any lattice point is reachable because we can pick appropriate non-negative coefficients to represent it. However, we need to ensure that the coefficients are non-negative. The representation using signs may require negative coefficients for some generators, but we have both signs available as separate generators, so we can always use the positive version of the opposite direction. As argued, if we have both a vector and its negative in the generating set, we can represent any integer combination with non-negative coefficients. For example, take u = 3, v = -2: then we can use 3C + 2*(-R(C)). Since -R(C) is in the set (as R^3(C) if C rotated thrice gives -R(C)? Actually -R(C) = R^3(C) because R^3(C) = R^{-1}(C) = (-y, x) and R(C) = (y, -x), so indeed -R(C) = (-y, x) = R^3(C). So yes. So any integer combination (u,v) yields a non-negative combination by using appropriate signs.
But careful: The representation uses two different sets: we have four generators: G0 = C, G1 = R(C), G2 = R^2(C) = -C, G3 = R^3(C) = -R(C). So any vector in L can be expressed as a0C + a1R(C) + a2*(-C) + a3*(-R(C)) with non-negative a_i. For a given u,v ∈ ℤ, we can set a0 = max(u,0), a2 = max(-u,0), a1 = max(v,0), a3 = max(-v,0). So indeed it's always possible. So S = L.
Thus the condition is simply: ∃ t ∈ {0,1,2,3} such that B - R^t A is in the lattice L = span_ℤ(C, R(C)). In other words, B - R^t A must be an integer linear combination of C and R(C). Equivalently, the vector D_t must be orthogonal to something? Since L is a sublattice of ℤ^2 of full rank (if C is not zero). If C = 0, then L = {0}. So we need to handle that case.
But wait, is it always sufficient that D_t ∈ L? We also need to ensure that the representation uses non-negative coefficients? But as argued, if D_t ∈ L, we can always write it as non-negative combination because we have both signs. However, is there any scenario where non-negative representation is impossible? For example, suppose C = (1,2). Then R(C) = (2,-1). L is the lattice spanned by (1,2) and (2,-1). What is the index? det = 1*(-1) - 22 = -1 -4 = -5, so index=5. So L is a sublattice of index 5. But does having both C and -C, R(C) and -R(C) guarantee that any vector in L can be expressed as a non-negative combination of these four? Let's test a vector that is in L but might require mixed signs? For example, take v = (0,5). Is (0,5) in L? Solve u(1,2)+v*(2,-1) = (0,5). Then u+2v =0, 2u - v =5. Solve: from first, u = -2v. Sub into second: 2(-2v) - v = -4v - v = -5v =5 => v = -1, then u =2. So (0,5) = 2C + (-1)R(C). Since we need a negative coefficient for R(C), we can replace (-1)R(C) with 1(-R(C)) which is R^3(C). So we have 2C + 1R^3(C). That's non-negative: a0=2, a3=1, others 0. So works.
What about v = (1,1)? Solve u+2v=1, 2u - v=1. From first, u=1-2v. Then second: 2(1-2v)-v =2-4v-v=2-5v=1 => 5v=1 => v=1/5 not integer. So (1,1) not in L. So no need.
So indeed, if D_t ∈ L, we can express it with non-negative coefficients because we can decompose the integer solution into positive and negative parts.
But wait, is there any case where the representation might require fractional coefficients? But we already have an integer solution (u,v) such that D_t = uC + vR(C). Then we can set a0 = max(u,0), a2 = max(-u,0), a1 = max(v,0), a3 = max(-v,0). So it's always non-negative. However, we need to verify that the expression using these a_i indeed equals uC+vR(C). Indeed, a0C + a2(-C) + a1R(C) + a3(-R(C)) = (a0 - a2)C + (a1 - a3)R(C) = uC + vR(C). So yes.
Thus condition reduces to D_t ∈ L.
But is there any constraint that the number of operations is finite? Since we can use arbitrarily many additions, we can achieve arbitrarily large coefficients.
Therefore, the problem is to check whether there exists t ∈ {0,1,2,3} such that B - R^t A is in the integer lattice generated by C and R(C). Equivalent to: B - R^t A = uC + vR(C) for some integers u,v.
But we should also consider special cases: If C = (0,0), then adding C does nothing. The only operations are rotations. So we can only get the four rotations of A. Then we need to check if B is one of those four. That is included in condition? For C=0, L = {0}, so D_t must be 0. That means B = R^t A. So yes.
If A and B are both zero? Then B = A, t=0 works.
If C is non-zero but A and B are arbitrary.
Thus we need to compute: For each t = 0,1,2,3, compute D = B - R^t A. Then check if D is in the lattice L = span(C, R(C)). Equivalent to: The determinant of (C, R(C)) as a matrix, and D must have integer coordinates relative to the basis? More concretely, we can solve the linear system:
We need integers u,v such that:
D_x = u * C_x + v * (R(C))_x = uC_x + vC_y (since R(C) = (C_y, -C_x))
D_y = u * C_y + v * (R(C))_y = uC_y + v(-C_x) = uC_y - vC_x.
So:
(1) D_x = u * C_x + v * C_y
(2) D_y = u * C_y - v * C_x
This is a linear system in u, v. We can solve for u, v as rational numbers. Since C may have components zero, need to handle degenerate cases where C and R(C) are linearly dependent? They are always linearly independent unless C is zero? Actually if C = (0,0) they are zero, degenerate. If C is non-zero, then C and R(C) are orthogonal and non-zero, so they are linearly independent. So there is a unique solution (u, v) over reals, given by:
Treat as matrix M = [C, R(C)] = [[C_x, C_y], [C_y, -C_x]]; its determinant = -(C_x^2 + C_y^2) = -|C|^2. So M is invertible if |C| != 0.
Inverse of M = (1/( -a^2 - b^2 )) * [ -a, -b; -b, a ]? Actually compute:
det = -a^2 - b^2 = -(a^2+b^2).
The adjugate: For matrix [[a, b], [b, -a]], the cofactor matrix: C11 = (-a), C12 = (-b), C21 = (-b), C22 = (a). But careful: The adjugate is transpose of cofactor matrix: So adj = [[-a, -b], [-b, a]]? Let's compute properly:
M = [a, b; c, d] with c = b, d = -a.
Cofactor matrix:
C11 = d = -a,
C12 = -c = -b,
C21 = -b = -b? Actually C21 = -b? Let's do systematically:
For a 2x2, cofactor matrix = [[d, -c], [-b, a]]. So here d = -a, c = b, b = b, a = a. So cofactor = [[-a, -b], [-b, a]]. Transpose is same (symmetric?) Actually transpose of that gives adjugate = same because it's symmetric? [[-a, -b], [-b, a]] is symmetric if we ignore sign? Yes it's symmetric. So adj(M) = [[-a, -b], [-b, a]].
Then M^{-1} = adj(M) / det = [[-a, -b], [-b, a]] / (-(a^2+b^2)) = [[a/(a^2+b^2), b/(a^2+b^2)]; [b/(a^2+b^2), -a/(a^2+b^2)]]? Let's compute: divide by negative: (-a)/det = (-a)/[-(a^2+b^2)] = a/(a^2+b^2). (-b)/det = b/(a^2+b^2). For the second row: (-b)/det = b/(a^2+b^2); (a)/det = a/ (-(a^2+b^2))? Wait careful: adj[2,1] = -b, so (-b)/det = (-b)/[-(a^2+b^2)] = b/(a^2+b^2). adj[2,2] = a, so a/det = a/ (-(a^2+b^2)) = -a/(a^2+b^2). So indeed M^{-1} = [[a/(a^2+b^2), b/(a^2+b^2)]; [b/(a^2+b^2), -a/(a^2+b^2)]].
Thus:
u = (a * D_x + b * D_y) / (a^2+b^2)
v = (b * D_x - a * D_y) / (a^2+b^2)
Now, we need u and v to be integers (since we need integer coefficients). That is the condition: (C_x D_x + C_y D_y) divisible by (C_x^2 + C_y^2), and (C_y D_x - C_x D_y) divisible by (C_x^2 + C_y^2). But also note that we only need existence of integers u,v satisfying the equations; they will be unique if C non-zero. So condition is: both these expressions are integers.
But also we need to check that the resulting u,v are such that we can represent with non-negative coefficients? As argued, we can always adjust signs by using -C or -R(C), so the signs of u and v don't matter; we can always use non-negative coefficients by splitting into positive and negative parts. So no extra condition.
Thus the algorithm:
Read A=(x1,y1), B=(x2,y2), C=(x3,y3).
For each t in 0..3:
compute R^t A: apply rotation t times clockwise.
Let D = (B.x - RA.x, B.y - RA.y).
If C == (0,0):
if D == (0,0): answer YES.
else:
Compute denom = C.x^2 + C.y^2.
Compute num1 = C.x * D.x + C.y * D.y.
Compute num2 = C.y * D.x - C.x * D.y.
Check if num1 % denom == 0 and num2 % denom == 0 (in integers, careful with negative modulo; better to check divisibility: num1 divisible by denom, i.e., num1 mod denom == 0, similarly num2). Since denom >0, we can just check remainder == 0.
If any t passes, print YES, else NO.
But is that sufficient? Let's test with examples.
Example 1:
A=(0,0), B=(1,1), C=(0,1). C=(0,1). denom = 0^2+1^2=1.
For t=0: D=(1,1). num1 = 01+11=1 divisible by 1. num2 = 11 - 01 =1 divisible by 1. So YES. Output YES.
Example 2:
A=(0,0), B=(1,1), C=(1,1). denom=1^2+1^2=2.
t=0: D=(1,1). num1=11+11=2 divisible by 2? 2%2=0 yes. num2=11 -11=0 divisible by 2 yes. So YES.
Example 3:
A=(0,0), B=(1,1), C=(2,2). denom=4+4=8.
t=0: D=(1,1). num1=21+21=4, 4%8 !=0. So no. t=1: R(A)= (0,0) rotated? Rotating (0,0) still (0,0). So all D same. So no. Output NO.
Matches.
But let's consider other scenarios. Suppose A=(1,0), B=(0,1), C=(1,0). Can we get? Operations: start (1,0). Rotate 90° clockwise -> (0,-1). Then add C=(1,0) -> (1,-1). Rotate -> (-1,-1)? Not sure. But we want (0,1). Let's test condition: C=(1,0) => denom=1. t=0: D = (0-1, 1-0)=(-1,1). num1 = 1*(-1)+01 = -1 divisible by 1 yes. num2 = 0(-1)-11 = -1 divisible by 1 yes. So condition says YES. Is it possible? Let's try to construct: We need to get (0,1) from (1,0) using rotations and additions of (1,0). Since denom=1, the lattice is whole ℤ^2. So reachable. Try sequence: start (1,0). Rotate clockwise: (0,-1). Add C: (1,-1). Rotate: (-1,-1). Add C: (0,-1). Rotate: (1,0). That cycles? Maybe we need different sequence. Let's attempt to solve equation: B - R^t A = uC + vR(C). For t=0, D = (-1,1). Solve uC+vR(C) = u(1,0)+v*(0,-1) = (u, -v). Set = (-1,1) => u=-1, v=-1. So we need u=-1, v=-1. This means we need -1C and -1R(C). But we can use negative multiples by using opposite vectors: -C = R^2(C) = (-1,0) and -R(C)=R^3(C) = (0,1). So we need one addition of (-1,0) and one addition of (0,1) with non-negative coefficients? Actually we can represent as: D = 1*(-C) + 1*(-R(C)). So we need to add -C once and -R(C) once. But can we achieve adding -C? Since we can only add C directly. But we can simulate adding -C by adding R^2(C) if we have rotations? Wait, the generators we have are C, R(C), R^2(C), R^3(C). So we can add R^2(C) which is -C. So indeed we can add -C as an operation, but the operation "Add to the vector a certain vector C" means we can only add the given vector C, not its rotations. However, the statement: "Add to the vector a certain vector C." That is ambiguous: Does it mean we can add the vector C exactly as given, or we can add a certain vector C (which is fixed) to the current vector? It says: "Add to the vector a certain vector C." It seems like we are allowed to add exactly the vector C (the given one) to the current vector. Not its rotations. But in our earlier analysis, we assumed we can add C at any time, but after rotations, the effect of adding C later is rotated if we subsequently rotate. But we cannot directly add R(C) unless we first rotate the whole vector, then add C, then maybe rotate back. That yields net addition of rotated C? Let's re-express: Operation 2: "Add to the vector a certain vector C." Means we take the current vector v and add the fixed vector C, i.e., v := v + C. So we cannot directly add R(C). However, by combining with rotations before and after, we can effectively add rotated versions of C. For example, to effectively add R(C), we could: Rotate v by 90° clockwise, then add C, then rotate back by 90° counterclockwise (or three times clockwise). But we are only allowed to rotate clockwise, not counterclockwise. But we can rotate clockwise multiple times; rotating three times clockwise is equivalent to counterclockwise once. So we can indeed perform net rotations to get back. More systematically, to add a rotated version R^k(C) to the original vector, we can do: rotate the current vector by k times clockwise, then add C, then rotate back by (4 - k) times clockwise? Actually, if we want to add R^k(C) to the vector v (in original orientation), we can do: first rotate v by k (so v becomes R^k(v)). Then add C: becomes R^k(v) + C. Then rotate back by -k (which is 4-k times clockwise) to return to original orientation? But we need to end up with vector in original orientation? Not necessary; we only need final vector to be B, not necessarily same orientation as start. But we can incorporate the rotations into the final rotation t. So indeed, we can effectively add any R^j(C) by appropriate sequence: for example, if we want to add R^1(C) to the current vector, we could: rotate by 1 (so v -> R(v)), add C (giving R(v) + C), then rotate by 3 (so R^3(R(v)+C) = R^4(v) + R^3(C) = v + R^3(C)). That's not R^1(C). Hmm.
Let's think more carefully. Suppose we have a current vector v. We can apply a sequence of operations. The net effect of adding C at some point and rotating before and after can produce contributions of rotated C. The expression we derived earlier (v = R^t A + ∑ m_j R^j C) came from modeling exactly the allowed operations: only rotate and add fixed C. That derivation is correct. So indeed the reachable set is as we described: all vectors of the form R^t A + sum over non-negative integer combinations of R^j C, where j = 0,1,2,3? But careful: In our derivation, we ended with m_j for j=0..3 where m_j are counts of additions that end up as R^j C after final rotation. That came from the expression v = R^t A + ∑{d} n_d R^{t-d} C. Setting j = t-d, we get v = R^t A + ∑{j} m_j R^j C where m_j = n_{t-j} are non-negative. So yes, the coefficients are for R^j C directly. So the set is exactly R^t A + non-negative integer combinations of the four vectors R^0(C)=C, R^1(C), R^2(C), R^3(C). So indeed we can add rotated versions of C indirectly via sequences. So the generators available for the semigroup are precisely the four rotated vectors of C. So our model is correct. Good.
Thus the representation D_t = sum_{j} m_j R^j C with m_j >= 0 is valid. And we argued that if D_t ∈ L (integer span of C and R(C)), then we can find integers u,v such that D_t = u C + v R(C). Then we can express using non-negative m_j as described. However, note that the representation we used (splitting u and v into positive/negative parts) uses m0 = max(u,0), m2 = max(-u,0), m1 = max(v,0), m3 = max(-v,0). This gives m_j >= 0 and D = m0 C + m1 R(C) + m2 R^2(C) + m3 R^3(C). Indeed because R^2(C) = -C and R^3(C) = -R(C). So it's a valid representation with non-negative coefficients. So indeed D_t ∈ L iff it is reachable.
Thus the condition is exactly: ∃ t such that D_t ∈ L.
Now we must also consider the case where C = (0,0). Then L = {0} and we check D_t = 0.
Now, what about when C is non-zero but the determinant is not 1? Then L is a proper sublattice. So we need to check divisibility.
But is there any nuance with the sign of denom? Since C_x^2+C_y^2 is always non-negative, and positive if C non-zero. So fine.
Now we should also consider that the operations may be performed any number of times, but the representation we gave assumes that the total rotation count t is taken modulo 4. But is it possible that the reachable set includes vectors where the coefficient of A is not exactly R^t A for some t mod 4? Could we have something like: we rotate A, add C, then rotate again, etc., leading to a term like R^t A plus something, but could the A part be multiplied by something else? No, because we never multiply A by any factor; we only rotate it. So the contribution of A is always some rotation of the original A (since addition of C does not affect the A part except that rotations apply to the whole vector). So indeed the A part is R^t A for some t.
Thus condition stands.
Now we should also think about whether the order of operations could produce a scenario where we effectively "discard" A? For example, could we make A disappear by adding its negative? No, because we cannot subtract A. So the A contribution is always present, rotated.
Thus problem solved.
But wait, there's a possibility: If C is zero, we already handled. If C is non-zero, L may be the whole ℤ^2 if |C|^2 = 1? Actually if C = (±1,0) or (0,±1) then |C|^2=1, then the condition reduces to D_t being any integer vector because u,v will be integers automatically? Actually if |C|^2=1, then denom=1, so num1 and num2 are integers, and divisibility always holds. So always YES? Not exactly; we still need to check that there exists t such that D_t is any integer vector? But D_t is integer vector because A,B are integer vectors, rotations preserve integer coordinates? Rotating (x,y) by 90° clockwise gives (y,-x) which is integer. So D_t is integer vector. So if denom=1, then for any D_t integer, u and v computed as above will be integers? Let's check formulas: u = (C_x D_x + C_y D_y) / 1 integer? C_x and C_y are integers, D_x,D_y integers, so yes integer. v = (C_y D_x - C_x D_y) / 1 integer. So divisibility holds automatically. Thus condition reduces to: ∃ t such that ... but since divisibility always holds, we just need to check existence of t? Actually if C has length 1, then for any t, D_t will be in L because L=ℤ^2. So if there is any t at all, it's automatically in L. But we have 4 t's. So the condition would be that B is some vector that can be expressed as R^t A + something in ℤ^2. Since ℤ^2 is everything, we can always choose t? Actually we need D_t to be in L which is ℤ^2, so any D_t works. But we need to have existence of t such that D_t is in L. Since L=ℤ^2, any D_t is in L. So the condition becomes: is there any t? Wait, we always have four t's, but we are checking each t. For each t, we check D_t ∈ L. If L=ℤ^2, then all D_t are in L. So condition becomes: There exists some t? Actually if L=ℤ^2, then for each t, D_t ∈ L automatically, so the answer would always be YES? That seems too strong; maybe not. Because even if L=ℤ^2, we still need that D_t can be expressed as non-negative combination of the four rotated Cs. But we argued that if D_t ∈ L, we can represent it as non-negative combination because we have both signs. However, that argument used that we have both C and -C etc. But if L=ℤ^2, that means that the integer span is everything; but does it guarantee that any integer vector can be expressed as non-negative combination of the four rotated vectors? Let's test with C=(1,0). Rotated vectors: (1,0), (0,-1), (-1,0), (0,1). As earlier, we can represent any integer vector with non-negative combos: e.g., (x,y) can be done by using positive and negative basis. So yes. For C=(0,1) same. For C=(1,1) has length sqrt(2), but denom=2, not 1. So length 1 only for unit coordinate vectors (or (-1,0),(0,±1)). So those indeed give L=ℤ^2. So for those, answer always YES? Let's test: Suppose A=(0,0), B=(1,2), C=(1,0). According to our reasoning, should be YES. Can we get (1,2) from (0,0) using rotations and adding (1,0)? Starting from (0,0). We need to generate (1,2). Let's try: Since we have unit vectors in all four directions via rotations and additions, we can produce any lattice point. For example, add C once => (1,0). To get (1,2), we need to add (0,2) or something. How to get (0,2)? We can add R^3(C) = (0,1) twice. To add R^3(C), we need a sequence: rotate 3 times (or 1 rotation? Actually to get R^3(C) effect, we could do: rotate 3 times (or -1), add C, then rotate 1 time? Let's find a sequence that effectively adds (0,1). Standard method: To add R^3(C) to current vector v, we can: rotate v by 3 (so R^3(v)), add C, then rotate by 1 (so R(R^3(v)+C)=R^4(v)+R(C)= v + R(C)). That gives R(C) not R^3(C). Hmm.
Better to follow our constructive representation: We need to end with final vector v = R^t A + sum m_j R^j C. So for A=(0,0), we can choose t=0 for simplicity. Then we need m0,m1,m2,m3 such that m0C + m1R(C)+ m2R^2(C)+ m3R^3(C) = (1,2). For C=(1,0), R(C)=(0,-1), R^2(C)=(-1,0), R^3(C)=(0,1). So we need (m0 - m2, -m1 + m3) = (1,2). So we need non-negative integers m0,m2 such that m0 - m2 =1 => we can set m0=1, m2=0. And -m1+m3 =2 => m3 - m1 =2. Choose m1=0, m3=2. So m0=1, m1=0, m2=0, m3=2. That's a valid non-negative combination. So according to our model, it's reachable. Now we need to actually construct a sequence of operations achieving these counts. Is it always possible to realize any combination of m_j? We derived the expression from the operations, and it is necessary and sufficient: given any t and non-negative m_j, there is a sequence of operations that yields exactly that representation. Is that always constructible? Let's think: The expression came from counting contributions. Given t and m_j, can we find a sequence? For a given final rotation t, the number of additions that end up as R^j C is m_j. But we need to schedule additions and rotations appropriately. For each addition that contributes R^j C, it must have been performed when the rotation state was d such that after subsequent rotations (t-d) = j (mod 4), i.e., d = t - j mod 4. So we need to have exactly m_j additions when the rotation state is d = t - j mod 4. And the total rotations after the last addition must be such that final rotation count is t. So we can construct: start with rotation count = 0. We can intersperse rotations and additions. To achieve exactly m_j additions at state d, we need to ensure that we are at state d at those times. This is possible: we can plan the sequence: we can perform some rotations to get to state d, then add C m_j times (each addition at that state), then perhaps rotate to next state, etc. Also we need final total rotations modulo 4 to be t. So we can order additions in any way. So yes, it's possible. So our model is sufficient.
Thus the algorithm stands.
But we must double-check the derivation of u and v formula and the divisibility condition. In the system:
D = uC + vR(C). That is a linear equation. For integer solution, we need that the vector D is in the integer lattice generated by C and R(C). Equivalently, the two scalars computed should be integers.
Alternatively, we could check if D can be expressed as combination with integer coefficients by checking if the determinant condition: (C_x^2 + C_y^2) divides both (C·D) and (C_y D_x - C_x D_y). That's necessary and sufficient because the matrix is invertible with determinant -|C|^2. So the solution (u,v) will be integer if and only if those two numbers are multiples of |C|^2.
But is there a scenario where C and R(C) are linearly dependent? Only if C = (0,0). So we handle separately.
Now, we must also consider the case where C = (0,0) and A and B are arbitrary; we already covered.
Now, we must consider performance: all numbers up to 1e8, squares up to 1e16, which fits in 64-bit signed integer (2^63 ~9e18). So we can use 128-bit or Python's big integers; but in C++ we need 128-bit? 1e8^2=1e16, product of two such: C_x * D_x could be up to 1e8 * (B - RA) maybe up to 2e8? Actually coordinates up to 1e8, differences up to 2e8. So product up to 2e16. Sum of two such up to 4e16, still within 64-bit signed? 2^63-1 ~9.22e18, so it's safe. In C++ we could use long long (64-bit). But we must be careful with absolute values; but 4e16 < 9e18, safe. However, if we use int, it's 32-bit and overflow. So use long long.
But in Python it's fine.
Now, we should test with some custom cases.
Case: A=(1,1), B=(2,2), C=(1,1). Rotations of A: (1,1), (1,-1), (-1,-1), (-1,1). For t=0: D=(1,1). denom=2. num1=11+11=2 divisible by 2. num2=11-11=0 divisible by 2. So YES. Indeed we can: start (1,1), add C => (2,2).
Case: A=(1,0), B=(0,0), C=(1,0). t=0: D=(-1,0). num1=1*(-1)+00 = -1 divisible by 1. num2=0(-1)-1*0=0 divisible by 1. So YES. Can we get zero? Starting (1,0). Need to subtract 1 from x. We have only positive additions? But we can use rotations to add negative direction? For example, add R^2(C) = (-1,0). How to add (-1,0)? Sequence: rotate twice (so (1,0)->( -1,0)? Actually rotate (1,0) twice gives (-1,0). Then add C (1,0) gives (0,0). Then rotate back? But final vector must be (0,0). If we do: rotate 2: v = R^2(1,0)=(-1,0). Add C: v = (0,0). That's it; final rotation count t=2? Wait we performed 2 rotations, then add, then we stop. So final vector is (0,0) and final rotation count is 2? But our representation says final vector = R^t A + sum m_j R^j C. For t=2, A part = R^2(A)=(-1,0). Then we added C at state d=2? Since we rotated twice to state 2, then added C, so that addition contributes R^{t-d} C = R^{2-2} C = C? Actually t is total rotations =2. At addition, rotation state d=2 (since we did two rotations before adding). Then contribution after final (no more rotations) is R^{t-d} C = R^0 C = C. So v = R^2 A + C = (-1,0)+(1,0)=0. So indeed matches with m0=1, others 0. So it works.
Case: A=(2,0), B=(0,0), C=(1,0). t=0: D=(-2,0). denom=1. num1=1*(-2)= -2 divisible by 1. num2=0. So condition says YES. Can we? Start (2,0). We need to subtract 2 in x direction. We can add (-1,0) twice? But we can add R^2(C)=(-1,0). To add (-1,0) once: rotate twice -> (2,0) -> (-2,0) (since R^2(2,0)=(-2,0)). Then add C (1,0) => (-1,0). Then rotate? Hmm maybe not. Better: Use two additions of R^2(C) and maybe final rotation? Let's use representation: We want v = R^t A + sum m_j R^j C = 0. For t=2, A part = R^2(2,0)=(-2,0). Then we need sum m_j R^j C = (2,0) to cancel. That sum must equal (2,0). With C=(1,0), R(C)=(0,-1), R^2(C)=(-1,0), R^3(C)=(0,1). To get (2,0), we can use m0=2 (add C twice) or m2= -2? But m2 must be non-negative, so we cannot subtract. We need to add positive (2,0). But after t=2, we are adding at state? Actually the sum is of R^j C. So if t=2, we can have m_j for j=0..3. To get (2,0) we can use m0=2. Then v = (-2,0) + 2*(1,0)=0. So we need to have two additions that contribute C (i.e., j=0). For t=2, an addition contributes R^j C where j = t - d mod 4. For j=0, we need d = t mod 4 =2. So we need to add C twice when rotation state =2. So sequence: rotate twice (state=2), add C twice, then stop (no more rotations). That yields v = R^2(A) + 2*C = (-2,0)+(2,0)=0. So works. So YES.
So the condition works.
But we need to be careful about the case where C is non-zero but D_t = 0. That is always reachable if 0 ∈ S. And 0 is in S because we can take all m_j =0. So if D_t =0 for some t, answer YES. That's covered by divisibility condition: num1=0, num2=0, divisible by denom always, so YES.
Now consider a case where A, B, C are such that D_t is not in L for any t. Then NO.
Now we should also consider if the operations are allowed infinite number of times; but finite is fine.
Now we should test edge cases: C with large values, etc.
But before finalizing, we must double-check the derivation of the expression for v (final vector). I want to be absolutely sure that the set of reachable vectors is exactly:
V = { R^t A + Σ_{j=0}^{3} n_j R^j(C) | t ∈ {0,1,2,3}, n_j ∈ ℕ }.
We derived by considering contributions of additions after all rotations. Let's do a formal proof:
We maintain a current vector v. Initially v = A. We'll record the number of rotations performed so far, r (mod 4). But maybe better to think of the process as building an expression:
Let the operations be a sequence. Each operation either rotates the current vector (v := R(v)) or adds C (v := v + C). Rotations are linear, so the final vector can be expressed as:
v = R^{t} A + Σ_{i=1}^{k} R^{t - t_i} C
where t is total number of rotations performed, and t_i is the number of rotations performed before the i-th addition. (We number additions in order). Since rotations are applied after some additions, the term R^{t - t_i} C is the contribution of that addition after all subsequent rotations. This is clear if we think of pushing all rotations to the end: If we have a sequence, we can commute rotations? Not exactly, but we can factor: For a sequence of operations, we can write final vector as:
Let the product of operations from left to right. Represent each addition as adding C at that moment, but then later rotations multiply it. So indeed the formula holds.
Now, t_i can be any integer between 0 and t inclusive (since rotations only happen before addition). But also note we can rotate after all additions, so some t_i = t? Actually if an addition occurs after all rotations, then t_i = t (since all rotations have been performed before that addition?), careful: If we have additions at the very end, after all rotations, then there are no rotations after that addition, so the contribution is R^{0} C = C. In the formula, t - t_i = 0, so t_i = t. But t_i counts rotations performed before that addition. So if addition occurs after all rotations, then indeed t_i = t. So t_i ∈ [0, t].
Now, t can be any non-negative integer. However, note that R^{4} = I. So for any t, R^{t} = R^{t mod 4}. Also, the contributions R^{t - t_i} C = R^{(t - t_i) mod 4} C because R^4 = I. So we can consider t mod 4, and we can group additions by the value of (t - t_i) mod 4, which is j. So we can write:
v = R^{t mod 4} A + Σ_{j=0}^{3} m_j R^{j} C
where m_j = number of additions i such that (t - t_i) mod 4 = j. Since t and t_i are non-negative integers, the set of possible j for given t and t_i is all four possibilities depending on t_i mod 4 relative to t. But can any j occur? Yes, because we can schedule additions appropriately; given a desired set of m_j, we can choose t mod 4 and then arrange additions at times with appropriate t_i mod 4 to achieve the j values. For a fixed t (the total rotations count), we can have any number of additions with each j by inserting rotations appropriately. Moreover, we are free to choose t arbitrarily (any number of rotations, but only mod 4 matters for the A part). However, note that t itself (the total number of rotations) may affect the ability to achieve certain m_j because the number of rotations before an addition cannot exceed t. But we can always increase t by adding extra rotations at the end (which don't affect contributions of previous additions because they will be rotated further). Actually adding extra rotations after all additions will increase t, and also change j for each addition? If we add extra rotations after all additions, then for each addition, t_i remains same, but t increases, so j = (t - t_i) mod 4 changes. So the mapping between m_j and the actual addition counts depends on t. However, we can also add extra rotations before additions. The key is: For any desired combination (t_mod, m_0,m_1,m_2,m_3) with t_mod ∈ {0,1,2,3} and m_j non-negative integers, can we realize it? I think yes, by the following construction:
Choose total rotations T such that T ≡ t_mod (mod 4) and T is large enough to accommodate required numbers of additions at specific mod residues.
We need to schedule additions at times when the rotation state (the number of rotations performed so far) r (mod 4) is something. For an addition to contribute R^j C in final vector, we need that after all subsequent rotations (which will be T - r more rotations), the net rotation applied to that C is j mod 4, i.e., (T - r) mod 4 = j, or equivalently r ≡ T - j (mod 4). So we need to perform additions exactly when the current rotation count mod 4 equals d = (T - j) mod 4.
So we need to be able to have for each j, exactly m_j additions at times when r mod 4 = d_j = (T - j) mod 4.
We can achieve this by the following: Start with r=0. We'll perform rotations step by step, and at each state we may add C multiple times. Since we can increase r arbitrarily by rotating, we can visit each state as many times as we like? Actually we can only increase r (since rotations are only clockwise, they increase the count). So the sequence of r values is monotonic increasing. So we start at r=0, then we may add some number of C at r=0, then rotate to r=1, add some number of C at r=1, then rotate to r=2, add at r=2, then rotate to r=3, add at r=3, then rotate to r=4 (which is 0 mod 4), etc. So we can only add at states in increasing order, and we cannot go back to a previous state (mod 4) without completing a full cycle (i.e., increasing r by 4 to return to same mod class). So we can add at state 0 multiple times by doing several full cycles? For example, after r=3, if we rotate again we go to r=4 (mod 4=0) and can add again at state 0. So indeed we can have as many additions as we want at any state modulo 4, by performing enough rotations to cycle through states.
So given any desired counts m_j, we can plan a sequence: Let T be some large number (the total rotations) that we will decide later. Actually we need to choose both T and the schedule. The condition that the final rotation count T must be such that the mapping from j to state d = (T - j) mod 4 is fixed. So for a given T mod 4, we have a bijection between j and d. For each j, we need m_j additions at state d. Since we can achieve any non-negative counts at each state by cycling, we can realize any multiset of state visits as long as we can schedule the visits in order. The typical method: We can start at state 0, then add some number of C's for the states we need in order. But we need to ensure that we can have exactly the required number at each state before moving to the next. Because we can only pass through states in a cyclic order: 0 -> 1 -> 2 -> 3 -> 0 -> 1 -> ... . So if we need, say, many additions at state 0 and many at state 2, we can do: add at state 0 (some), then rotate to 1 (maybe add 0), rotate to 2, add some, then rotate to 3 (maybe 0), rotate to 0 again, add more at state 0, etc. So we can intersperse cycles. So we can achieve any desired counts for each state, provided we can perform enough rotations to go through cycles. The only constraint is that the total rotations T must be at least the number of state transitions we do. But we can always make T large enough to accommodate all additions.
Thus any non-negative m_j and any t_mod is achievable. Therefore, the reachable set is exactly as described. So condition is correct.
Now we should also consider the scenario where C = (0,0) separately; our formula would involve division by zero, so handle.
Now, there is another nuance: The addition operation says "Add to the vector a certain vector C." Could it be that C is a fixed vector, but we are allowed to add it only once? The phrase "Add to the vector a certain vector C." In many problems like this, it means you can add the vector C to the current vector any number of times. So it's fine.
Now we might ask: Could there be a more subtle constraint like we cannot have an addition after a rotation that would make the state count not integer? No.
Thus solution: For t in 0..3: compute RA = rotate(t). Compute D = (B.x - RA.x, B.y - RA.y). If C == (0,0): if D == (0,0): YES. Else continue. Else compute denom = C.x^2 + C.y^2 (as long long). Compute num1 = C.x * D.x + C.y * D.y; num2 = C.y * D.x - C.x * D.y. Check if num1 % denom == 0 and num2 % denom == 0. Note: In languages like C++, negative modulo might give negative remainder; but we can check ((num1 % denom) == 0) using modulo that may be negative; to be safe, we can check if num1 mod denom != 0 using (num1 % denom) != 0; but if num1 is negative, % may give negative remainder; however (num1 % denom) == 0 will still be true if num1 is multiple of denom because remainder 0 regardless sign. In C++, -4 % 2 == 0? Yes, -4 % 2 = 0. So it's fine. But if denom could be negative? No, denom is sum of squares, always positive. So fine.
Alternatively, we can check (num1 * 1LL) % denom == 0 etc.
But we need to be careful with overflow when computing num1 and num2; use long long.
Now, what about the case where C is non-zero but D_t is not integer? D_t is integer.
So algorithm is straightforward.
But let's test more edge cases.
Edge 1: C = (0,0), A = (1,1), B = (1,1). Then t=0 gives D=(0,0) -> YES. B = (1, -1)? Rotating A: (1,1) rotated once gives (1,-1)? Actually clockwise: (1,1) -> (1, -1)? Let's compute: (x,y) -> (y, -x) => (1,1) -> (1, -1)? That's (y, -x) = (1, -1). So B = (1,-1) is reachable by just rotating once, no adds. Our condition: C=0, D_t= B - R^t A. For t=1, D = (1,-1) - (1,-1) = (0,0) -> YES.
Edge 2: C = (0,0), A = (1,1), B = (2,2). D_t = (2,2) - R^t (1,1). Rotations: t0: (1,1); t1: (1,-1); t2: (-1,-1); t3: (-1,1). None equal (2,2). So D_t ≠ 0 for all, so NO.
Now test C = (2,0), A=(0,0), B=(1,0). denom=4. D=(1,0). num1=21+00=2, not multiple of 4 => no. t others same. So NO. Can we get (1,0)? Starting from (0,0), we can only add (2,0) or its rotations: (0,-2),(-2,0),(0,2). So reachable vectors are multiples of 2 in coordinates? Actually sums of these produce even coordinates only? Let's check: Adding (2,0) and (0,2) yields (2,2) (even even). Adding (2,0) and (-2,0) yields (0,0) (even). So all coordinates will be even. (1,0) has odd x, so cannot be reached. So NO.
Test C = (2,0), A=(1,0), B=(1,0). D_t = (0,0) -> YES (t=0). That's trivial.
Test C = (2,0), A=(1,0), B=(3,0). D=(2,0). denom=4, num1=2*2=4 divisible? 4%4=0, num2=0. So YES. Indeed: rotate? Actually we can just add C once: (1,0)+(2,0)=(3,0). So YES.
Test C = (2,0), A=(1,0), B=(0,0). D=(-1,0). num1=2*(-1)=-2, not divisible by 4? -2%4 !=0. So condition says NO. But can we reach (0,0) from (1,0)? Starting (1,0). We have C=(2,0). To get 0, we need to subtract 1. But we can't subtract directly. We could use rotations to get -2,0 etc. Let's try: rotate twice: (1,0)->(-1,0). Add C: (-1,0)+(2,0)=(1,0). Back to start. Not helpful. Maybe use multiple steps: rotate 1: (1,0)->(0,-1). Add C: (2,-1). Rotate 3: ( -? ) Let's see if we can get (0,0). The reachable set from A is A rotated plus even coordinate combinations? Actually from our model, reachable vectors = R^t A + L, where L = lattice spanned by (2,0) and (0,-2)? Actually L = { u*(2,0)+ v*(0,-2) } = all vectors with coordinates (2u, -2v). But also we have -C and -R(C) in the semigroup, so L = { (2u, -2v) } with u,v ∈ ℤ. So L consists of vectors where x is even and y is even? Wait: (2,0) gives even x,0. (0,-2) gives 0, even y. So any combination yields (2u, -2v) => x even, y even. But also we have R^2(C)=(-2,0) and R^3(C)=(0,2) which are also even. So indeed L = all vectors with both coordinates even. So reachable set is R^t A + (even, even). A = (1,0). R^0 A = (1,0) -> plus even gives odd x, even y? (1+even, 0+even) => x odd, y even. R^1 A = (0,-1) -> (even, -1+even) => x even, y odd? Actually (0+even, -1+even) => x even, y odd (since even plus even = even? Wait, -1+even: even can be 0, ±2, ±4,... so -1+even is odd). So y odd. R^2 A = (-1,0) -> x odd, y even? Actually (-1+even,0+even) => x odd? -1+even: if even=2k, then -1+2k is odd? For k integer, -1+2k = 2k-1 which is odd. So x odd, y even. R^3 A = (0,1) -> x even, y odd. So reachable vectors always have one coordinate odd, one even? Actually (odd, even) or (even, odd). (0,0) has both even, so not reachable. So NO, correct.
So condition works.
Now consider C = (2,2). Then denom = 8. L is the lattice spanned by (2,2) and (2,-2)? Actually R(C) = (2, -2) since (x,y) -> (y,-x) gives (2,-2). So L consists of vectors u*(2,2)+v*(2,-2) = (2(u+v), 2(u-v)). So coordinates are both multiples of 2, and sum/difference conditions? Actually any vector (2a, 2b) where a,b integers? Let's see: set s = u+v, t = u-v, then a = s, b = t? But s and t have same parity? Because u = (s+t)/2, v = (s-t)/2 must be integers, so s and t must have same parity. So L = { (2a,2b) | a,b integers, a ≡ b (mod 2) }? That is, both even, and a and b same parity. For example, (2,0): a=1,b=0 -> parity different (1 and 0) not same, so (2,0) not in L? Check if (2,0) can be expressed: solve 2(u+v)=2 => u+v=1, 2(u-v)=0 => u-v=0 => u=v, then u+v=2u=1 => u=0.5 not integer. So (2,0) not in L. But our semigroup S includes also -C and -R(C) so S = L, same. So reachable vectors are A rotated plus L. So condition is that D_t must be in L. So we must check divisibility by 8 and also parity conditions? But our condition with num1 and num2 divisibility by denom (8) is necessary and sufficient for being in L? Let's verify: For L as described, D ∈ L iff there exist integers u,v such that D = uC + vR(C). This is equivalent to solving the linear system with integer u,v. The solution is given by u = (C·D)/|C|^2, v = (C_y D_x - C_x D_y)/|C|^2. For C=(2,2), |C|^2=8. So these must be integers. That is exactly our condition. So if both are integers, then D ∈ L. So (2,0) would give C·D = 22+20=4, 4/8=0.5 not integer; so fails. Good.
Thus condition is correct.
Now test: A=(0,0), B=(2,0), C=(2,2). denom=8, D=(2,0). num1=22+20=4 not multiple of 8 => NO. Is it reachable? We cannot because L does not contain (2,0). So NO.
Now test: A=(0,0), B=(4,0), C=(2,2). D=(4,0). num1=24+20=8, divisible by 8; num2=24-20=8, divisible by 8? Actually num2 = C_y D_x - C_x D_y = 24 - 20 =8. 8%8=0. So condition says YES. Is (4,0) reachable? Check L: need u,v such that (4,0)= u*(2,2)+ v*(2,-2) = (2(u+v), 2(u-v)). So we need 2(u+v)=4 => u+v=2; 2(u-v)=0 => u=v => then u+v=2u=2 => u=1, v=1. So D = C + R(C) = (2,2)+(2,-2)=(4,0). So indeed (4,0) ∈ L. And we can represent with non-negative coefficients: m0=1 (C), m1=1 (R(C)). So reachable. Good.
Thus algorithm passes.
Now we need to code.
Implementation steps:
Read input as integers.
Define function rotate(x,y,k): for k times clockwise: (x,y) -> (y,-x). We can implement by loop or by formula: after 0: (x,y); 1: (y,-x); 2: (-x,-y); 3: (-y,x). We'll compute directly.
For t in range(4):
compute (rx, ry) = rotation of A.
compute dx = Bx - rx, dy = By - ry.
if C == (0,0):
if dx == 0 and dy == 0: print YES; return.
else:
denom = CxCx + CyCy (as long long)
num1 = Cxdx + Cydy
num2 = Cydx - Cxdy
if num1 % denom == 0 and num2 % denom == 0:
print YES; return.
After loop, print NO.
But be careful: When C is non-zero, the condition must also hold for D = (0,0) trivially because num1=0, num2=0 divisible. So that covers the case B = R^t A.
Now consider special cases: what if C is non-zero but |C|^2 is not a divisor of 0? 0 divisible by any non-zero integer is true. So fine.
But wait: In sample 2, C=(1,1) denom=2, but is D=(1,1) actually in L? C·D = 2, /2 =1 integer; cross = 0, integer. So u=1, v=0 => D = C? Actually D=(1,1) = C, which is in L. So reachable: start A=0, add C yields B. So YES.
Now consider more complex cases: Could there be a scenario where D_t is in L but the representation requires m_j that are not achievable because we cannot have m_j additions at the needed states due to the fact that we cannot have negative t_i? But we argued it's always possible. However, let's double-check with an example where we want many contributions of a certain rotated version. Suppose we want m0 large, m2 large, but m1=m3=0. For t=0, contributions: j=0 requires state d = t - j mod 4 = 0; j=2 requires d = (0-2) mod 4 = 2. So we need to add C at states 0 and 2. Can we add at state 0 and state 2 arbitrarily many times? Yes: start at state 0, add some m0, then rotate to state 1 (maybe no adds), rotate to state 2, add m2, then optionally rotate to state 3 and back to 0 for more m0, etc. Since we can cycle, we can achieve any numbers. The final rotation count t will be the total rotations we perform. To keep t=0 mod 4, we need to end with total rotations being a multiple of 4. If we add after some cycles, we need to end with t mod 4 = 0. If we start at 0, and we perform some rotations, we can return to state 0 after 4k rotations. So we need to end after a number of rotations that is multiple of 4. That can be arranged: after doing the additions, we can add extra rotations to adjust the total to a multiple of 4, but that will change the contributions j because extra rotations after additions will change the j mapping. So careful: In our construction, we assumed a fixed total rotations t (mod 4). The mapping from state d to final j depends on total rotations t. So if we want a specific t_mod, we must end with total rotations t such that t mod 4 = t_mod. We can always add extra rotations at the very end, after all additions. But adding extra rotations at the end changes the contributions? Actually if we add rotations after all additions, then for each addition, the number of subsequent rotations increases, so j = (t - t_i) mod 4 changes. So if we already scheduled additions with the intention that j = (t - t_i) mod 4, and then we add more rotations at the end, the j values would all increase (mod 4). So we cannot simply add extra rotations at the end without altering the representation. However, we can add extra rotations before any additions? But that changes state for additions. So we need to design the sequence to exactly achieve both the desired m_j and t_mod. This is a more subtle scheduling problem: given desired m_j and t_mod, can we always find a sequence? We need to prove that for any non-negative integers m_j and any t_mod ∈ {0,1,2,3}, there exists a sequence of operations (rotations and additions) such that final vector is Σ m_j R^j C + R^{t_mod} A (with t_mod being the total number of rotations modulo 4). In our earlier reasoning, we thought we could arbitrarily assign states to additions as d = (t_mod - j) mod 4, and then just perform rotations and additions in order to achieve those counts, making sure that total rotations at the end is exactly some T with T mod 4 = t_mod. But we need to be able to end exactly at that state after finishing additions. If we schedule additions at states d as needed, the total rotations we perform will be the number of rotations we used to move between states. We can decide when to stop. The final rotation count will be the state we are in after the last addition (if we don't rotate after last addition). Or we could rotate after last addition to adjust the final t_mod, but that would change contributions of previous additions. So we need to have the final rotation count after the last addition equal to the desired t_mod (mod 4) because we aren't going to rotate after additions (since that would change contributions). But we could also rotate after the last addition, but that would add to the contributions? If we rotate after the last addition, then those rotations would apply to the whole vector, which would effectively change the representation: the A part would be rotated further, and each addition's contribution would be rotated further. That would change the set of j coefficients. So to achieve a specific representation (t_mod, m_j), we cannot have extra rotations after the last addition; the last operation should be an addition (or we could have rotations after but then we would have to adjust m_j accordingly). So we need to construct a sequence where after the last addition, the total rotations performed so far (T) satisfies T mod 4 = t_mod, and during the sequence, each addition happened at a state d such that (T - d) mod 4 = j for that addition. Since T is the total rotations at the end, which is after all operations (no further rotations). So if we want T mod 4 = t_mod, we need to end with T rotations and no further operations. We can design the sequence such that we end after some additions, with T being whatever we have rotated. But we have control: we can rotate to states, add, and then perhaps rotate to a new state to add more, and finally after the last addition we stop. The total rotations T is the number of rotations performed overall. The state at the moment of an addition is the number of rotations performed before that addition, mod 4. So given desired m_j and t_mod, we need to find a sequence of state transitions and additions that ends with total rotations T such that T mod 4 = t_mod and for each addition, if its assigned j, then the state d must satisfy (T - d) mod 4 = j. But note that T is not known a priori; it is the total rotations. If we fix T, then the mapping from j to d is d = (T - j) mod 4. So for each j, we need to have exactly m_j additions at states d = (T - j) mod 4. The sequence must start at state 0 (initial rotations count 0) and proceed by increasing rotation count (we can only increase by rotating). We can add at any state any number of times. The sequence ends after the last addition, with total rotations = T. So the final state (after all rotations) is T mod 4 = t_mod, which is consistent.
Thus the problem reduces to: Given desired counts n_d for each state d (where n_d = number of additions to be performed at state d), can we schedule a sequence of rotations and additions such that we start at state 0, we can visit states in order (increasing rotation count), add the required numbers, and finally end with total rotations T, where T mod 4 = t_mod, and the state counts n_d are as given? Moreover, the total rotations T is the final rotation count, which is the number of rotations performed. The sequence of states visited is a path from 0 to T, where we increment rotation count by 1 each time we rotate. Between rotations, we can add C any number of times at the current state. So essentially we need to produce a sequence: Start at r=0; we can add a0 times; then rotate (r becomes 1); add a1 times; rotate to 2; add a2; rotate to 3; add a3; rotate to 4 (which is 0 mod 4); add a4 times? But careful: The state d is defined as r mod 4, but the actual rotation count r increases. When we rotate from state 3 to state 0 (since 3+1=4, mod4=0), we are still at a higher rotation count, but the state (mod4) is 0 again. So we can have multiple visits to the same state modulo 4, at different times. For example, we can add at state 0 at the beginning (r=0), then after some rotations we can come back to state 0 (r=4,8,...) and add again. So the total number of additions assigned to state d (mod 4) can be split across multiple visits to that state. So the condition is: We need to be able to assign additions to visits such that the total number of additions at state d (across all visits) equals n_d. This is always possible as long as we can make enough visits to each state.
Given we can rotate arbitrarily many times, we can always achieve any distribution n_d. For example, we can follow this schedule:
Start at r=0. Add all additions needed for state 0? But if we add all at r=0, then we will never visit state 0 again unless we do full cycles. But we can also postpone some additions for state 0 to later cycles, but that's okay. The simplest constructive method: Choose a large T, say T = t_mod + 4 * K, where K is large enough. We'll perform exactly T rotations total. Then we want exactly n_d additions at states d = (T - j) mod 4. We can just perform rotations step by step from r=0 to r=T, and at each step, before rotating, we add the required number of times for that state. But we need to know, for each r from 0 to T-1, what is its state mod 4 = r mod 4. At each such r, we can add some number of C. At the final r=T, we stop (no addition after final rotation because we stop after additions? Actually we can add after the last rotation? The sequence: we can add, then rotate, add, rotate,... The last operation can be an addition. So we need to decide at which r we add. Typically, we can have: for r=0, add some; then rotate to r=1; add some; ... rotate to r=T-1; add some; then rotate to r=T? Or we could end with addition after the last rotation: that would be addition at state T (i.e., after T rotations). But if we add after the last rotation, then the addition occurs at state r = T (since rotations count is T before addition). But in our model, t_i is number of rotations before addition; for an addition after all rotations, t_i = T. So that addition would contribute R^{T - T} C = C, i.e., j=0. So its j is 0. That corresponds to state d = T mod 4, and j = (T - d) mod 4 = 0. So additions after final rotation are also possible, but then we don't rotate after them. So we can include additions at the final state T as well, with j=0 always? Actually if we add after final rotation, then no further rotations, so contribution is C (j=0). So j is fixed to 0 for additions after the last rotation. So to get other j values at the end, we would need to not have final additions, or have them with j=0. So it's simpler to consider that all additions occur before the final rotation count is reached, and the last operation is a rotation to achieve final T. But we could also have additions after some rotations and then stop without further rotations; in that case the total rotations T is the number of rotations performed, and the last operation is an addition. In that scenario, the additions after the last rotation have t_i = T, and contribute j=0. So we can have additions at state T (which is mod 4 = T mod 4) that contribute j=0. So we have flexibility.
But to avoid complexity, we can always construct a sequence that ends with a rotation (i.e., last operation is rotate) so that the final rotation count T is as desired and all additions are done before the final rotations? Actually if we end with a rotation, then the last operation rotates the vector, and there is no addition after that. In that case, the last state we added at is some state d < T, and we rotated to T and stop. That's fine.
Given the many degrees of freedom, it's plausible that any (t_mod, m_j) is achievable. A rigorous proof would involve showing that we can realize any vector in the set as defined. Since the problem is from Codeforces likely (maybe problem 560B?), I think this is the intended solution: check if B - R^t A lies in the lattice generated by C.
Actually I recall a Codeforces problem: "Vectors" or "Geometry" similar. The typical solution is: rotate A four times, and check if (B - R(A)) can be expressed as linear combination of C and perpendicular. Or something like that.
Let's search memory: There is Codeforces Round #... Problem "Vectors" maybe. The solution often uses checking parity or gcd.
But I want to ensure no missing constraints: Could there be a scenario where D_t is in L but the required m_j coefficients are not all non-negative? As we argued, if D_t = uC + vR(C), we can set m0 = max(u,0), m2 = max(-u,0), m1 = max(v,0), m3 = max(-v,0). This yields non-negative m_j. So that's fine. So as long as u,v are integers (i.e., D_t ∈ L), we have non-negative representation. So yes.
But wait, what about the possibility that we need to use a different t? Because for a given D_t, the representation with m_j is from that specific t. But we already selected t. So if D_t ∈ L, we have representation. So condition is sufficient.
Now, is it also necessary? Suppose we have a reachable vector B. Then there exists some sequence, which yields B = R^t A + Σ m_j R^j C with m_j >=0. Then D_t = Σ m_j R^j C. Since each R^j C is in L (obviously), and L is a lattice (additive group), the sum is in L. So D_t ∈ L. So necessary.
Thus condition is both necessary and sufficient.
So the algorithm is correct.
Now, we must also consider if there is any trick with large numbers and modulo operations in C++ with negative numbers. We'll use long long.
We can implement rotate function inline.
Let's write code.
Pseudo-code in C++:
#include
using namespace std;
typedef long long ll;
int main() {
ll x1,y1, x2,y2, x3,y3;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
// We'll try all rotations of A
// Rotation 0: ( x1, y1)
// Rotation 1: ( y1, -x1)
// Rotation 2: (-x1, -y1)
// Rotation 3: (-y1, x1)
// For each, check condition with original B and C.
// If any true, print YES else NO.
bool ok = false;
// t=0
if (check(x1, y1, x2, y2, x3, y3)) ok = true;
// t=1
else if (check(y1, -x1, x2, y2, x3, y3)) ok = true;
// t=2
else if (check(-x1, -y1, x2, y2, x3, y3)) ok = true;
// t=3
else if (check(-y1, x1, x2, y2, x3, y3)) ok = true;
code
cout << (ok ? "YES" : "NO") << endl;
return 0;
}
But careful: In check, we treat C as given, but if C is zero, we special-case. Also we need to handle that denom may be zero only if C zero. So fine.
But is there any scenario where C is non-zero but denom does not divide num1 or num2 due to sign? The remainder check should be okay.
Now, note: In C++, the % operator with negative numbers: e.g., -4 % 3 = -1, not 2. But we are checking if remainder is 0, which works because if a multiple, remainder will be 0. For example, -8 % 4 = 0. So fine.
Now, the numbers: cxcx up to 1e16, fits in long long (9e18). But we also compute num1 = cxdx + cy*dy. dx and dy can be up to difference between B and rotated A. Rotated A coordinates are within ±1e8, so dx,dy up to 2e8 roughly. So product up to 1e8 * 2e8 = 2e16. Sum of two such up to 4e16. So within 9e18. So long long safe.
Now, one more nuance: When we compute rotation, we need to use correct rotation direction: clockwise 90°. The rotation matrix is (x,y) -> (y, -x). That is clockwise. Many problems use counterclockwise, but here they said clockwise. Let's confirm: Example: A=(0,0) to B=(1,1) with C=(0,1) gave YES. If we had used counterclockwise, would it still give YES? Possibly, but we must use correct. We can test with known: Suppose A=(1,0). Rotate clockwise once gives (0,-1). Rotate counterclockwise would give (0,1). We'll trust the problem.
Let's test a case to verify direction: For A=(1,0), if we rotate clockwise, we get (0,-1). If we then add C maybe? But not needed.
Now, ensure we are using correct rotation for t=1: (y1, -x1). For t=2: (-x1, -y1). For t=3: (-y1, x1). That's correct for clockwise: After two rotations: (x,y)->(y,-x)->(-x,-y). After three: (-x,-y)->(-y,x). After four: (x,y). Good.
Now, some problems consider that you can also add C before any rotation, etc. Already covered.
Thus solution done.
But wait: Could there be a case where C is not zero but the denominator divides both num1 and num2, yet the representation requires m_j that are not achievable because of the ordering constraints? Let's try to construct a potential counterexample.
Suppose we want t_mod = 0, and m0 = 1, m2 = 1, others 0. Then D = C + R^2(C) = C - C = 0? Actually R^2(C) = -C. So C + (-C) = 0. So D=0, trivial. Non-trivial: t_mod=0, m0=0, m2=0, but m1=1, m3=1: D = R(C) + R^3(C) = R(C) - R(C) = 0. So trivial. So non-zero D would involve at least one coefficient positive and possibly others zero. For a nontrivial D, say D = C (m0=1). That's easy: just add C at state 0 and then ensure total rotations 0 mod 4? To have t_mod=0, we need total rotations T mod 4 =0. We can do: start at state 0, add C, then do nothing? But then total rotations T=0, ok. So works.
What about D = R(C) (j=1). That requires m1=1. For t_mod=0, mapping: j=1 means we need additions at state d = (0 - 1) mod 4 = 3. So we need to add C when state is 3. But we start at state 0. To get to state 3, we need to rotate three times. After rotating three times, we are at state 3. Then we add C. Then we want total rotations T mod 4 =0. Currently T would be 3 (since we rotated three times). To make T mod 4 =0, we need one more rotation after addition? But if we rotate after addition, that would change the contribution of that addition: originally if we add at state 3, and then rotate additional times, the contribution becomes R^{T - 3} C. If we want final contribution to be R^1 C, we need (T - 3) mod 4 = 1 => T mod 4 = (3+1)=0 mod 4. So if after addition we rotate 1 more time, T=4, then contribution = R^{4-3}=R^1 C. And final t_mod = T mod 4 =0. So sequence: rotate 3 times, add C, rotate 1 time. That yields final vector: after 3 rotations, A becomes R^3(A); then add C => R^3(A)+C; then rotate once: R( R^3(A)+C ) = R^4(A) + R(C) = A + R(C). But our representation for t_mod=0 is v = R^0 A + m1 R^1 C = A + R(C). That matches. So sequence works. So we can always add an extra rotation after addition to adjust the total T and achieve the correct mapping, as long as we account for that extra rotation in the contribution. Indeed our construction should account for that. The mapping we used: for each addition, j = (T - d) mod 4. If we decide on T and the d at which addition occurs, we get j. So we are free to choose T and d accordingly. So given desired m_j and t_mod (which is T mod 4), we need to find a T with that mod and then schedule additions at states d = (T - j) mod 4 to get the right j. We can then construct a sequence: Starting from 0, we go through states incrementally, adding the required numbers when at state d. But we must ensure that we end with total rotations T. Since we can always continue rotating after the last addition to reach T, we can set T to be the final rotation count after all additions plus any extra rotations. However, if we add extra rotations after the last addition, they affect contributions of all previous additions because they increase T and thus change j for each addition? Wait: In the formula j = (T - d) mod 4, T is total rotations after all operations. If we initially plan to have additions at certain d values, and then we decide to add extra rotations after the last addition, then T increases, and the j values for those additions change. So we cannot independently choose T and addition d's; they are coupled. However, we can decide on T first, then schedule additions accordingly. So the design is: choose T (any non-negative integer with T mod 4 = t_mod). Then determine the required d for each j: d = (T - j) mod 4. Then we need to have exactly m_j additions at state d. Now we need to produce a sequence of rotations and additions that starts at r=0, ends at r=T, and for each r from 0 to T-1 (or maybe including r=T if we add after last rotation? Actually additions can occur at various r values. Typically, we add before rotating from a state. So at a given rotation count r, we are in state r mod 4. We can add some number of C's before we rotate to r+1. So the additions occur at r values such that 0 ≤ r ≤ T-1? Actually if we add after the final rotation, that would be at r = T (since after performing T rotations, we are at state T mod 4, and we could add without further rotations). That would be additions at r = T. Those additions would have d = T, and their contribution j = (T - T) mod 4 = 0. So they contribute only to j=0. So generally, additions can occur at r values from 0 up to T inclusive. But if we add at r = T, no further rotations, so T is final rotation count. So in our planning, we can allow additions at r = T as well, but those only contribute j=0.
But to simplify, we can avoid additions at r = T by ensuring we only add before the final rotation, and then we may have a final rotation to adjust T. However, we can always incorporate additions at r = T as j=0 contributions, which is fine.
Thus, given T and required counts for each state d (mod 4) (where d ranges over 0,1,2,3, and also possibly d = T mod 4 for additions at r=T), we need to schedule a path from r=0 to r=T, with additions allowed at each integer r (including possibly at r=T). The number of additions at each r is constrained only by the state mod 4 of that r (and for r=T, state is T mod 4). Since we can choose T arbitrarily large, we can always allocate enough visits to each state. For example, we can take T = t_mod + 4*M, where M is large enough to have at least the required number of visits to each state. The visits to a state d occur at r values ≡ d (mod 4). The number of such r values in [0, T] (inclusive) is either floor(T/4)+1 if d ≤ T mod 4, else floor(T/4). Actually count of integers r in [0,T] with r mod 4 = d is either ⌊T/4⌋ + 1 if d ≤ T mod 4 (and maybe special if we count inclusive?). Let's compute: For T = 4k + s, where s = t_mod ∈ {0,1,2,3} (since we need T mod 4 = t_mod). Then the set {0,1,...,T} includes for each residue d: if d = 0,1,2,3, the count is k+1 for residues 0..s? Actually for residues 0 <= d <= s, the count is k+1; for residues > s, count is k. Since T = 4k + s, the numbers from 0 to T inclusive: there are 4k + s +1 numbers. The residues cycle: 0,1,2,3,0,1,... up to s. So residues 0 appear at indices: 0,4,8,...,4k => k+1 times if s>=0? Actually index 0 counts as residue 0. For residue 0, indices: 0,4,8,...,4k. That's k+1 times (since 0,4,...,4k gives k+1 numbers). For residue 1: indices 1,5,...,4k+1 if 4k+1 ≤ T? That requires s ≥1. So if s≥1, then residue 1 appears k+1 times; else k times. So indeed counts can differ by at most 1. So the maximum number of times we can add at a given state d is either k+1 or k. To achieve arbitrarily large m_d, we can increase k accordingly. Since m_d can be any non-negative integer, we can always choose k large enough such that the maximum possible count (k+1) is at least m_d for each d. However, note that the counts for different residues are interdependent because the total number of additions at each residue cannot exceed the number of opportunities. But since we can increase k arbitrarily, we can satisfy any finite requirements. For any given finite m_0,m_1,m_2,m_3, we can choose k large enough so that k+1 ≥ max(m_0,m_1,m_2,m_3). Also we need to respect that the actual allowed number of additions at each residue is either k or k+1 depending on s. But if we set k large, we can always allocate the required number because even the minimum count for a residue (which is k) will eventually exceed any fixed m_d if we take k > m_d. So for sufficiently large k, we have k ≥ m_d for all d. So we can schedule exactly m_d additions at each residue d by adding that many times at the appropriate r values (just use the first m_d opportunities). This works provided we don't need to add more than the number of available r values for that residue. With k large enough, we can. So indeed any combination is achievable. This confirms.
Thus solution stands.
Now, final code in Python will be simpler. We'll implement.
But wait, there is also the possibility that the vector C is zero. In that case we only have rotations. So we check if B is one of the four rotations of A. That's covered.
Now, let's test with more random cases to be confident.
We'll generate random small integer vectors and brute-force BFS to confirm condition.
Let's do a mental brute-force for small coordinates (like -2 to 2) to test. But I'll trust.
Given that the solution is quite standard, I'll proceed.
But before finalizing, consider if there is any simplification: The condition for each t can be combined into checking if there exists t such that two linear equations hold modulo something? Possibly we can solve using gcd, but our brute-force over 4 rotations is fine.
Now, time complexity O(1).
Now, corner case: When C is non-zero, but denom might be large, but fine.
Now, let's also think about the possibility of overflow in C++ when computing denom as long long, but fine.
Now, what about integer division: In C++, we need to ensure num1 % denom works for negative numbers; but as said, it's fine.
Alternatively, we can check if num1 * 1.0 / denom is integer, but better to use modulo.
Now, finalize.
But I recall a Codeforces problem "Vectors" (maybe 73A?) that had similar operations. The solution was to check if (B - A) can be expressed as combination of C and a perpendicular vector, and also check rotations. Actually the condition might be that (B - A) dot something and cross something are divisible by norm squared, etc.
Alright.
Now, let's produce the answer.
But before we output, note that in the problem statement, they use "YES" and "NO" uppercase.
One more check: In the input examples, the third line is vector C. Example 2: Input:
0 0
1 1
1 1
Output YES.
Our check gives YES.
All good.
Now, I'll implement in Python for simplicity.
But the problem might be from a contest where C++ is expected. But we can produce solution in any language; the user asks for solution (likely they want explanation). But as ChatGPT, we need to output final answer? The user likely wants the solution code and explanation. Since the instruction says "Print ...", but the conversation is about solving the problem. Usually we provide reasoning and then code. So I'll provide a full solution in Python, with explanation.
But also maybe they expect C++ code. I can provide both. But as the answer, I'll give a Python solution, as it's concise and easy to verify.
Now, ensure we handle large ints in Python automatically.
Python code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
x1, y1, x2, y2, x3, y3 = map(int, data)
A = (x1, y1)
B = (x2, y2)
C = (x3, y3)
# define rotations
rots = [(x1, y1), (y1, -x1), (-x1, -y1), (-y1, x1)]
for (ax, ay) in rots:
dx = x2 - ax
dy = y2 - ay
if x3 == 0 and y3 == 0:
if dx == 0 and dy == 0:
print("YES")
return
else:
denom = x3x3 + y3y3
num1 = x3dx + y3dy
num2 = y3dx - x3dy
if num1 % denom == 0 and num2 % denom == 0:
print("YES")
return
print("NO")
if name == "main":
solve()
But careful: In Python, the modulo with negative numbers yields a non-negative remainder? Actually Python's % returns a remainder with the sign of the divisor, which is positive if denom >0. So -8 % 4 = 0. So fine.
Now, note: We might need to handle case where C is non-zero but denom is large; Python handles big ints.
Now, double-check rotation: (y1, -x1) is correct for clockwise.
Now, possible pitfalls: The input lines might have extra spaces, but split handles.
Now, let's test some random cases quickly with mental brute-force.
Case: A=(1,2), B=(3,4), C=(5,6). We can test with code later, but not necessary.
Now, one more nuance: Could there be a scenario where the operations can be applied infinitely, but we might need to use an infinite number to converge? No, we need exact B, finite steps.
Thus final answer.
But wait, is there any possibility that we need to consider the possibility of using zero additions but multiple rotations to get B? That is covered by t rotations and D=0.
Thus final solution.
Now, maybe there is an even simpler condition: The problem reduces to checking whether (B - A) is in the lattice generated by C and a perpendicular vector, but also considering rotations of A. So we need to try all four.
Now, I'll produce the final answer with explanation and code.
But the user asked: "Can Gerald cope with the task?" So we need to output "YES" or "NO". So the code will do that.
Now, I'll provide the final answer in a typical format.
Given the conversation, I'm ChatGPT, I need to output the solution.
I'll output a thorough explanation and then the code.
But maybe they want only code. I'll include both.
Now, to be safe, I'll also mention the reasoning.
Thus final. The problem asks whether we can transform vector (A) into vector (B) using only two operations:
Rotate the current vector by 90° clockwise.
Add the fixed vector (C).
Operations can be performed any number of times in any order.
After analyzing the process, the set of reachable vectors from (A) is exactly
where (R) denotes clockwise rotation ((R(x,y) = (y,-x))).
Because the four vectors (C, R(C), R^2(C), R^3(C)) together are closed under sign, the semigroup they generate is actually the integer lattice
If (C = (0,0)), only rotations are available, so we simply check whether (B) equals one of the four rotations of (A).
The algorithm tries all four rotations of (A) and checks the conditions.
All numbers fit comfortably in 64‑bit integers, and the check runs in constant time.
python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
x1, y1, x2, y2, x3, y3 = map(int, data)
# the four clockwise rotations of A
rotations = [
( x1, y1), # 0°
( y1, -x1), # 90°
(-x1, -y1), # 180°
(-y1, x1) # 270°
]
for ax, ay in rotations:
dx = x2 - ax
dy = y2 - ay
if x3 == 0 and y3 == 0:
if dx == 0 and dy == 0:
print("YES")
return
else:
denom = x3 * x3 + y3 * y3 # |C|² > 0
num1 = x3 * dx + y3 * dy # C·D
num2 = y3 * dx - x3 * dy # cross-like term
if num1 % denom == 0 and num2 % denom == 0:
print("YES")
return
print("NO")
if __name__ == "__main__":
solve()