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:
#!/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)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

N, A, B, C, D = map(int, input().split())
diff = B - A
if diff < 0:
    diff *= 1

def can_reach(k, M):
    if k < 0:
        return False
    mx = M * D
    mn = M * C
    m = (M - k) // 2

    mx -= (D + C) * m
    mn -= (D + C) * m

    # print("M, k, mn, mx = ", M, k, mn, mx)
    return mn <= diff <= mx

flag = False
if N % 2 == 1:
    for k in range(0, N+1, 2):
        if can_reach(k, N - 1):
            flag = True
else:
    for k in range(1, N+1, 2):
        if can_reach(k, N - 1):
            flag = True
if flag:
    print("YES")
else:
    print("NO")

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

    c_div, c_mod = divmod(dif_ab, C)
    d_div, d_mod = divmod(dif_ab, D)

    if d_div > n_dif:
        return "NO"

    if c_div > n_dif:
        dif_n_rest = n_dif - d_div
        dif_rest = d_mod
        if dif_n_rest % 2 == 1:
            dif_n_rest -= 1
            dif_rest += C
        if dif_rest <= dif_cd * dif_n_rest:
            return "YES"
        else:
            return "NO"
    else:
        dif_n_rest = n_dif - c_div
        dif_rest = c_mod
        if dif_n_rest % 2 == 1:
            dif_n_rest -= 1
            dif_rest += C
        if dif_rest <= dif_cd * dif_n_rest:
            return "YES"
        else:
            return "NO"


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


if __name__ == "__main__":
    main()

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

    c_div, c_mod = divmod(dif_ab, C)
    d_div, d_mod = divmod(dif_ab, D)

    if d_div > n_dif:
        return "NO"

    if c_div > n_dif:
        dif_n_rest = n_dif - d_div
        dif_rest = d_mod
        if dif_n_rest % 2 == 1:
            dif_n_rest -= 1
            dif_rest += C
        if dif_rest <= dif_cd * dif_n_rest:
            return "YES"
        else:
            return "NO"
    else:
        dif_n_rest = n_dif - c_div
        dif_rest = c_mod
        if dif_n_rest % 2 == 1:
            dif_n_rest -= 1
            dif_rest += C
        if dif_rest <= dif_cd * n_dif:
            return "YES"
        else:
            return "NO"


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


if __name__ == "__main__":
    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, 2):
        if C*(M - i) <= B <= (D - C)*i + D*(M - i):
            return True

    return False

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

Submission status: Wrong Answer

Solution code:
(N, A, B, C, D) = map(int, raw_input().split())
ans = True
if abs(B-A)/float(N) >= D:
	ans = False

if ans:
	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 + 1)/2) + D*(M - i):
            return True

    return False

print "YES" if solve() else "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 - 1, 2):
        if C*(M - i) <= B <= (D - C)*((i + 1)/2) + D*(M - i):
            return True

    return False

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

Submission status: Wrong Answer

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

A = input().split()
#A = [5, 1, 5, 2, 4]
#A = [4, 7, 6, 4, 5]
#A  = [48792, 105960835, 681218449, 90629745, 90632170]
#A = [491995, 412925347, 825318103, 59999126, 59999339]

n = int(A[0])
a = int(A[1])
b = int(A[2])
c = int(A[3])
d = int(A[4])

sa = d- c

LR = abs(a - b)

x = sa * (n - 1)

if  a > d and a - sa > d:
#    print(1)
    print("NO")

elif  a < c and a + sa < c:
#    print(2)
    print("NO")

elif  b > d and b - sa > d:
#    print(3)
    print("NO")

elif  b < c and b + sa < c:
#    print(4)
    print("NO")


elif x >= LR:
#    print(5)
    print("YES")

else:
#    print(6)
    print("NO")

Submission status: Wrong Answer

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

A = input().split()
#A = [5, 1, 5, 2, 4]
#A = [4, 7, 6, 4, 5]
#A  = [48792, 105960835, 681218449, 90629745, 90632170]
#A = [491995, 412925347, 825318103, 59999126, 59999339]

n = int(A[0])
a = int(A[1])
b = int(A[2])
c = int(A[3])
d = int(A[4])

sa = d- c

LR = abs(a - b)

x = sa * (n - 1)

if  a > d and a - sa > d:
    print(1)
    print("NO")

elif  a < c and a + sa < c:
    print(2)
    print("NO")

elif  b > d and b - sa > d:
    print(3)
    print("NO")

elif  b < c and b + sa < c:
    print(4)
    print("NO")


elif x >= LR:
    print(5)
    print("YES")

else:
    print(6)
    print("NO")

Submission status: Wrong Answer

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

A = input().split()
#A = [5, 1, 5, 2, 4]
#A = [4, 7, 6, 4, 5]
#A  = [48792, 105960835, 681218449, 90629745, 90632170]
#A = [491995, 412925347, 825318103, 59999126, 59999339]

n = int(A[0])
a = int(A[1])
b = int(A[2])
c = int(A[3])
d = int(A[4])

sa = d- c

LR = abs(a - b)

x = sa * (n - 1)

if sa <= 1:
    print("NO")

elif x >= LR:
    print("YES")



else:
    print("NO")

Submission status: Time Limit Exceeded

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

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


def calc_min_max(n_plus, n_minus, min_diff, max_diff):
    min_val = n_plus * min_diff - n_minus * max_diff
    max_val = n_plus * max_diff - n_minus * min_diff
    return min_val, max_val


