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

Solution (Python):
from bisect import bisect_left
N, A, B, C, D = map(int, input().split())
A, B, C, D = A*2, B*2, C*2, D*2
M = C + (D - C) // 2
W = (D - C) // 2
arr = list(range(-(N-1)*M+A, N*M+A, C+D))





    

idx = bisect_left(arr, B)
ans = 'NO'
if idx > 0:
    if B - arr[idx-1] <= W*(N-1):
        ans = 'YES'
if idx < N:
    if arr[idx] - B <= W*(N-1):
        ans = 'YES'
print(ans)

Solution (Python):
N, A, B, C, D = list(map(int, input().split()))
M = N - 1
n = abs(A - B)
m = abs(C - D)

def prin(tf):
  if tf:
    print("YES")
  else:
    print("NO")
  exit()

if n > D * M:
  prin(False)

GC = n
GD = n
if M % 2 == 1:
  M -= 1
  GC = abs(n - C)
  GD = abs(n - D)
t = GC // (2 * C)
k = M // 2 - t
a = t * 2 * C
b = t * 2 * D + k * m

if b >= GD:
  prin(True)

if GD % (2 * D) == 0:
  prin(True)

t = GD // (2 * D) + 1
k = M // 2 - t
a = t * 2 * C - k * m
b = t * 2 * D


if a <= GC:
  prin(True)

prin(False)

Solution (Python):
n, a, b, c, d = map(int, input().split())
x = b - a
for p in range(n):
    m = n - p - 1
    if c * p - d * m <= x <= d * p - c * m:
        print("YES")
        exit()
print("NO")

Solution (Python):

N, A, B, C, D = map(int, input().split())
for m in range(N):
    lb = m * C - (N - 1 - m) * D
    ub = m * D - (N - 1 - m) * C
    if lb <= abs(B - A) <= ub:
        print("YES")
        exit()

print("NO")

