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: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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))

#################

# 範囲が A+iC-jD ~ A+iD-jC の形で進む

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')

Submission status: Accepted

Solution code:
# 解説AC
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#moderate differences
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")

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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 # and False
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
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()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
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)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

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()

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
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()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
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)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

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()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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"

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, A, B, C, D = map(int, input().split())
for i in range(N - 1):
  r = D * (N - i - 1) + A
  l = C * (N - i - 1) + A

  r -= C * i
  l -= D * i
  #print(l, r, i)
  if B in range(l, r + 1):
    print("YES")
    exit(0)
print("NO")

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = 0
    print(ans)
    return

def examB():
    N, A, B, C, D = LI()
    for i in range(N-1):
        low = A + i*C - (N-1-i)*D
        up = A + i*D - (N-1-i)*C
        if low<=B<=up:
            print("YES")
            return
    print("NO")
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Accepted

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

X = abs(A - B)

result = "NO"
for i in range(N):
    X_min = i * C - ((N - 1 - i) * D)
    X_max = i * D - ((N - 1 - i) * C)
    if X_min <= X <= X_max:
        result = "YES"
        break

print(result)

Submission status: Accepted

Solution code:
N, A, B, C, D = map(int, input().split())
E = B - A
for m in range(N):
  if C*(N-1-m) - D*m <= E <= -C*m + (N-1-m)*D:
    print("YES")
    break
else:
  print("NO")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

YES = "YES"  # type: str
NO = "NO"  # type: str


def calc_n_plus(required_diff, max_diff, min_diff, n):
    if (max_diff == 0) & (min_diff == 0):
        return 0
    return min(
        (required_diff + n * max_diff) // (max_diff + min_diff),
        n
    )


def check_in_max(required_diff, max_diff, min_diff, n, n_plus):
    max_val = n_plus * (min_diff + max_diff) - n * min_diff
    return required_diff <= max_val


# min_val = i * (min_diff + max_diff) - N * max_diff
# max_val = i * (min_diff + max_diff) - N * min_diff


def solve(N: int, A: int, B: int, C: int, D: int):
    n_plus = calc_n_plus(abs(B - A), D, C, N - 1)
    res = check_in_max(abs(B - A), D, C, N - 1, n_plus)
    print([NO, YES][res])
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    D = int(next(tokens))  # type: int
    solve(N, A, B, C, D)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,A,B,C,D = map(int,input().split())
x = B-A
flag = 0
for i in range(1,N+1):
    if -(N-i)*D+(i-1)*C<=x<=-(N-i)*C+(i-1)*D:
        flag = 1
        break
if flag==1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N, A, B, C, D = map(int, input().split())
ans = False
for i in range(N):
  inc = i
  dec = N-1-i
  # print(i, A + inc * C - dec * D, A + inc * D - dec * C)
  if A + inc * C - dec * D <= B <= A + inc * D - dec * C:
    ans = True
print('YES' if ans else 'NO')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

# Aを起点に対称に広がるのでとりあえずAを起点とする
B = abs(A - B)
for i in range(N):
    # i回足して、j回引く
    j = N - i - 1
    # 小さく足して大きく引く
    mn = C * i - D * j
    # 大きく足して小さく引く
    mx = D * i - C * j
    # 範囲内なら自由に調節が効くのでOK
    if mn <= B <= mx:
        YES()
        exit()
NO()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, A, B, C, D = MAP()
B -= A

l = D - C + 1
b = C * 2 - 1
mx = D
l += (D-C) * (N-2)
b = max(b - (D-C) * (N-2), 0)
mx *= N - 1

B += mx
mod = l + b
if B < 0 or B > mx*2:
    NO()
    exit()
if B % mod < l:
    YES()
else:
    NO()

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to only numbers
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, A, B, C, D = rl()
# N-1回を+の回数と-の回数ずつに区切る
bool = False
diff = B - A
for i in range(N):
    j = N-1-i
    #iが+, jが-
    if C*i-D*j <= diff <= D*i-C*j:
        bool = True
        break

print('YES' if bool else 'NO')
# 22

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,a,b,c,d=map(int,input().split())
e=abs(b-a)
if c+d+e==0:
    print("YES")
    exit()
