codeforces 675A A. Infinite Sequence(水题)

题目链接:

A. Infinite Sequence

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 bappears 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 ab and c ( - 10^9 ≤ a, b, c ≤ 10^9) — 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.

题意:

给数列的首项,再给出数列的相邻的差,给出一个数问是否出现在这个数列中;

思路:

简单的分情况讨论了;

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e4+25;
int n;
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(c==0){if(a==b)printf("YES
");
    else printf("NO
");}
    else if(c>0)
    {
    if((b-a)%c==0&&b>=a)printf("YES
");
    else printf("NO
");
    }
    else
    {
        c=-c;
        if((a-b)%c==0&&b<=a)printf("YES
");
        else printf("NO
");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5502133.html