Solution (Python):
n,a,b,c,d = map(int,input().split())
b = max(a,b)-min(a,b)
n -= 1
dif = d-c
for i in range(n%2,n+1,2):
    if c*i-dif*((n-i)//2) <= b <= d*i+dif*((n-i)//2):
        print("YES")
        exit()
print("NO")

Solution (Python):
n,a,b,c,d = map(int,input().split())
if a > b:
    a,b = b,a
b -= a
n -= 1
dif = d-c
for i in range(n%2,n+1,2):
    if c*i-dif*((n-i)//2) <= b <= d*i+dif*((n-i)//2):
        print("YES")
        exit()
print("NO")

Solution (Python):










def main():
    N, A, B, C, D = map(int, input().split())
    lower, upper = - (N - 1) * D, - (N - 1) * C
    target = B - A
    for _ in range(N):
        if lower <= target <= upper:
            print('YES')
            return
        lower += C + D
        upper += C + D
    print('NO')


if __name__ == '__main__':
    main()

Solution (Python):
N,a,b,c,d=map(int, input().split())
N-=1
a,b=0,abs(b-a)


for i in range(N//2+1):
  e=b-(N-i*2)*d
  f=b-(N-i*2)*c
  if e<=(d-c)*i and (c-d)*i<=f:
    print('YES')
    exit()
print('NO')

Solution (Python):
N,a,b,c,d=map(int, input().split())
N-=1
a,b=0,abs(b-a)

if d*N<b:
  print('NO')
  exit()
  
for i in range(N//2+1):
  e=b-(N-i*2)*d
  f=b-(N-i*2)*c
  if e<=(d-c)*i and (c-d)*i<=f:
    print('YES')
    exit()
print('NO')

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, a, b, c, d = map(int, input().split())
    diff = b - a
    for k in range(n):
        mi = k * c - (n - 1 - k) * d
        ma = k * d - (n - 1 - k) * c
        if mi <= diff <= ma:
            print("YES")
            break
    else:
        print("NO")


if __name__ == '__main__':
    resolve()

Solution (Python):
n, a, b, c, d = (int(x) for x in input().split())
flag = False
for i in range(n):
    j = n - 1 - i
    upper = a + i * d - j * c
    lower = a + i * c - j * d
    if lower <= b <= upper:
        flag = True
print("YES" if flag else "NO")

Solution (Python):
import sys
from bisect import bisect_right

def input(): return sys.stdin.readline().rstrip()

n, a, b, c, d = (int(x) for x in input().split())
A = [(a, a)]
for i in range(1, n):
    tmp = []
    for l, r in A:
        tmp.append((l + c, r + d))
        tmp.append((l - d, r - c))
    tmp.sort()
    index = bisect_right(tmp, (b, b))
    A = []
    for j in range(index - 2, index + 3):
        if 0 <= j < len(tmp):
            A.append(tmp[j])

ans = "NO"
for l, r in A:
    if l <= b <= r:
        ans = "YES"

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N, A, B, C, D = map(int, input().split())
for i in range(N - 1):
  l = C * i + A - D * (N - i - 1)
  r = D * i + A - C * (N - i - 1) + 1
  if B in range(l, r):
    print("YES")
    break
else: print("NO")

Solution (Python):
from bisect import *
N, A, B, C, D = map(int, input().split())
P = [A + (N-1-i)*D - C*i for i in reversed(range(N))]
Q = [A + (N-1-i)*C - D*i for i in reversed(range(N))]
j = bisect_left(P,B)
if j < N and B >= Q[j]:
    print("YES")
else:
    print("NO")

Solution (Python):

def bisect(ng, ok, judge, eps=1):
    while abs(ng-ok) > eps:
        m = (ng+ok)//2
        if judge(m):
            ok = m
        else:
            ng = m
    return ok

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

X = B-A

for n in range(N):
    m = N-n-1
    if n*C-m*D <= X <= n*D-m*C:
        print('YES')
        break
else:
    print('NO')

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, a, b, c, d = map(int, readline().split())
    diff = a - b

    for i in range(n - 1):
        l = i * c - (n - 1 - i) * d
        r = i * d - (n - 1 - i) * c
        if l <= diff <= r:
            return print("YES")

    print("NO")


if __name__ == '__main__':
    main()

Solution (Python):
p=print
n,a,b,c,d=map(int,input().split())
for i in range(n):
 if i*c-d*(n-i-1)<=b-a<=i*d-c*(n-i-1):exit(p("YES"))
p("NO")

Solution (Python):
def main():
    n, a, b, c, d = map(int, input().split())
    last = abs(a-b)
    if (n-1)*d < last:
        print("NO")
    else:
        f = False
        for i in range(n):
            p = d*i
            q = (n-1 - i)//2 * (d-c)
            if (n-1 - i) % 2 == 1:
                if q < c:
                    if p + c - q <= last <= p + d + q:
                        f = True
                    if p - d - q <= last <= p - c + q:
                        f = True
                else:
                    if p - d - q <= last <= p + d + q:
                        f = True
            else:
                if p - q <= last <= p + q:
                    f = True
        for i in range(n):
            p = c*i
            q = (n-1 - i)//2 * (d-c)
            if (n-1 - i) % 2 == 1:
                if q < c:
                    if p + c - q <= last <= p + d + q:
                        f = True
                    if p - d - q <= last <= p - c + q:
                        f = True
                else:
                    if p - d - q <= last <= p + d + q:
                        f = True
            else:
                if p - q <= last <= p + q:
                    f = True
        if f:
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()

Solution (Python):
import sys
readline = sys.stdin.readline

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

X = abs(B - A)
n = N - 1



if n * C <= X <= n * D:
  print("YES")
  exit(0)


for i in range(1, n):
  rest_1 = abs(C * i - X)
  rest_2 = X + C * i
  
  useful_num = n - i 
  
  if (C <= (rest_1 / useful_num) <= D) or (C <= (rest_2 / useful_num) <= D):
    print("YES")
    break
else:
  print("NO")

Solution (Python):
import sys
readline = sys.stdin.readline

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

X = abs(B - A)
n = N - 1



if n * C <= X <= n * D:
  print("YES")
  exit(0)


for i in range(1, n):
  rest_1 = abs(C * i - X)
  rest_2 = X + C * i
  
  useful_num = n - i 
  
  if (C <= (rest_1 / useful_num) <= D) or (C <= (rest_2 / useful_num) <= D):


    print("YES")
    break
else:
  print("NO")

Solution (Python):
n,a,b,c,d=map(int,input().split())

nn=n
n-=1
for i in range(nn):
  cnt=0
  while n>=0:
    l=a-n*d-cnt
    r=a-n*c+cnt
    if l<=b<=r:
      print('YES')
      exit()
    r=a+n*d+cnt
    l=a+n*c-cnt
    if l<=b<=r:
      print('YES')
      exit()
    n-=2
    cnt+=d-c
print('NO')

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter



import bisect
n,a,b,c,d=map(int,input().split())
delta=d-c
delta=delta*(n-1)
l=a-(n-1)*d
r=a+(n-1)*c
while True:
    if l<=b<=l+delta:
        print("YES")
        exit()
    l+=(c+d)
    if l>r:
        break
print("NO")

Solution (Python):

n,a,b,c,d=map(int,input().split())
ans='NO'
maxi=a+d*(n-1)
mini=a+c*(n-1)
if mini<=b<=maxi:
    ans='YES'

else:
    for i in range(1,n):
        maxi=maxi-d-c
        mini=mini-c-d
        if mini<=b<=maxi:
            ans='YES'
            break

print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,a,b,c,d = list(map(int, input().split()))
diff = a-b
for i in range(n):
    if c*(n-1-i)-i*d <= diff <= d*(n-1-i)-i*c:
        ans = "YES"
        break
else:
    ans = "NO"
print(ans)

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

for i in range(n):
    if c*i-d*(n-i-1)<=b-a<=d*i-c*(n-i-1):
        print("YES")
        break
else:
    print("NO")

Solution (Python):
n,a,b,c,d = map(int,input().split())
n -= 1

target = b-a

for i in range(n+1):
    left = c*i - d*(n-i)
    right = d*i - c*(n-i)
    if(left <= target <= right):
        print('YES')
        exit()

print('NO')

Solution (Python):
import sys
input = sys.stdin.buffer.readline





def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	n,a,b,c,d=MI()

	n-=1
	x=abs(b-a)

	ans="NO"

	for i in range(n):
		r=(n-i)*d-c*i
		l=(n-i)*c-d*i
		
		if l<=x<=r:
			ans="YES"
			
			break
		elif l<0:
			break

	print(ans)





if __name__ == "__main__":
	main()

Solution (Python):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

N -= 1

x = np.arange(N + 1, dtype=np.int64)  
low = C * x - D * (N - x)
high = D * x - C * (N - x)

diff = B - A
cond = np.any((low <= diff) & (diff <= high))
print('YES' if cond else 'NO')

Solution (Python):
n, a, b, c, d = map(int, input().split())
for m in range(n):
    l = c*(n-1-m)-d*m
    r = d*(n-1-m)-c*m
    if l <= b-a <= r:
        print('YES')
        exit()
else:
    print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
from collections import *

N, A, B, C, D = map(int, input().split())
d = max(A, B)-min(A, B)

for i in range(N):
    if C*i-D*(N-1-i)<=d<=D*i-C*(N-1-i):
        print('YES')
        exit()

print('NO')

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

dif = B - A

ans = "NO"
for i in range(N):
    pos = i
    neg = N - i - 1
    high = D * pos - C * neg
    low = C * pos - D * neg
    if low <= dif <= high:
        ans = "YES"
        break

print(ans)

Solution (Python):
n,a,b,c,d=map(int,input().split())
l=r=a
for i in range(n-1):
  if l+r<b*2:
    l+=c
    r+=d
  else:
    l-=d
    r-=c
if l<=b<=r:print('YES')
else:print('NO')

Solution (Python):
n,a,b,c,d = map(int, input().split())
for i in range(n):
  pi = i
  mi = n - i - 1
  if a + c * pi - d * mi <= b <= a + d * pi - c * mi:
    print("YES")
    break
else:
  print("NO")

Solution (Python):
n,a,b,c,d = map(int,input().split())
x = int((b-a+d*(n-1))/(c+d))
if x < 0 or x > n-1: s='NO'
else:
 z = c * x - d * (n-1-x)
 if z<=b-a<=z+(d-c)*(n-1):s='YES'
 else:s='NO'
print(s)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,a,b,c,d = LI()

    x = int((b-a+d*(n-1))/(c+d))
    if x < 0 or x > n-1:
        print('NO')
        return
    z = c * x - d * (n-1-x)
    if z <= b - a <= z + (d-c) * (n-1):
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Solution (Python):
N, A, B, C, D = map(int, input().split())
B = abs(B - A)

if N*C - (N-2)*D <= 0:
  if 0 <= B <= (N-1)*D:
    print('YES')
  else:
    print('NO')
else:
  if B > (N-1)*D:
    print('NO')
  else:
    diff = N*C - (N-2)*D
    lower = (N-1)*C
    while lower >= 0:
      if lower - diff < B < lower:
        print('NO')
        quit()
      lower -= D + C
    print('YES')

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

left_max = [A] * N
left_min = [A] * N
right_max = [B] * N
right_min = [B] * N

for i in range(1, N):
  left_max[i] = left_max[i - 1] + D
  left_min[i] = left_min[i - 1] + C

for i in range(N - 2, -1, -1):
  right_max[i] = right_max[i + 1] + D
  right_min[i] = right_min[i + 1] + C

able = False

for i in range(N):
  if left_max[i] >= right_min[i] and left_min[i] <= right_max[i]:
    able = True
    break

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

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

S = A - B - D * (N - 1)
if S > 0:
    print("NO")
else:
    if C == D == 0:
        if A == B:
            print("YES")
        else:
            print("NO")
    else:
        X = (-S) % (C+D)
        if X <= (D-C) * (N-1) and A - B + D * (N - 1) >= 0:
            print("YES")
        else:
            print("NO")

Solution (Python):
n, a, b, c, d = map(int, input().split())

flg = False
for x in range(n):
    y = n - 1 - x
    if x * d - y * c >= b - a >= x * c - y * d:
        flg = True

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

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)


def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


def main():
    N, A, B, C, D = read_values()

    K = (A - B + (N - 1) * D) // (C + D)
    M = A + (N - K - 1) * D - K * C
    m = A + (N - K - 1) * C - K * D
    print("YES" if 0 <= K and m <= B <= M else "NO")


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)


def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


def main():
    N, A, B, C, D = read_values()

    M = A + (N - 1) * D
    m = A + (N - 1) * C
    for _ in range(N):
        if m <= B <= M:
            print("YES")
            return
        
        M -= D + C
        m -= D + C

    print("NO")


if __name__ == "__main__":
    main()

Solution (Python):
N, A, B, C, D = map(int, input().split(' '))
flag = False
for m in range(0, N):
    flag |= (N - 1) * C - (C + D) * m <= A - B <= (N - 1) * D - (C + D) * m
print('YES' if flag else 'NO')

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

def check():
    l = (C*(N-1)-(B-A))/(D+C)
    r = (D*(N-1)-(B-A))/(D+C)
    if l<=N and r>=0 and int(r)>=l:
        return 'YES'
    return 'NO'
print(check())

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,a,b,c,d = na()

b = abs(b-a)
e = ((d-c)/2)*(n-1)
f = (c+d)/2
a = 0

for i in range(2,n+1):
    if a < b:
        a+=f
    else:
        a-=f

if a-e <= b <= a+e:
    print('YES')
else:
    print('NO')

Solution (Python):
n,a,b,c,d=map(int,input().split())
a,b=sorted([a,b])
for i in range(n-1):
    if max(a+c*(i),b+c*(n-1-i))<=min(b+d*(n-1-i),a+d*(i)):
        print("YES");exit()
print("NO")

Solution (Python):
n,a,b,c,d = map(int,input().split())
plus = [a,a]
minus = [a,a]
ans = 'NO'
if n%2 == 0:
    plus = [a+c,a+d]
    minus = [a-d,a-c]
can = (n-1)//2
delta = d-c
for i in range(can+1):
    if plus[0]-delta*(can-i) <= b <= plus[1] + delta*(can-i):
        ans = 'YES'
        break
    if minus[0] - delta*(can-i) <= b <= minus[1] + delta*(can-i):
        ans = 'YES'
        break
    plus[0] += 2*c
    plus[1] += 2*d
    minus[0] -= 2*d
    minus[1] -= 2*c
print(ans)

Solution (Python):
N,A,B,c,d=map(int,input().split())
n_time=N-1
_A=abs(A-B)
for i in range((N+1)//2):
    if (n_time-i)*d-i*c>=_A>=(n_time-i)*c-i*d:
        print('YES')
        break
else:
    print('NO')

Solution (Python):
n, a, b, c, d = map(int, input().split())
result = "NO"
for i in range(n-2):
    k = n-1-i
    mi = c*k-d*i
    ma = d*k-c*i
    if mi <= abs(b-a) and abs(b-a) <= ma:
        result = "YES"
        break
print(result)

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

for i in range(0,N):
    M=(C+D)*i-C*(N-1)
    m=(C+D)*i-D*(N-1)
    if M>=B-A>=m:
        print('YES')
        break
else:
    print('NO')

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))





N,A,B,C,D = LI()

for i in range(N):
    if A+i*C-(N-1-i)*D <= B <= A+i*D-(N-1-i)*C:
        print('YES')
        exit()

print('NO')

Solution (Python):

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

flag = False
for i in range(N):
    L = i * C + (i - (N - 1)) * D
    H = (i - (N - 1)) * C + i * D

    if L <= B - A <= H:
        flag = True

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

Solution (Python):
n, a, b, c, d = map(int, input().split())

for i in range(n-1):
    l = c*(n-1-i)-d*i
    r = -c*i+(n-1-i)*d
    if l <= b-a <= r:
        print('YES')
        exit()
else:
    print('NO')

Solution (Python):
n, a, b, c, d = map(int, input().split())

for i in range(n-1):
    l = c*(n-1-i)-d*i
    r = -c*i+(n-1-i)*d
    if l <= b-a <= r:
        print('YES')
        exit()
else:
    print('NO')

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

def isOk():
    M = B - A
    R = D - C

    for k in range(N):
        Z = M - (N - 1 - 2 * k) * C
        if -R * k <= Z <= R * (N - 1 - k):
            return True
    return False

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

Solution (Python):
N,A,B,C,D = map(int,input().split())
for p in range(N-1):
    m = N-1 - p
    mn = C*p - D*m
    mx = D*p - C*m
    if mn <= B-A <= mx:
        print('YES')
        exit()
print('NO')

Solution (Python):
n, a, b, c, d = [int(i) for i in input().split()]

ok = False
for i in range(n):
    j = n - 1 - i
    x, y = a + c * i - d * j, a + d * i - c * j
    if x <= b <= y:
        ok = True

if ok:
    print("YES")
else:
    print("NO")

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

ans = "NO"
for i in range(N):
    high = i * D - (N - i - 1) * C
    low = i * C - (N - i - 1) * D
    if low <= B <= high:
        ans = "YES"
print(ans)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,a,b,c,d = LI()
    b -= a
    n -= 1
    if b < -n*d or b > n*d:
        print("NO")
        return
    m = n*d
    x = -m
    y = n*(d-c)
    z = max(0,(n-1)*(c-d)+2*c)
    b -= x
    b %= (y+z)
    if b <= y:
        print("YES")
    else:
        print("NO")
    return


if __name__ == "__main__":
    solve()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,a,b,c,d = LI()
    b -= a
    n -= 1
    if b < -n*d or b > n*d:
        print("NO")
        return
    m = n*d
    x = -m
    y = n*(d-c)
    z = max(0,(n-1)*(c-d)+2*c)
    while x < m:
        if x <= b <= x+y:
            print("YES")
            return
        x += y+z
    print("NO")
    return


if __name__ == "__main__":
    solve()

Solution (Python):
def solve():
    n,a,b,c,d = map(int,input().split())
    mx = n-1
    for i in range(n):
        if a-d*i+c*(mx-i) <= b<=a-c*i+d*(mx-i):
            print('YES')
            return
    print('NO')
if __name__=='__main__':
    solve()

Solution (Python):
n,a,b,c,d=map(int,input().split())
x=y=a
for i in range(1,n):
  if (x+y)//2<b:
    x+=c
    y+=d
  else:
    x-=d
    y-=c
if x<=b<=y:print('YES')
else:print('NO')

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

L = A + N * C + D
R = A + N * D + C

for i in range(N):
    L -= C + D
    R -= C + D
    if L <= B <= R:
        print('YES')
        break
else:
    print('NO')

Solution (Python):
N, A, B, C, D = map(int, input().split())
l = A - (N-1) * D
r = A - (N-1) * C
if l <= B <= r:
    print('YES')
    exit()
for _ in range(N):
    l = r + 2*C + (N-2) * (C-D)
    r = l + (N-1) * (D-C)
    if l <= B <= r:
        print('YES')
        exit()
print('NO')

Solution (Python):
n, a, b, c, d = map(int, input().split())

ans = "NO"
for i in range(n):
    if i * c - (n - i - 1) * d <= b - a <= i * d - (n - i - 1) * c:
        ans = "YES"

print(ans)

Solution (Python):

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

flag = False
for i in range(N):
    L = i * C + (i - (N - 1)) * D
    H = (i - (N - 1)) * C + i * D

    if L <= B - A <= H:
        flag = True

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

Solution (Python):
n,a,b,c,d=map(int,input().split())
for i in range(n):
    if a+i*d-b>=c*(n-i-1)>=a+i*c-b or a+i*c-b<=d*(n-i-1)<=a+i*d-b or (d*(n-i-1)>a+i*d-b and c*(n-i-1)<a+i*c-b):
        print("YES")
        break
else:
    print("NO")

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

left = 0
right = N
if A > B:
    A, B = B, A


def solve(n):
    minimum = A + C * n - (N - 1 - n) * D
    maximum = A + D * n - (N - 1 - n) * C
    if minimum <= B <= maximum:
        print('YES')
        exit()
    elif B < minimum:
        return False
    else:
        return True


while right - left > 1:
    mid = (right + left) // 2
    if solve(mid):
        left = mid
    else:
        right = mid
else:
    print('NO')

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

for i in range(N):
    minimum = A + C * i - (N - 1 - i) * D
    maximum = A + D * i - (N - 1 - i) * C
    if minimum <= B <= maximum:
        print('YES')
        exit()
else:
    print('NO')

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

B -= A

ans = "NO"
for k in range(N):
    p = k
    q = N - k - 1
    Upper = D * p - C * q
    Lower = C * p - D * q
    if Lower <= B <= Upper:
        ans = "YES"
        break

print(ans)

Solution (Python):

n,a,b,c,d=map(int,input().split())
flag=False
for i in range(n):
    if (d+c)*i-(n-1)*d<=b-a<=(d+c)*i-(n-1)*c:
        flag=True
        break
if flag:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    N,A,B,C,D = map(int,input().split())
    if A > B:
        B = A+(A-B)
    ne = (C+D)/2
    wi = (D-C)*(N-1)+1
    use = [A+(2*i+(N+1)%2)*ne for i in range((N-1)//2+1)]
    for num in use:
        u,d = int(num+wi//2-(wi+1)%2/2),int(num-(wi//2-(wi+1)%2/2))
        if d <= B <= u:
            print("YES")
            exit()
            
    print("NO")

if __name__ == "__main__":
    main()

Solution (Python):
N, A, B, C, D = map(int, input().split())
for i in range(N):
    m = N - 1 - i
    if i * C + -m * D <= B - A <= i * D + -m * C:
        print('YES')
        exit()
print('NO')

Solution (Python):
n,a,b,c,d=map(int,input().split())
def kyo(p,q):
    if p[1]<q[0] or q[1]<p[0]:
        return False
    else:
        return True
for i in range(n):
    am=[a-i*d,a-i*c]
    ap=[a+i*c,a+i*c]
    bm=[b-(n-1-i)*d,b-(n-1-i)*c]
    bp=[b+(n-1-i)*c,b+(n-1-i)*d]
    for j in [am,ap]:
        for k in [bm,bp]:
            if kyo(j,k):
                print('YES')
                exit()
print('NO')

Solution (Python):
n, a, b, c, d = map(int,input().split())

y = 0
for i in range(n):
    j = n-1-i
    if i*d-j*c >= a-b >= i*c-j*d:
        y = 1
if y == 1:
    print("YES")
else:
    print("NO")

Solution (Python):
N, A, B, C, D = map(int, open(0).read().split())
m = B-A
M = B-A
j = N-1
for i in range(N-1):
  if m<=C*j<=M or m<=D*j<=M:
    print('YES')
    break
  m += C
  M += D
  j -= 1
else:
  print('NO')

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

ans = "NO"
B -= A
for i in range(N):
    num = i*D - (N-1-i)*D
    if num-i*(D-C) <= B <= num+(N-i-1)*(D-C):
        ans = "YES"
        break

print(ans)

Solution (Python):
N,A,B,C,D=map(int, open(0).read().split())
T=abs(A-B)
E=D-C
n=(N-1)//2
if N&1:
    b=any(-(n-i)*E+2*i*C<=T<=(n-i)*E+2*i*D for i in range(n+1))
else:
    b=any(-(n-i)*E+2*i*C+C<=T<=(n-i)*E+2*i*D+D for i in range(n+1))
print("YNEOS"[not b::2])

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,a,b,c,d = inm()
v = abs(b-a)
for i in range(n):
    l = i*c-(n-1-i)*d
    r = i*d-(n-1-i)*c
    if l<=v<=r:
        print('YES')
        exit()
print('NO')

Solution (Python):
n,a,b,c,d=map(int,input().split())
for k in range(n-1):
  if a+(c+d)*k-(n-1)*d<=b<=a+(c+d)*k-(n-1)*c:
    print('YES')
    exit()
print('NO')

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) 
def inpm(): return map(int,input().split()) 
def inpl(): return list(map(int, input().split())) 
def inpls(): return list(input().split())  
def inplm(n): return list(int(input()) for _ in range(n)) 
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) 

def main():
    n,a,b,c,d = inpm()
    for i in range(n-1):
        minus = i
        plus = n-1-i
        lower = minus*(-d) + plus*c
        higher = minus*(-c) + plus*d
        if lower <= b-a and b-a <= higher:
            print('YES')
            return
    print('NO')

if __name__ == "__main__":
    main()

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) 
def inpm(): return map(int,input().split()) 
def inpl(): return list(map(int, input().split())) 
def inpls(): return list(input().split())  
def inplm(n): return list(int(input()) for _ in range(n)) 
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) 