upper=(e+(n-1)*d)//(c+d)
lower=(e+(n-1)*c)//(c+d)
if upper==lower:
    if (e+(n-1)*c) % (c+d) == 0 and 0<=lower and (e+(n-1)*c)/(c+d) <= n-1 and e <= (n-1)*max([c,d]):
        print("YES")
    else:
        print("NO")
else:
  if 0<=lower and (e+(n-1)*c)/(c+d) <= n-1 and e <= (n-1)*max([c,d]):
	    print("YES")
  else:
        print("NO")

Submission status: Accepted

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

flag = False
diff = B-A
d = 0
for i in range(0, N, 2):
    if i > 0:
        d += D-C
    j = N-1 - i
    if diff == d and j % 2 == 0:
        flag == True
        break
    if j == 0:
        break

    k = abs(diff - d) / j
    if (k >= C and k <= D) or (abs(diff-d) >= C and abs(diff-d) <= D and j % 2 == 1):
        flag = True
        break
    k = abs(diff + d) / j
    if (k >= C and k <= D) or (diff+d >= C and diff+d <= D and j % 2 == 1):
        flag = True
        break

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n,a,b,c,d=map(int,input().split())
    for k in range(n): # k 右に行った回数
        L=a+k*c-(n-k-1)*d
        R=a+k*d-(n-k-1)*c
        if(L<=b<=R):
            print("YES")
            return
    print("NO")
resolve()

Submission status: Accepted

Solution code:
n, a, b, m, M = map(int, input().split())
n -= 1
l, r = -1, n + 1
while r - l > 1:
	mid = (l + r) // 2
	t = (n - 2 * mid) * (m + M)
	if abs(t - 2 * (b - a)) <= n * (M - m):
		print("YES")
		exit()
	if t > 2 * (b - a):
		l = mid
	else:
		r = mid
print("NO")

Submission status: Accepted

Solution code:
n,a,b,m,M=map(int,input().split())
print("YES" if any((n-i-1)*m-i*M<=b-a<=-i*m+(n-i-1)*M for i in range(n)) else "NO")

Submission status: Accepted

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

cnt = n - 1
diff = abs(a-b)

for i in range(cnt+1):
    plus_cnt = i
    minus_cnt = cnt - i
    plus_range = [c*plus_cnt, d*plus_cnt]
    minus_range = [c*minus_cnt, d*minus_cnt]
    ran = [plus_range[0] - minus_range[1], plus_range[1] - minus_range[0]]
    if ran[0] <= diff and diff <= ran[1]:
        print("YES")
        exit()
print("NO")

Submission status: Accepted

Solution code:
# 解説

def solve() -> bool:
    n, a, b, c, d = map(int, input().split())
    return any((n - 1 - m) * c - m * d <= b - a <= (n - 1 - m) * d - m * c for m in range(n - 1))


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

Submission status: Accepted

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

