Codeforces Round #353 (Div. 2) A. Infinite Sequence Thinking problem

Source: Internet
Author: User

A. Infinite sequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Vasya likes everything infinite. Now he is studying the properties of a sequences, such that it first element is equal toa( s1? =? a ), and the difference between any and neighbouring elements are equal toC( si?-? s i?-? 1? =? C ). In particular, Vasya wonders if his favourite integerbAppears in this sequence, which is, there exists a positive integerI, such that si? =? b . Of course, you is the person who he asks for a help.

Input

The first line of the input contain three integersa,bandC(?-?9? ≤? a,? b,? c. ≤?109 )-the first element of the sequence, Vasya ' favorite number and the difference between any of neighbouring elements of The sequence, respectively.

Output

If b appears in the sequence s print "YES" (without quotes), otherwise print "NO" (Wit Hout quotes).

Examplesinput
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, and so 7 are its element.

In the second sample, the favorite an integer of Vasya is equal to the first element of the sequence.

In the third sample all elements of the sequence is greater than Vasya ' s favorite integer.

In the fourth sample, the sequence starts from 0, +, and all the following elements is G Reater than Vasya ' s favorite integer.


Source

A. Infinite Sequence


My Solution

by Cha, (┬_┬), the reason is to use (b-a) * C >= (b-a) with the same number of C, or b-a = = 0. here int * int overflow, later still basically do not, write.

Honest ((t>=0&&c > 0) | | (t<=0 && C < 0)) Write it like this.

If c = = 0, then a = = B Yes otherwise NO

If c! = 0, then see if it can be divisible, and (b-a) the same number as C (if B is not equal to a)

#include <iostream> #include <cstdio>using namespace Std;int main () {    int A, b, C;    scanf ("%d%d%d", &a, &b, &c);    int t = b-a;    if (c = = 0) {        if (a = = b) printf ("YES");        else printf ("NO");    }    else{        //if (t%c = = 0 && (t*c >= 0)) printf ("YES"),//!t*c overflow        if (t%c = = 0 && ((t>=0&& C > 0) | | (t<=0 && C < 0))) printf ("YES");        else printf ("NO");    }    return 0;}


Thank you!

------ from Prolights

Codeforces Round #353 (Div. 2) A. Infinite Sequence Thinking problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.