def main():
    n,a,b,c,d = inpm()
    if (c+d)+(n-1)*(c-d) <= 1 and a-(n-1)*d <= b and b <= a+(n-1)*d:
        print('YES')
        return
    for i in range(n):
        left = a - (n-1)*d + i*( (n-1)*(d-c) + (c+d)+(n-1)*(c-d) )
        right = left + (n-1)*(d-c)
        if left <= b and b <= right:
            print('YES')
            return
    print('NO')

if __name__ == "__main__":
    main()

Solution (Python):
n,a,b,c,d = map(int,input().split())
mx = n-1
for i in range(n):
    if a-d*i+c*(mx-i) <= b<=a-c*i+d*(mx-i):
        print('YES')
        exit()
print('NO')

Solution (Python):
N, A, B, C, D = map(int, input().split())
G = B-A
flag = False
for i in range(N):
    l = C*i-D*(N-1-i)
    r = D*i-C*(N-1-i)
    if l<=G<=r or -r<=G<=-l:
        flag = True
        break
if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
N, A, B, C, D = map(int, raw_input().split())

for m in range(N):
  if A-m*D+(N-m-1)*C <= B <= A-m*C+(N-m-1)*D:
    print "YES"
    exit(0)
print "NO"

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

E = C + D
for i in range(N):
  if A + (N - 1) * C - E * i <= B <= A + (N - 1) * D - E * i:
    print("YES")
    exit()
print("NO")