def loc_in_range(init_val, end_val, min_val, max_val):
    in_max = (init_val + max_val) >= end_val
    in_min = (init_val + min_val) <= end_val
    if in_max and in_min:
        return True
    elif in_max:
        return 'lower'
    return 'higher'


def update_n_plus_minus(n_plus, n_minus, n_max, n_min, loc):
    if loc == 'higher':
        diff = (n_max - n_plus) // 2
        if (n_max - n_plus) == 1:
            diff = 1
        return (
            n_plus + diff, 
            n_minus - diff,
            n_max,
            n_plus,
        )
    diff = (n_plus - n_min) // 2
    if (n_plus - n_min) == 1:
        diff = 1
    return (
        n_plus - diff,
        n_minus + diff,
        n_plus,
        n_min,
    )


def binary_search(N, init_val, end_val, min_diff, max_diff):
    n_plus = (N - 1) // 2
    n_minus = (N - 1) - n_plus
    min_val, max_val = calc_min_max(n_plus, n_minus, min_diff, max_diff)
    n_max, n_min = N - 1, 0
    loc = loc_in_range(init_val, end_val, min_val, max_val)
    while loc in ['lower', 'higher']:
        new_n_plus, new_n_minus, n_max, n_min = update_n_plus_minus(
            n_plus, n_minus, n_max, n_min, loc
        )
        if new_n_plus == n_plus:
            return False
        n_plus, n_minus = new_n_plus, new_n_minus
        min_val, max_val = calc_min_max(n_plus, n_minus, min_diff, max_diff)
        loc = loc_in_range(init_val, end_val, min_val, max_val)
    return True


def solve(N: int, A: int, B: int, C: int, D: int):
    res = binary_search(N, A, B, C, D)
    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: Time Limit Exceeded

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

B = B - A
A = 0

dp = {}
dp[1] = {}
dp[1][A] = True
for i in range(2, N + 1):
    dp[i] = {}
    for n in dp[i - 1]:
        for x in range(C, D + 1):
            dp[i][n + x] = True
            dp[i][n - x] = True

if B in dp[N]:
    print('YES')
else:
    print('NO')

Submission status: Time Limit Exceeded

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

memo_dic = {}

def OK_area(n, c, d):
    l = []
    for i in range(c, d+1):
        l.append(n-i)
        l.append(n+i)
    l_uniq = list(set(l))
    return l_uniq

a = min(A, B)
b = max(A, B)
start = [a]
for i in range(N-1):
    list_i = []
    for s in start:
        if s in memo_dic:
            area = memo_dic[s]
        else:
            area = OK_area(s, C, D)
            memo_dic[s] = area
        list_i.extend(area)
        
    list_i_uniq = list(set(list_i))
    start = list_i_uniq
if b in start:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

#s = []
#for n in arr:
#    s.append(str("%d-%s-%d" % ((n-W*(N-1))/2, n/2, (n+W*(N-1))/2)))
#print(', '.join(s))
    

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# m回増えて、N-1-m回減ったとしたときの範囲は出せるので、全探索。
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")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 【考察】
# 原点から、移動距離が C ～ D で移動していくと、到達できる区間は…
# [0]
# [-D, -C], [C, D]
# [-2D, -2C], [-D+C, D-C], [2C, 2D]
# [-3D, -3C], [-2D+C, D-2C], [-D+2C, 2D-C], [3C, 3D]
# [-4D, -4C], [-3D+C, D-3C], [-2D+2C, 2D-2C], [-D+3C, 3D-C], [4C, 4D]
# ...


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

Submission status: Accepted

Solution code:
N,a,b,c,d=map(int, input().split())
N-=1
a,b=0,abs(b-a)
#上下をn回とすると、0<=n<=N//2

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

Submission status: Accepted

Solution code:
N,a,b,c,d=map(int, input().split())
N-=1
a,b=0,abs(b-a)
#上下をn回とすると、0<=n<=N//2
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')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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

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

# n回で絶対値Xを作れるか

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

# n * Cより小さい数を作る必要があるため、Cを何回使うかを基準に考える
for i in range(1, n):
  rest_1 = abs(C * i - X)
  rest_2 = X + C * i
  # 絶対値restを残り使える数で作れるか
  useful_num = n - i # 残り使える数
  # 残りの数を割って、C <= 商 <= Dに収まるなら作れる
  if (C <= (rest_1 / useful_num) <= D) or (C <= (rest_2 / useful_num) <= D):
    print("YES")
    break
else:
  print("NO")

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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

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

# n回で絶対値Xを作れるか

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

# n * Cより小さい数を作る必要があるため、Cを何回使うかを基準に考える
for i in range(1, n):
  rest_1 = abs(C * i - X)
  rest_2 = X + C * i
  # 絶対値restを残り使える数で作れるか
  useful_num = n - i # 残り使える数
  # 残りの数を割って、C <= 商 <= Dに収まるなら作れる
  if (C <= (rest_1 / useful_num) <= D) or (C <= (rest_2 / useful_num) <= D):
#    print("i",i,"rest_2",rest_2,"useful_num",useful_num)
#    print("rest_2 // useful_num",rest_2 // useful_num)
    print("YES")
    break
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
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")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

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()))
# rstrip().decode()

#import numpy as np

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
		#print(l,x,r)
		if l<=x<=r:
			ans="YES"
			#print(l,x,r)
			break
		elif l<0:
			break

	print(ans)





if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
# mod = 10 ** 9 + 7

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

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
# mod = 10 ** 9 + 7

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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