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:
if __name__ == "__main__":
    N, A, B, C, D = map(int, input().split())
    # if A > B:
    #     A, B = B, A
    rest = N - 2

    for k in range(rest + 1):
        #minus
        l = rest + 1 - k
        left = A + C * k - l * D
        right = A + D * k - l * C
        # print(k, left, right)
        if left <= B and B <= right:
            print("YES")
            exit(0)
    print("NO")

Submission status: Accepted

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

import sys, math, copy
# import fractions, itertools
# import numpy as np
# import scipy

HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    n, a, b, c, d = map(int, input().split())
    diff = b - a
    for m in range(n):
        mi = -d * m + c * (n - 1 - m)
        ma = -c * m + d * (n - 1 - m)
        if mi <= diff <= ma:
            return True
    return False

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

Submission status: Accepted

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

for k in range(N):
    if k*C - (N-1-k)*D <= abs(B-A) <= k*D - (N-1-k)*C:
        print("YES")
        break
else:
    print("NO")

Submission status: Accepted

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

ok = 0
B -= A; B = abs(B)
k = N-1
if B <= k*D:
    if (k-1)*(D-C) >= 2*C:
        ok = 1
    else:
        r = k*(D-C)
        sp = 2*C - (k-1)*(D-C)
        if k % 2 == 1:
            # sp/2 -> r -> sp -> r -> ...
            if sp//2 <= B and (B-sp//2) % (sp + r) <= r:
                ok = 1
        else:
            # r/2 -> sp -> r -> sp -> r -> ...
            if B <= r//2 or (B-r//2-sp) % (sp + r) <= r:
                ok = 1
print('YES' if ok else 'NO')

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    N, A, B, C, D = map(int, input().split())
    B -= A
    for i in range(N):
        j = N - i - 1
        large1 = C * i
        small1 = D * j
        small = large1 - small1

        large2 = D * i
        small2 = C * j
        large = large2 - small2
        if small <= B <= large:
            print("YES")
            return
    print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input1 = list(map(int,input("").split(" ")))
N = input1[0]
A = input1[1]
B = input1[2]
C = input1[3]
D = input1[4]

flag = False
for x in range(N-1):
	y = N -1 - x
	if (C*y - D*x) <= (B-A) and (B-A) <= -1*C*x + y*D:
		flag = True
		break

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

Submission status: Accepted

Solution code:
def moderate_differences_2():
	n, a, b, c, d = map(int, raw_input().split(" "))
	# there are totally n-1 differences
	# assume y_i = x_(i+1)-x_i
	# -d<=y_i<=-c or c<=y_i<=d
	# assume there are m y_is meet -d<=y_i<=-c
	for m in range(0, n):
		max_sum = m*(-c)+(n-1-m)*(d)
		min_sum = m*(-d)+(n-1-m)*(c)
		if (b-a)>=min_sum and (b-a)<=max_sum:
			print "YES"
			return
	print "NO"

if __name__ == "__main__":
	moderate_differences_2()

Submission status: Accepted

Solution code:
N,A,B,C,D=map(int,input().split())
ans=0
for i in range(N):
  if C*i-D*(N-1-i)<= B-A<= D*i-C*(N-1-i): ans=1

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

Submission status: Accepted

Solution code:
N, A, B, C, D = map(int, input().split())
# N, A, B, C, D = 48792, 105960835, 681218449, 90629745, 90632170
# N, A, B, C, D = 491995, 412925347, 825318103, 59999126, 59999339


succeed = False
for m in range(N):
    if C * (N - 1 - m) - D * m <= B - A <= -C * m + (N - 1 - m) * D:
        succeed = True
        break

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

Submission status: Accepted

Solution code:
# coding: utf-8

# AtCoder Grand Contest 017
# B - Moderate Differences

# This code is compatible with both Python 2 and 3;
# however, in Python 2, the int type must be 64 bits,
# due to the range of the parameters of this problem.

import sys, re

def solve(N, A, B, C, D):
  for i in range(N):
    upper = i * D - (N - 1 - i) * C
    lower = i * C - (N - 1 - i) * D
    if lower <= B - A and B - A <= upper:
      return True
  return False

for line in sys.stdin:
  words = re.split(r'\s+', line.strip())
  N, A, B, C, D = [int(w) for w in words]  # convert to int
  result = solve(N, A, B, C, D)
  print('YES' if result else 'NO')