def solve() :
    if N % 2 == 0 :
        l = (N // 2) * C - (N // 2) * D
        r = (N // 2) * D - (N // 2) * C
        for _ in range(N // 2 + 1) :
            if A + l <= B <= A + r :
                return 'YES'
            if A - r <= B <= A - l :
                return 'YES'
            l += C + D
            r += C + D
            
    else :
        l = (N // 2 + 1) * C - (N // 2) * D
        r = (N // 2 + 1) * D - (N // 2) * C
        for _ in range(N // 2 + 1) :
            if A + l <= B <= A + r :
                return 'YES'
            if A - r <= B <= A - l :
                return 'YES'
            l += C + D
            r += C + D
            
    return 'NO'
    
N -= 1    
print(solve())

Submission status: Accepted

Solution code:
def main():
    N, A, B, dmin, dmax = [int(n) for n in input().split()]
    diff = B - A
    i = N-1
    for i in range(N):
        plusmin = dmin*i
        plusmax = dmax*i#(pmin, pmax)
        minusmin = dmin * (N-1-i)
        minusmax = dmax*(N-1-i)#(-mmax, -mmin)
        left = plusmin - minusmax
        right = -minusmin + plusmax
        if left <= diff <= right:
            print("YES")
            return
    print("NO")
main()

Submission status: Accepted

Solution code:
n,a,b,c,d=map(int,input().split())
b=abs(b-a);flag=False
for i in range(1,n+1):
  ma=d*(i-1)-c*(n-i)
  mi=c*(i-1)-d*(n-i)
  if mi<=b and ma>=b:
    flag=True
print("YES" if flag else "NO")

Submission status: Accepted

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

    H = abs(A - B)

    if D == 0:
        return (H == 0)

    mi = C * (N - 1)
    ma = D * (N - 1)

    if H > ma:
        return False

    return ((H - mi) % (C + D) <= (ma - mi))

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

Submission status: Accepted

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

a, b = 0, abs(b-a)
if b > (n-1) * d:
    print('NO')
    exit()
if (n-1) * c <= b <= (n-1) * d:
    print('YES')
    exit()

if c == d:
    if b % c == 0 and b // c <= n-1:
        print('YES')
    else:
        print('NO')
    exit()

if (b//d) % 2 == (n-1)%2:
    diff1 = b - (b//d) * d
    if (diff1 - 1) // (d-c) + 1 <= (n-1 - (b//d)) // 2:
        print('YES')
        exit()
else:
    diff1 = b - (b//d - 1) * d
    if (diff1 - 1) // (d-c) + 1 <= (n-1 - (b//d - 1)) // 2:
        print('YES')
        exit()

if (((b-1)//c)+1) % 2 == (n-1)%2:
    diff2 = (((b-1)//c)+1) * c - b
    if (diff2-1) // (d-c) + 1 <= (n-1 - (((b-1)//c)+1)) // 2:
        print('YES')
        exit()
else:
    diff2 = (((b-1)//c)+2) * c - b
    if (diff2-1) // (d-c) + 1 <= (n-1 - (((b-1)//c)+2)) // 2:
        print('YES')
        exit()

print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def ascending_range(i):
    return (a+c*i, a+d*i)

def descendable_range(i, m, M):
    j = n-1-i
    floor = b+c*j
    ceil = b+d*j
    return not (m > ceil or M < floor)

def solve_local(i):
    return descendable_range(i, *ascending_range(i))

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

print("YES" if any(solve_local(i) for i in range(n)) else "NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

import sys

N,A,B,C,D = map(int,input().split())
num = abs(A-B)
nmax = (N-1) * D
nmin = (N-1) * C
flag = False

for i in range(N-1):

    if nmax >= num and num >= nmin:
        flag = True
        break

    nmax -= D + C
    nmin -= C + D

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left
import random
from itertools import permutations, accumulate, combinations
import sys
import string


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 998244353



n, a, b, c, d = LI()
for add_cnt in range(n):
    subtract_cnt = n - 1 - add_cnt
    min_val = add_cnt * c - subtract_cnt * d
    max_val = add_cnt * d - subtract_cnt * c
    if min_val <= b - a <= max_val:
        print('YES')
        break
else:
    print('NO')

Submission status: Accepted

Solution code:
n,a,b,c,d = map(int,input().split())
lo,hi = -d*(n-1), -c*(n-1)
for _ in range(n):
  if lo <= a-b <= hi:
    print("YES")
    break
  lo += c+d
  hi += c+d
else:
  print("NO")

Submission status: Accepted

Solution code:
n,a,b,c,d=map(int,input().split());print('NYOE S'[any(c*i-d*(~i+n)<=b-a<=d*i-c*(~i+n)for i in range(n))::2])

Submission status: Accepted

Solution code:
n,a,b,c,d=map(int,input().split())
f=0
for i in range(n):f|=i*c-(~i+n)*d<=b-a<=i*d-(~i+n)*c
print('NYOE S'[f::2])

Submission status: Accepted

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

Submission status: Accepted

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

S = B - A
ans = False
for m in range(N):
    n = N - 1 - m
    if C * m - D * n <= S <= D * m - C * n:
        ans = True
print('YES' if ans else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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

ans = 'NO'
for n in range(N+1):
    if A + n*C - (N-1-n)*D <= B <= A + n*D - (N-1-n)*C:
        ans = 'YES'
        break
print(ans)