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.
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.
If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (without quotes).
1 7 3
YES
10 10 0
YES
1 -4 5
NO
0 60 50
NO
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.
题意:给你一个首项为 a ,公差为 c 的等差数列, 问是否存在 b。
代码如下:
法一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
int a, b, c, v;
while (~scanf("%d%d%d", &a, &b, &c)){
if ((a > b&&c>=0) || (a < b && c<=0)){
puts("NO"); continue;
}
if (a == b){
puts("YES"); continue;
}
if (c == 0){
if (a == b) {
puts("YES"); continue;
}
else {
puts("NO"); continue;
}
}
else {
if (abs(b-a)%abs(c) == 0)
puts("YES");
else
puts("NO");
}
}
return 0;
}
法二:(某大神代码)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
int main()
{
#ifdef OFFLINE
freopen("t.txt", "r", stdin);
#endif
int a, b, c, v;
while (~scanf("%d%d%d", &a, &b, &c)){
v=b-a;
bool ret;
if(v==0) ret=true;
else if(c==0) ret=false;
else if(v/c<0) ret=false;//v, c异号则不行
else ret=(v%c==0)?true:false;
printf("%s\n",(ret?"YES":"NO"));
}
return 0;
}