# 以降, コメント:
#
# 左から第0マス, 第1マス, ..., 第N-1マスと呼ぶ.
# 第0マスに0, 第N-1マスにB-Aを書いても結果 (YES or NO) は変わらない
# ので, 以降, 青橋君は第0マスに0を書くものと仮定する.
#
# 第kマス (0 <= k < N) に書くことのできる数は,
#   (i * C - (k - i) * D)以上 (i * D - (k - i) * C)以下
# (ただし 0 <= i <= k) の整数である
# (これらの数は書くことができる, かつこれら以外は書くことができない).
# このことはkに関する数学的帰納法で示すことができる.
#
# 従って, 0以上N-1以下のある i について
# B-A が (i * C - (N-1-i) * D)以上 (i * D - (N-1-i) * C)以下のとき,
# かつそのときのみ, 青橋君は第N-1マスにB-Aを書くことができる.

Submission status: Accepted

Solution code:
# AtCoder Grand Contest 017
# B - Moderate Differences

import sys, re

def solve(args):
  N = args[0]
  A = args[1]
  B = args[2]
  C = args[3]
  D = args[4]
  for i in range(N):
    upper = i * D - (N - 1 - i) * C
    lower = i * C - (N - 1 - i) * D
    if lower <= B - A and B - A <= upper:
      return True
  return False

