Problem:


Score : 400 points

Problem Statement

There are N squares in a row.
The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).

As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares.
Determine whether it is possible to fill the squares under the condition.

Constraints

3 \leq N \leq 500000

0 \leq A \leq 10^9

0 \leq B \leq 10^9

0 \leq C \leq D \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B C D

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.

Sample Input 1

5 1 5 2 4

Sample Output 1

YES

For example, fill the squares with the following integers: 1, -1, 3, 7, 5, from left to right.

Sample Input 2

4 7 6 4 5

Sample Output 2

NO

Sample Input 3

48792 105960835 681218449 90629745 90632170

Sample Output 3

NO

Sample Input 4

491995 412925347 825318103 59999126 59999339

Sample Output 4

YES



Sample input:
5 1 5 2 4


Sample output:
YES

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N, A, B, C, D = ILI()
    return N, A, B, C, D


def solve(N, A, B, C, D):
    dif_ab = abs(B - A)
    dif_cd = D - C
    n_dif = N - 1

    d_div, d_mod = divmod(dif_ab, D)

    if d_div > n_dif:
        return "NO"
    else:
        n_dif_rest = n_dif - d_div
        dif_rest = d_mod
        if n_dif_rest == 0:
            if d_mod == 0:
                return "YES"
            else:
                return "NO"
        else:
            if d_mod >= C:
                dif_rest -= C
                n_dif_rest -= 1

            if n_dif_rest % 2 == 1:
                dif_rest += C
                n_dif_rest -= 1

            if dif_rest <= n_dif_rest * dif_cd:
                return "YES"

    return "NO"


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
if B<A:
	A,B=B,A
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    assert False
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (B-A)%D==0 and abs(B-A)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        C//=g
        D//=g
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 18 and D > 1:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % (C + D) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	if (abs(B % (C + D) - (D - C) / 2) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (D - C)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
import numpy as np
hoge = input()
N,A,B,C,D = list(map(int,hoge.split(" ")))
k = np.abs(B-A)//D
l = np.abs(B-A)%D
times = k + 2 * np.ceil (l/ (D-C))
if times > N-2:
    print("NO")
else:
    print("YES")

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
if B<A:
	A,B=B,A
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    assert False
    if D==0:
        C,D=D,C
    if (B-A)%D==0 and abs(B-A)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        C//=g
        D//=g
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D=map(int,input().split())
D-=1
l=[]
for i in range(0,N+1):
    l.append(range(i*C-(N-i)*D,i*D-(N-i)*C+1))

for _ in l:
    if B-A in _:
        print("YES")
        break
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
if B<A:
	A,B=B,A
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (B-A)%D==0 and abs(B-A)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        C//=g
        D//=g
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
N, A, B, C, D = list(map(int, input().split()))

add = abs(B - A)
delta = (N - 1) * (D - C)
center = (D + C)/2
#print("add", add)
#print("delta", delta)
#print("center", center)
if N % 2 == 0:
    need = (add + center) // (D + C) * 2 + 1
else:
    need = add // (D + C) * 2
#print("need", need)

if need > N - 1:
    print("NO")
elif abs(need * center - add) <= delta:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
def f():
    n, a, b, c, d = list(map(int, input().split(" ")))
    # c <= x <= d
    n-=1
    
    if abs(b-a)/(n) > d:
        print("NO")
        return
    
    buf = abs(d-c)
    bfcnt = 0
    tikai = abs(b-a)
    if tikai > d :
        bfcnt = tikai //d 
        tikai = tikai-d*bfcnt
    if (n-bfcnt) == 1:
        if tikai-d <= 0 <= tikai-c:
            print("YES")
            return
    if (n-bfcnt)%2 == 1:
        bfcnt += 1
        tikai -= d 
    
    if abs(tikai) <= (d-c)*(n//2):
        print("YES")
    else:
        print("NO")

f()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (A-B)%D==0 and abs(A-B)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        C//=g
        D//=g
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
# Modelrate Difference

N,A,B,C,D = list(map(int, input().split()))

ans = "NO"
for i in range(N//2):
    t = N%2
    k = (i + t)*2 + 1 - t
    if ( (k*C<=abs(A-B)<=k*D) or ( max(0, ((k+1)/2 )*C - ((k-1)/2 )*D)<= abs(A-B) ) ):
        ans="YES"
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (A-B)%D==0 and abs(A-B)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        C/=g
        D/=g
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
def f(mi, ma, x):
    return x >= mi and x <= ma 

n, a, b, c, d = map(int, input().split())
min = b
max = b
for i in range(n - 2):
   max += d
   min -= d
if f(min, max, a+c) or f(min, max, a-c) or f(min, max, a+d) or f(min, max, a+d):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
def f(mi, ma, x):
    return x >= mi and x <= ma 

n, a, b, c, d = map(int, input().split())
min = b
max = b
for i in range(n - 12):
   max += d
   min -= d
if f(min, max, a+c) or f(min, max, a-c) or f(min, max, a+d) or f(min, max, a+d):
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D=map(int,input().split())
l=[]
for i in range(1,N):
    l.append(range(i*C-(N-i)*D,i*D-(N-i)*C+1,2))
l.append(range(-N*D,-N*C+1))
l.append(range(N*C,N*D+1))
for _ in l:
    if B-A in _:
        print("YES")
        break
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 18 and D > 1:
	flag = 10 **9 + B
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % ((C + D) * 2) <= (N - 1) * (D - C) / 2 or abs(B % ((C + D)* 2) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16 and D > 1:
	flag = 10 **9 + B
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % ((C + D) * 2) <= (N - 1) * (D - C) / 2 or abs(B % ((C + D)* 2) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
# Model Difference

N,A,B,C,D = list(map(int, input().split()))

ans = "NO"
for i in range(N//2):
    k=i*2 +1
    if ( (k*C<=abs(A-B)<=k*D) or ( max(0, ((k+1)/2 )*C - ((k-1)/2 )*D)<= abs(A-B) ) ):
        ans="YES"
print(ans)

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16 and D > 1:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % ((C + D) * 2) <= (N - 1) * (D - C) / 2 or abs(B % ((C + D)* 2) - (C + D) *2) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import itertools

N,A,B,C,D = map(int, sys.stdin.readline().rstrip().split())


# diff = D-C
# 1 : [D-diff, D]
# 2 : [-diff, diff], [2D-2diff, 2D]
# 3 : [D-2diff, D+diff], [3D-3diff, 3D]
# 4 : [-2diff, 2diff] [2D-3diff, 2D+diff], [4D-4diff, 4D]
# 5 : [D-3diff, D+2diff] [3D-4diff, 3D+diff], [5D-5diff, 5D]


diff = D-C
target_diff = abs(A-B)
odd_or_evens = (N-1) % 2
upper_raw_count = (target_diff // D + 1)
upper_count = (upper_raw_count + (upper_raw_count + odd_or_evens) % 2)
upper = upper_count * D
lower_raw_count = (target_diff // D)
lower_count = (lower_raw_count - (lower_raw_count + odd_or_evens) % 2 )
lower = lower_count * D

#print("target = {}".format(target_diff))
#print("raw_lower {}*{}={}".format(D, lower_raw_count, D*lower_raw_count))
#print("raw_upper {}*{}={}".format(D, upper_raw_count, D*upper_raw_count))
#print(diff)
#print("{} < {} < {}".format(lower, target_diff, upper))


if target_diff > (N-2) * D:
    print("NO")
else:
    #print((upper - target_diff) // diff)
    #print("{} < {} < {}".format(upper - upper_count * diff, target_diff, upper))
    #print((target_diff - lower) // diff)
    #print("{} < {} < {}".format(lower, target_diff, lower + ((N-1-lower_count) // 2) * diff))
    if (
            (upper - upper_count * diff) <= target_diff
    ) or (
        (lower + ((N-1-lower_count) // 2) * diff) >= target_diff):
        print("YES")
    else:
        print("NO")

exit(0)

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16 and D > 1:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % ((C + D) * 2) <= (N - 1) * (D - C) / 2 or abs(B % ((C + D)* 2) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D=map(int,input().split())
l=[]
for i in range(N+1):
    l.append(range(i*C-(N-i)*D,int(i*D-(N-i)*C/2)*2+2,2))
for _ in l:
    if B-A in _:
        print("YES")
        break
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16 and D > 1:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % (C + D) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D=map(int,input().split())
l=[]
for i in range(N+1):
    l.append(range(i*C-(N-i)*D,i*D-(N-i)*C+2,2))
for _ in l:
    if B-A in _:
        print("YES")
        break
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if (B % (C + D) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (C + D)) <= (N - 1) * (D - C) / 2) and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16:
	flag = 10 **9
else:
	flag = pow(D,N)
flag -= A
B -= A
if N % 2 == 1:
	if B % (C + D) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (C + D)) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D = map(int,input().split())
if N > 16:
	flag = 10 **9
else:
	flag = pow(D,N)
B -= A
if N % 2 == 1:
	if B % (C + D) <= (N - 1) * (D - C) / 2 or abs(B % (C + D) - (C + D)) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")
else:
	m = B % ((C + D) * 2) - (D - C) / 2
	if abs(m) <= (N - 1) * (D - C) / 2 and B <= flag and B >= -flag:
		print("YES")
	else:
		print("NO")

Submission status: Wrong Answer

Solution code:
import math

n, a, b, c, d = map(int, input().split())
if a > b:
    a,b = b,a

# if c <= (b-a) / (n-1) <= d:
#     print('YES')
# else:
#     print('NO')

# points = [
#     ((-a+b+c*(n-1))/(c+d), (-a+b+c*(n-1))*d/(c+d) + a),
#     ((-a+b+d*(n-1))/(2*d), (a+b+d*(n-1))/2),
#     ((-a+b+c*(n-1))/(2*c), (a+b+c*(n-1))/2),
#     ((-a+b+d*(n-1))/(c+d), (-a+b+d*(n-1))*c/(c+d) + a)
# ]

# def sub_vector(a, b):
#     return [a[i] - b[i] for i in range(len(a))]

# def f(a, b):
#     return a[0] * b[1] + a[1] * b[0]
    
# def is_contained(a, b, c, p):
#     ab = sub_vector(b, a)
#     bp = sub_vector(p, b)
#     bc = sub_vector(c, b)
#     cp = sub_vector(p, c)
#     ca = sub_vector(a, c)
#     ap = sub_vector(p, a)
#     c1 = f(ab, bp)
#     c2 = f(bc, cp)
#     c3 = f(ca, ap)
#     return (c1>=0 and c2>=0 and c3>=0) or (c1<=0 and c2<=0 and c3<=0)

# flag = False

# cands = [
#     [math.ceil(points[0][0]), math.ceil(points[0][1])],
#     [math.ceil(points[0][0]), math.floor(points[0][1])],
#     [math.ceil(points[1][0]), math.floor(points[1][1])],
#     [math.floor(points[1][0]), math.floor(points[1][1])],
#     [math.ceil(points[2][0]), math.ceil(points[2][1])],
#     [math.floor(points[2][0]), math.ceil(points[2][1])],
#     [math.floor(points[3][0]), math.ceil(points[3][1])],
#     [math.floor(points[3][0]), math.floor(points[3][1])],
# ]

# for p in cands:
#     if is_contained(points[0], points[1], points[2], p):
#         flag = True
#     if is_contained(points[3], points[1], points[2], p):
#         flag = True
def range_and(a, b):
    return  b[0] <= a[0] <= b[1] or b[0] <= a[1] <= b[1]
flag = False

m = (n-1) // 2
cand = [
    [a + 2 * m * d, a + 2 * m * c],
    [a + m * (d - c), a + m * (c - d)],
    [a - 2 * m * c, a - 2 * m * d]
]


if n%2 == 1:
    for u,l in cand:
        if l <= b <= u:
            flag = True
            break
else:
    for l,u in cand:
        if range_and((l, u), (b-d, b-c)):
            flag = True
        if range_and((l, u), (b+c, b+d)):
            flag = True

nn = (b - a) // d
n -= nn
b -= d * nn
m = (n-1) // 2
cand = [
    [a + 2 * m * d, a + 2 * m * c],
    [a + m * (d - c), a + m * (c - d)],
    [a - 2 * m * c, a - 2 * m * d]
]


if n%2 == 1:
    for u,l in cand:
        if l <= b <= u:
            flag = True
            break
else:
    for l,u in cand:
        if range_and((l, u), (b-d, b-c)):
            flag = True
        if range_and((l, u), (b+c, b+d)):
            flag = True

print('YES' if flag else 'NO')

Submission status: Wrong Answer

Solution code:
def main():
  N,A,B,C,D = map(int,input().split())
  n = (N-1)//2
  if N%2 == 1:
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D) and B <= A2+i*(D-C):
        print('YES')
        return
      A1 += 2*C
      A2 += 2*D
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D) and B <= A2+i*(D-C):
        print('YES')
        return
      A1 -= 2*D
      A2 -= 2*C
    print('NO')
  else:
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D)+C and B <= A2+i*(D-C)+D:
        print('YES')
        break
      A1 += 2*C
      A2 += 2*D
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D)+C and B <= A2+i*(D-C)+D:
        print('YES')
        return
      A1 -= 2*D
      A2 -= 2*C
    print('NO')

main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    N, A, B, C, D = map(int, raw_input().split()) 
    A, B = 0, B - A
    if B < 0: B *= -1
    M = N - 1

    for i in xrange(0, N + 1, 2):
        if C*(M - i) <= B <= (D - C)*(i/2) + D*(M - i):
            return True

    return False

print "YES" if solve() else "NO"

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, A, B, C, D = list(map(int, input().split()))
    if C+D == 0:
        if A == B :
            print("YES")
        else :
            print("NO")
    else:
        average_c_d = (C + D) / 2

        range_c_d = (D - C) / 2

        ul = A - average_c_d * (N - 1)

        target_index = round((B - ul) / (C + D))

        under = ul + (C + D) * target_index - range_c_d * (N - 1)
        upper = ul + (C + D) * target_index + range_c_d * (N - 1)

        if under <= B <= upper:
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
N,A,B,C,D=map(int,input().split())
l=[]
for i in range(N+1):
    l.append(range(i*C-(N-i)*D,i*D-(N-i)*C+1,2))
for _ in l:
    if B-A in _:
        print("YES")
        break
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    N, A, B, C, D = map(int, raw_input().split()) 
    A, B = 0, B - A
    if B < 0: B *= -1
    M = N - 1

    if C*M <= B <= D*M:
        return True

    for i in xrange(1, N - 1, 2):
        if C*(M - i - 1) <= B <= (D - C)*i + D*(M - i - 1):
            #print C*(M - i - 1), B, (D - C)*i + D*(M - i - 1)
            return True

    return False

print "YES" if solve() else "NO"

Submission status: Wrong Answer

Solution code:
n , a, b, c, d = map(int, input().split())
min = b
max = b
for i in range(n - 2):
   max += d
   min -= d
if a >= min and a <= max:
    print("YES")
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
# Model Difference

N,A,B,C,D = list(map(int, input().split()))

ans = "NO"
for i in range(N-1):
    if ( (i*C<=abs(A-B)<=i*D  or abs(A-B) >= i*(D-C))  and (N-1-i)%2==0 ):
        ans="YES"
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  N,A,B,C,D = map(int,input().split())
  n = (N-1)//2
  if N%2 == 1:
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D) and B <= A2+i*(D-C):
        print('YES')
        break
      A1 += 2*C
      A2 += 2*D
    else:
      print('NO')
  else:
    A1 = A
    A2 = A
    for i in range(n,-1,-1):
      if B >= A1+i*(C-D)+C and B <= A2+i*(D-C)+D:
        print('YES')
        break
      A1 += 2*C
      A2 += 2*D
    else:
      print('NO')

main()

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N, A, B, C, D = list(map(int, input().split()))

    average_c_d = (C + D) / 2

    range_c_d = (D - C) / 2

    ul = A - average_c_d * (N - 1)

    target_index = round((B - ul) / (C + D))

    under = ul + (C + D) * target_index - range_c_d * (N - 1)
    upper = ul + (C + D) * target_index + range_c_d * (N - 1)

    if under <= B <= upper:
        print("YES")
    else:
        print("NO")

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (A-B)%D==0 and abs(A-B)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*C))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (A-B)%D==0 and abs(A-B)//D<N and (abs(A-B)//D)%2!=N%2:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        alphamin=-10**100
        alphamax=10**100
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=list(range(-50,50))
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*D))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

def solve():
    N, A, B, C, D = map(int, raw_input().split()) 
    A, B = 0, B - A
    if B < 0: B *= -1
    M = N - 1

    for i in xrange(0, N, 2):
        if C*(M - i) <= B <= (D - C)*i + D*(M - i):
            return True

    for i in xrange(1, N - 1, 2):
        if C*(M - i - 1) <= B <= (D - C)*i + D*(M - i - 1):
            return True

    return False

print "YES" if solve() else "NO"

Submission status: Wrong Answer

Solution code:
# Model Difference

N,A,B,C,D = list(map(int, input().split()))

ans = "NO"
for i in range(N-1):
    if ( (i*C<=abs(A-B)<=i*D)  and (N-1-i)%2==0 ):
        ans="YES"
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
N,A,B,C,D=map(int,input().split())
def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, x, y = egcd(b % a, a)
        return (g, y - (b // a) * x, x)
if C==0 and D==0:
    if A==B:
        print("YES")
    else:
        print("NO")
elif C==0 or D==0:
    if D==0:
        C,D=D,C
    if (A-B)%D==0 and abs(A-B)//D<N:
        print("YES")
    else:
        print("NO")
else:
    if abs(B-A)%gcd(C,D):
        print("NO")
    else:
        (g,x,y)=egcd(C,D)
        times=(B-A)//g
        #xC+yD=B-A
        x*=times
        y*=times
#        x+=D*alpha
#        y-=C*alpha
        alphamin=-10**36
        alphamax=10**36
        while alphamax-alphamin>1:
            mid=alphamin+(alphamax-alphamin)//2
            if abs(x+mid*D)+abs(y-mid*C)>abs(x+mid*D+D)+abs(y-mid*C-C):
                alphamin=mid
            else:
                alphamax=mid
        x=x+alphamax*D
        y=y-alphamax*C
        a=[-1,0,1]
        if any(abs(x+i*D)+abs(y-C*i)<N and (abs(x+i*D)+abs(y-i*D))%2!=N%2 for i in a):
            print("YES")
        else:
            print("NO")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  9 18:12:12 2017

@author: abhay
"""

def check(diff, small, large, N, A, B, smallMultiplier, largeMultiplier):
    if diff >= smallMultiplier * small and diff <= largeMultiplier * large:
        return True, True
    elif diff > largeMultiplier * large:
        return True, False          
    else:
        return False, False

inputList = [int(x) for x in raw_input().split(" ")]
N = inputList[0]
A = inputList[1]
B = inputList[2]
C = inputList[3]
D = inputList[4]

large = max(C, D)
small = min(C, D)

diff = abs(A - B)

shouldFinish, conditionsMet = check(diff, small, large, N, A, B, (N-2), (N-1))

if shouldFinish:
    if conditionsMet:
        print "YES"
    else:
        print "NO"
else:
    start = min(A, B) - large
    end = max(A, B) + large
    newDiff = abs(start - end)
    fin, yes =  check(newDiff, small, large, N, start, end, N - 2, N - 2)    
    if yes:
        print "YES"
    else:
        print "NO"

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import itertools

N,A,B,C,D = map(int, sys.stdin.readline().rstrip().split())


# diff = D-C
# 1 : [D-diff, D]
# 2 : [-2diff, diff], [2D-2diff, 2D]
# 3 : [D-3diff, D+diff], [3D-3diff, 3D]
# 4 : [-4diff, 2diff] [2D-4diff, 2D+2diff], [4D-4diff, 4D]


diff = D-C
target_diff = abs(A-B)
odd_or_evens = N % 2
upper_raw_count = (target_diff // D + 1)
upper = (upper_raw_count + (upper_raw_count + odd_or_evens) % 2) * D
lower_raw_count = (target_diff // D)
lower = (lower_raw_count - (lower_raw_count + odd_or_evens) % 2 ) * D

#print("{} < {} < {}".format(lower, target_diff, upper))


if target_diff > (N-2) * D:
    print("NO")
else:
    #print("{} < {} < {}".format(lower, target_diff, ))
    #print("{} < {} < {}".format(lower, target_diff, upper))
    if ((upper - (N-2) * diff) < target_diff) or ((lower + ((N-2) // 2) * diff) > target_diff):
        print("YES")
    else:
        print("NO")

exit(0)