Codeforces Round #353 (Div. 2) 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 (?-?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.

Source

A. Infinite Sequence

My Solution

被cha了,(┬_┬), 原因是用 (b - a) * c >= 0 来表示(b-a) 与 c同号, 或b - a == 0.这里int * int 溢出了,以后还是基本上不要这样,写了。

老老实实的 ((t>=0&&c > 0)|| (t<=0 && c < 0)) 这样写吧。

如果 c == 0, 则 a == b 则YES 否则 NO

如果c != 0, 则看看能不能整除, 同时 (b-a) 要与c同号(如果 b与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 溢出了
        if(t%c == 0 && ((t>=0&&c > 0)|| (t<=0 && c < 0))) printf("YES");
        else printf("NO");
    }
    return 0;
}

Thank you!

------from ProLights

时间: 2024-10-09 01:31:08

Codeforces Round #353 (Div. 2) A. Infinite Sequence 思维题的相关文章

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

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 won

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #260 (Div. 2) A. Laptops(简单题)

题目链接:http://codeforces.com/problemset/problem/456/A A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Dima and Alex had an argument about the price and quality of laptops.

Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load 题意: 给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小. 题解: 显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好. 然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了. 最大距离的最小值就是离根最远的和最近的加一加就行了. 1 #include<cstdio> 2 #define F(i,a,b) for

Codeforces Round #301 (Div. 2) E. Infinite Inversions —— 逆序对 离散化 + 树状数组

题目链接:http://codeforces.com/contest/540/problem/E E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is an infinite sequence consisting of all positive integers in

Codeforces Round #353 (Div. 2) ABCDE 题解 python

Problems # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring Painting standard input/output 1 s, 256 MB    x2519 C Money Transfers standard input/output 1 s, 256 MB    x724 D Tree Construction standard input/output 2

Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input  output standard output There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We pe

Codeforces Round #629 (Div. 3) D. Carousel(思维/贪心?)

The round carousel consists of nn figures of animals. Figures are numbered from 11 to nn in order of the carousel moving. Thus, after the nn -th figure the figure with the number 11 follows. Each figure has its own type — the type of the animal corre

Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)

链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to 1. More formally, a sequence s1,s2,-,sn is beautiful if |si?si+1|=1 for all 1≤i≤n?1. Trans