for line in sys.stdin:
  words = re.split(r'\s+', line.strip())
  args  = [int(w) for w in words]  # convert to int
  print('YES' if solve(args) else 'NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, a, b, c, d):
    if d == 0:
        return a == b
    t = n - 1
    ab = abs(a - b)
    if ab > d * t:
        return False
    return (ab - c * t) % (c + d) <= (d - c) * t


print('YES' if solve(*map(int, input().split())) else 'NO')

Submission status: Accepted

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

flag = False
for k in range(1,n-1):
    if(k*c-(n-1-k)*d <= b-a and b-a <= k*d-(n-1-k)*c):
        flag = True
if(flag): print("YES")
else: print("NO")

Submission status: Accepted

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

answer_flag = False
for i in range(N):
    if (C*(N-1-i)-D*i<=(B-A)) and ((B-A)<=-C*i+D*(N-1-i)):
        answer_flag = True
        break

if answer_flag==True:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

a = abs(A - B)
b = abs(D - C)
l = 0
r = 0
p = 0

for i in range(N-1):
    if p < a:
        p += C
        r += b
    else:
        p -= C
        l += b
        
if p <= a:
    print('YES' if p+r >= a else 'NO')
else:
    print('YES' if p-l <= a else 'NO')

Submission status: Accepted

Solution code:
def f():
    n, a, b, c, d = list(map(int, input().split(" ")))
    h = abs(b-a)
    for m in range(n):
        if c*m-d*(n-m-1)<=h<=d*m - c*(n-m-1):
            print("YES")
            return
    print("NO")

f()

Submission status: Accepted

Solution code:
n, a, b, c, d = map(int, raw_input().split())
if a > b: a, b = b, a
for i in xrange(n - 1):
    mn = -d * i + c * (n - 1 - i)
    mx = -c * i + d * (n - 1 - i)
    if mn <= b - a <= mx:
        print "YES"
        exit()
print "NO"

Submission status: Accepted

Solution code:
N, A, B, C, D = list(map(int, input().split()))
isYes = False
for k in range(N-1):
	if k*C-(N-1-k)*D <= B-A and B-A <= k*D-(N-1-k)*C:
		isYes = True
		break
if isYes:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

# k : count of plus
diff = B - A

is_ok = False
for k in range(N - 1):
    min = k * C - (N - 1 - k) * D
    max = k * D - (N - 1 - k) * C
    if min <= diff and diff <= max:
        is_ok = True
        break

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

Submission status: Accepted

Solution code:
n, a, b, c, d = map(int, raw_input().split())
dd = b - a
n -= 1
if dd < 0:
  dd = -dd
#print(dd)

def get(n, c, d):
  l = -1
  r = n
  while l + 1 < r:
    mid = (l + r) / 2
    if mid * c - (n - mid) * d >= 0:
      r = mid
    else:
      l = mid
  return l if l >= 0 else 0

l = get(n, c, d)

ok = False
#print(l)
for i in range(l, n + 1):
  ll = i * c - (n - i) * d
  rr = i * d - (n - i) * c
  #print(ll, rr)
  if ll <= dd and rr >= dd:
    #print(i, ll, rr, dd)
    ok = True
  if ll > dd:
    break

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

Submission status: Accepted

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

def foo():
    for i in range(N):
        hoge = C * (N - 1 - i) - D * i <= B - A
        fuga = B - A <= -C * i + (N - 1 - i) * D
        
        yield hoge and fuga

print('YES' if any(foo()) else 'NO')

Submission status: Accepted

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


for i in range(N) :
    j = N-1-i
    lb = A+C*i-D*j
    ub = A+D*i-C*j
    if lb <= B <= ub :
        print('YES')
        break
else :
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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(N):
        if C*(M - i) - D*i <= B <= D*(M - i) - C*i:
            return True

    return False

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

Submission status: Accepted

Solution code:
N,A,B,C,D = map(int, raw_input().rstrip().split(' '))
diff = abs(B-A)
r = False

m = N-1
for k in xrange(1, 1+m):
    hi = k*D - (m-k)*C
    lo = k*C - (m-k)*D
    if lo <= diff <= hi:
        r = True
        break

print 'YES' if r else 'NO'

Submission status: Accepted

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

#N = 48792
#A = 105960835
#B = 5
#C = 2
#D = 4

num = abs(B-A)
ans = False
for i in range(N-1, N):
    if(i%2==1):
        for j in range(i//2+1, i+1):
            if(j*C-(i-j)*D<=num and num<=j*D-(i-j)*C):
                ans = True
    else:
        for j in range(i//2, i+1):
            if(j*C-(i-j)*D<=num and num<= j*D-(i-j)*C):
                ans = True
if(ans):
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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


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


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

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


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

exit(0)

Submission status: Accepted

Solution code:
# encoding:utf-8
import math

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

	for i in range(N):
		plus_band = calcBand(i, C, D)
		minus_band = calcBand(N - i - 1, C, D)
		total_band = (plus_band[0] - minus_band[1], plus_band[1] - minus_band[0])

		if total_band[0] <= B - A <= total_band[1]:
			print("YES")
			break
	else:
		print("NO")

def calcBand(n_band, C, D):
	return((C * n_band, D * n_band))

if __name__ == '__main__':
	main()

Submission status: Accepted

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

        range_c_d = (D - C) / 2

        ul = A - average_c_d * (N - 1)

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

        if target_index < 0:
            target_index = 0
        elif N - 1 < target_index:
            target_index = N - 1

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

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

Submission status: Accepted

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

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

    return False

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

Submission status: Accepted

Solution code:
N,A,B,C,D=[int(i) for i in input().split()]
diff=B-A

found=False
for i in range(N):
    j=N-1-i
    left=i*C-j*D
    right=i*D-j*C
    if left<=diff and diff<=right:
        print("YES")
        found=True
        break
if not(found):
    print("NO")

Submission status: Accepted

Solution code:
def main():
    n, a, b, c, d = [int(_) for _ in raw_input().split()]
    x0 = int(round((b - a) / (c + d) * 1.0 + (n - 1) / 2.0))
    for x in xrange(max(0, x0 - 2), min(x0 + 3, n - 1)):
        m1 = a + c * x
        M1 = a + d * x
        m2 = b - c * (x - n + 1)
        M2 = b - d * (x - n + 1)
        if max(m1, m2) <= min(M1, M2):
            return True
    return False


if __name__ == '__main__':
    print("YES" if main() else "NO")

Submission status: Accepted

Solution code:
import sys

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

n -= 1;

u_lim = a + n * d;
l_lim = a + n * c;

for i in range(n):
	if b >= l_lim and b <= u_lim:
		print("YES")
		sys.exit(0)
	u_lim = u_lim - c - d;
	l_lim = l_lim - c - d;
print("NO")

Submission status: Accepted

Solution code:
N,left,right,up,down=map(int,raw_input().split())
diff=abs(left-right)
def maxmin(plus,minus):
    upper=plus*(up)-minus*(down)
    under=plus*(down)-minus*(up)
    return sorted([upper,under])[::-1]
upper=N-1
under=0
flag=False
while upper>=under and not flag:
    mid=(upper+under)/2
    m=maxmin(mid,N-mid-1)
    if m[1]<=diff<=m[0]:
        flag=True
    if diff>=m[0]:
        under=mid+1
    if diff<=m[1]:
        upper=mid-1
print "YES" if flag else "NO"

Submission status: Accepted

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("diff, M, k, mn, mx = ", diff, 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: Accepted

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

Submission status: Accepted

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

judge = "null"
for i in range(N):
	mx = D * i - C * (N - 1 - i)
	mn = C * i - D * (N - 1 - i)
	if mx >= B - A and mn <= B - A:
		print("YES")
		judge = "yes"
		break

if judge == "null":
	print("NO")

Submission status: Accepted

Solution code:
import sys

def diff(N,A,B,C,D):
    mini = (B+(N-1)*D-A)/(C+D)
    for i in range(int(mini), N-1):
        lower_bound = A + i*C - (N-i-1)*D
        upper_bound = A + i*D - (N-i-1)*C
        #print(lower_bound, upper_bound)
        if lower_bound <= B and B <= upper_bound:
            return True
        elif B < lower_bound:
            return False

str = sys.stdin.readline().strip() #"491995 412925347 825318103 59999126 59999339"
N,A,B,C,D = map(int,str.strip().split(" "))
print("YES" if diff(N,A,B,C,D) else "NO")

Submission status: Accepted

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

from __future__ import division, print_function
import sys


def main():
    n, a, b, c, d = map(int, sys.stdin.readline().strip().split(" "))
    m = n - 1
    diff = b - a

    for i in range(m):
        j = m - i
        plus_min = c * i
        plus_max = d * i
        minus_min = -c * j
        minus_max = -d * j
        minimum = plus_min + minus_max
        maximum = plus_max + minus_min
        if minimum <= diff <= maximum:
            print("YES")
            break
    else:
        print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = False
l = 0
r = n - 1
cnt = 0
while cnt <= 20:
    m = (l + r) // 2
    if b < a + m * c - (n - 1 - m) * d:
        r = m
    elif a + m * d - (n - 1 - m) * c < b:
        l = m
    else:
        ans = True
        break
    cnt += 1
print('YES' if ans else 'NO')

Submission status: Accepted

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

ans = False
for m in range(n):
    if a + m * c - (n - 1 - m) * d <= b <= a + m * d - (n - 1 - m) * c:
        ans = True
        break

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

Submission status: Accepted

Solution code:

N,A,B,C,D= list(map(int,input().split()))
def nibun(func ,min_ ,max_ ):
    left = min_
    right= max_
    while not(left==right):
        mid = (left+right) //2
        tmp = func(mid)
        if tmp:
            right = mid
        else:
            left = mid+1
    return left

X = abs(A-B)
def calc(n):
    m = N-n-1
    min_val = -D*n +C*m
    max_val = -C*n +D*m
    return min_val , max_val

def check(n):
    return calc(n)[0] <=X


n= (nibun(check,0,N))
print("YES" if calc(n)[0]<=abs(A-B) and calc(n)[1]>=abs(A-B)else "NO")

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('b1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve_it0(n, a, b, c, d):
    p = [[0]]
    for i in range(1, n):
        q = set()
        for x in p[-1]:
            for k in range(c, d + 1):
                q.add(x - k)
                q.add(x + k)
        p.append(sorted(q))
    for l in p:
        print(l)
    if b - a in p[-1]:
        return 'YES'
    return 'NO'


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


def solve():
    n, a, b, c, d = read_int_list()
    res1 = solve_it1(n, a, b, c, d)
    # res0 = solve_it0(n, a, b, c, d)
    # assert res0 == res1
    return res1


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

res = 'NO'
dif = D - C
E = abs(B - A) 

for n in range(0, N):
    min_n = (N - 1 - n) * C - n*D
    max_n = (N - 1 - n) * D - n*C
    if min_n <= E and E <= max_n:
        res = 'YES'
        break

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, a, b, c, d = map(int, raw_input().split())
t = n - 1
for i in xrange(t + 1):
    l, r = a + c * i, a + d * i
    l, r = l - d * (t - i), r - c * (t - i)
    if l <= b <= r:
        print "YES"
        break
else:
    print "NO"

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    n,a,b,c,d = LI()
    t = n - 1
    s = abs(a-b)
    if s > d * t:
        return 'NO'
    if s >= c * t:
        return 'YES'

    for i in range(1,t):
        k = c * (t-i) - d * i
        if k <= s:
            if (d-c) * t >= s-k:
                return 'YES'
            break
    return 'NO'


print(main())

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

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

# iterate through number of +'s and -'s
for i in range(n):
    j=n-1-i
    # i +'s, j -'s
    x=a+c*i-d*j
    y=a+d*i-c*j
    if x<=b<=y:
        print("YES")
        break
else:
    print("NO")

Submission status: Runtime Error

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 B >= Q[j]:
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

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: Runtime Error

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

dp = [0] * N
dp[0] = A
dp[-1] = B
x = -1
def no():
  print("NO")
  exit(0)
if B < A: A, B = B, A
for i in range(N - 2):
  if dp[i] < B:
    dp[i + 1] = dp[i] + D
  else:
    #print(dp[i], (i - 1) * (D - C), B)
    f = abs(dp[i] - (i - 1) * (D - C))
    if f <= B:
      x = i
      dp[i] = B
    else:
      x = i + -(-abs(B - f) // (D - C)) * 2
      if x >= N: no()
      dp[x] = B
    break

#print(x, dp[x])
t = (N - 1 - x)
#print(t)
if t % 2:
  if t == 1: no()
  if -(-C // (D - C)) > t: no()
print("YES")

Submission status: Runtime Error

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

dp = [0] * N
dp[0] = A
dp[-1] = B
x = -1
def no():
  print("NO")
  exit(0)
if B < A: A, B = B, A
for i in range(N - 2):
  if dp[i] < B:
    dp[i + 1] = dp[i] + D
  else:
    #print(dp[i], (i - 1) * (D - C), B)
    if abs(dp[i] - (i - 1) * (D - C)) <= B:
      x = i
      dp[i] = B
    else:
      x = i + -(-abs(B - dp[i]) // (D - C)) * 2
      if x >= N: no()
      dp[x] = B
    break

#print(x, dp[x])
t = (N - 1 - x)
#print(t)
if t % 2:
  if t == 1: no()
  if -(-C // (D - C)) > t: no()
print("YES")

Submission status: Runtime Error

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")
        print(2//0)
    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: Runtime Error

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")
        print(2//0)
    else:
        print("NO")
else:
    print("YES")

Submission status: Runtime Error

Solution code:
N,A,B,C,D=map(int,input().split())
if A> B:
  A,B=B,A
if C>D:
  C,B=B,C
if B-A>D*(N-1):
  print("NO")
elif B-A>=C*(N-1):
  print("YES")
else:
  flag=False
  diff=B-A
  d=0
  for i in range(2,N,2):
    d+=D-C
    j= N-1 - i
    if j==0:
      if diff==d:
        flag=True
      break
    k=(diff - d) / j
    if k>=C and k<=D:
      flag=True
    
if flag:
  print("YES")
else:
  print("NO")

Submission status: Runtime Error

Solution code:

N,M,D = map(int,input().split())

A = list(map(int,input().split()))


lis = [i for i in range(N)]

for i in range(M):

    a = A[i] - 1

    k = lis[a]
    lis[a] = lis[a+1]
    lis[a+1] = k


#print (lis)

end = [False] * N
endp = 0
ans = [None] * N

while endp < N:

    if end[endp]:
        endp += 1

    else:
        q = []
        start = endp
        now = endp

        while len(q) == 0 or start != now:

            q.append(now)
            now = lis[now]

        amari = D % len(q)

        for i in range(len(q)):

            end[q[i]] = True
            ind = (i + amari) % len(q)
            ans[q[i]] = q[ind]

ans2 = [0] * N

for i in range(N):

    ans2[ans[i]] = i + 1

for i in ans2:
    print (i)

Submission status: Runtime Error

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

Submission status: Runtime Error

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


#print("target:", abs(A-B))
#print("range: ", D-C)
#print("target is larger than D: " ,
#     abs(A-B)>D)
#target=abs(A-B)
#print("next target is: ", target-D)
#print("how many? : ", (target-D)//(D-C))

ans=False
def g(a,b,c,d,n):
  if (n-1)%2==0:
    target=abs(a-b) #スタートは0
    gain=d-c
    if ((n-1)//2)*gain>=target:
      return True
    else:
      return False
  else:
    target=abs(a-b)
    gain=d-c
    if target>d:
      if (n-2)//2 * gain >=target-d:
        return True
      else:
        return False
    elif target<c:
      if (n-2)//2 *gain >= c- target:
        return True
      else:
        return False
    elif c<=target<=d:
      return True
      
A,B=min(A,B),max(A,B)
target=B-A
pos=0
for i in range(N+1):
  target=abs(target)
  if  g(pos, target, C,D, N-i):
      print("YES")
      exit(0)
 　target-=D
    
print("NO")
exit(0)      
  
if g(A,B,C,D,N):
  print("YES")
else:
  print("NO")

Submission status: Runtime Error

Solution code:

input1s = input()

input1 = [int(i) for i in input1s.split(' ')]
N = input1[0]
A = input1[1]
B = input1[2]
C = input1[3]
D = input1[4]

AB = math.fabs(A-B)
DC = math.fabs(D-C)
m = N-2

if((N-2)%2 == 0):
  if( C-m*(D-C) <= AB and AB <= m*(D-C)+D ):
    print("YES")
  else:
    print("NO")
if((N-2)%2 == 1):
  if( 0 <= AB and AB <= m*(D-C)+2*D):
    print("YES")
  else:
    print("NO")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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