Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.
Input
The first line of the input contain three integers a, b and c ( - 109 ≤ a, b, c ≤ 109) — the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.
Output
If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes).
Examples
Input
1 7 3
Output
YES
Input
10 10 0
Output
YES
Input
1 -4 5
Output
NO
Input
0 60 50
Output
NO
Note
In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.
In the second sample, the favorite integer of Vasya is equal to the first element of the sequence.
In the third sample all elements of the sequence are greater than Vasya's favorite integer.
In the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer.
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 need to determine if integer b is in the arithmetic progression: a, a + c, a + 2c, ... (with index starting at 1). The condition: There exists positive integer i such that a + (i-1)*c = b. This is equivalent to b - a = (i-1)*c for some nonnegative integer i-1 (i >= 1, i integer, so i-1 >= 0). Let d = b - a.
We need to check if there exists integer k >= 0 such that d = k * c. If c == 0, then the sequence is constant: all terms are a. So b is in the sequence iff b == a. Also need to consider i positive integer, so if a == b and c=0, it's fine (k=0 works). If c == 0 and a != b, then NO.
If c != 0, then we need d / c to be integer >= 0, and also note that if c divides d, we need that the quotient is nonnegative.
Edge cases: negative numbers allowed. So c could be positive or negative. The sequence can go in either direction. But i must be positive integer (i >= 1). So k = i-1 >= 0. So we need d and c such that d/c is a nonnegative integer.
Thus algorithm: if c == 0: print "YES" if a == b else "NO". else: if (b - a) % c == 0 then check quotient >= 0: if ((b - a) / c) >= 0 then YES else NO. However careful with negative modulo in programming languages: In C++/Java etc, remainder can be negative. Better to check: (b - a) % c == 0? But if c is negative, modulus may be tricky. Safer: if c != 0, then we check if (b - a) is exactly divisible by c, i.e., the remainder when dividing (b - a) by c is 0. In many languages, we can check (b - a) % c == 0 only if c > 0; but if c negative, modulus might not be zero because sign of divisor matters. To avoid issues, we can check that c divides (b - a) using: (c != 0) && ((b - a) % c == 0) works in C++ if both are positive? Actually, C++ remainder can be negative but if both numbers have same sign? Better to check: (b - a) % c == 0 works if c divides d exactly, i.e., d mod c = 0. In C++, for integers a % b, the sign of the result is implementation-defined pre-C++11? Actually C++11 standardized that the remainder has the same sign as the dividend. So -7 % 3 = -1, not 2. So it's not safe to check remainder == 0 because 0 is unaffected. If d % c == 0, then it is divisible regardless of signs? Let's test: d=6, c=3 -> 6%3=0. d=6, c=-3 -> 6%(-3)=0. d=-6, c=3 -> (-6)%3=0? In C++11, (-6)%3 = 0? Actually, -6 / 3 = -2 with remainder 0, so yes 0. d=-6, c=-3 -> (-6)%(-3)=0. So if it's exactly divisible, remainder is 0 irrespective of signs. So we can safely use (d % c == 0). The only issue is if c=0 (division by zero) so we handle that separately.
Now check quotient >= 0: we need k >= 0. Since k = d / c. We can compute if (d / c >= 0). However, we should be careful with integer division sign: In C++, division truncates towards zero. So if d and c have opposite signs, d/c is negative. So we can compute k = d / c and check k >= 0. But if d and c are both negative, the quotient is positive (since negative/negative=positive). So we need to compute k = (b - a) / c and check k >= 0. However, we must ensure that d % c == 0 before division, to guarantee integer quotient. But we could also check: (c > 0 && d >= 0) or (c < 0 && d <= 0) and divisibility? But easier: compute d = b - a; if c != 0 && d % c == 0) { if d / c >= 0 then YES else NO }. That should work. However, if c is negative and d is positive, then d % c == 0 could still hold if d is multiple of c (e.g., d = 6, c = -2 -> 6 % -2 = 0; quotient = -3 (negative). That would mean that b = a + (-3)c? Wait: if c = -2, then sequence: a, a-2, a-4, ... So b = a + kc. If c negative, then b is less than a if k>0. If b is greater than a, then to have b in sequence, c must be positive (or zero) else you need negative k? But k must be >=0. So if c negative and d positive (b > a), then k = d / c would be negative, so not allowed. So our check works.
Edge cases: huge numbers, but within int range, up to 1e9, and differences up to 2e9, but we use 64-bit (long long) to avoid overflow when computing d and multiplication? Actually we don't multiply; we compute d = b - a which could be up to 2e9 (difference of two up to 1e9), fits in 32-bit signed? Actually 1e9 - (-1e9) = 2e9, which fits in 32-bit signed int (max 2,147,483,647). But if we multiply something? We might do division, but no multiplication. So 32-bit int is enough. However, to be safe, we can use long long.
Another approach: check if c == 0: a == b; else if (b - a) and c have same sign (or one is zero) and (b - a) % c == 0, then YES. But using division works.
a=10,b=20,c=-5: d=10, c=-5, d%c=0? 10%(-5)=0, d/c = -2 <0 => NO, because sequence goes down: 10,5,0,... never reaches 20. So correct.
Now consider case where c != 0 and d=0: then d%c=0 (since 0%anything=0). d/c = 0 >=0 => YES (b equals a). So that's covered.
Thus solution is straightforward.
One more nuance: For c != 0, we need to check that d % c == 0. But if c is negative, some languages might give remainder negative? But remainder of 0 is 0. For non-zero d, if d is divisible by c, remainder is 0. So it's fine.
Implement in C++:
code
#include <iostream>
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
if (c == 0) {
if (a == b) cout << "YES";
else cout << "NO";
} else {
if ((b - a) % c == 0 && (b - a) / c >= 0) cout << "YES";
else cout << "NO";
}
return 0;
}
That's it.
But we must consider overflow? b - a could be as large as 2e9, which fits in long long. Even if we used int, it's safe. But to be safe, use long long.
Time O(1).
Now I'll write explanation.
Edge Cases: Ensure positive integer i, i >= 1 => k >= 0.
Now done. ```cpp
#include
using namespace std;
int main() {
long long a, b, c;
cin >> a >> b >> c;
code
if (c == 0) {
// Sequence is constant: a, a, a, ...
if (a == b) cout << "YES";
else cout << "NO";
} else {
long long d = b - a;
// Check divisibility and non-negative index
if (d % c == 0 && d / c >= 0) {
cout << "YES";
} else {
cout << "NO";
}
}
return 0;