Problem:


Score : 600 points

Problem Statement

Ringo Mart, a convenience store, sells apple juice.

On the opening day of Ringo Mart, there were A cans of juice in stock in the morning.
Snuke buys B cans of juice here every day in the daytime.
Then, the manager checks the number of cans of juice remaining in stock every night.
If there are C or less cans, D new cans will be added to the stock by the next morning.

Determine if Snuke can buy juice indefinitely, that is, there is always B or more cans of juice in stock when he attempts to buy them.
Nobody besides Snuke buy juice at this store.

Note that each test case in this problem consists of T queries.

Constraints

1 \leq T \leq 300

1 \leq A, B, C, D \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

T
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_T B_T C_T D_T

In the i-th query, A = A_i, B = B_i, C = C_i, D = D_i.

Output

Print T lines. The i-th line should contain Yes if Snuke can buy apple juice indefinitely in the i-th query; No otherwise.

Sample Input 1

14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985

Sample Output 1

No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

In the first query, the number of cans of juice in stock changes as follows: (D represents daytime and N represents night.)

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 6
→D x

In the second query, the number of cans of juice in stock changes as follows:

9
→D 2
→N 11
→D 4
→N 13
→D 6
→N 15
→D 8
→N 8
→D 1
→N 10
→D 3
→N 12
→D 5
→N 14
→D 7
→N 7
→D 0
→N 9
→D 2
→N 11
→D …

and so on, thus Snuke can buy juice indefinitely.

Sample Input 2

24
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

Sample Output 2

No
No
No
No
No
No
Yes
Yes
No
No
No
No
Yes
Yes
Yes
No
No
No
Yes
Yes
Yes
No
No
No



Sample input:
14
9 7 5 9
9 7 6 9
14 10 7 12
14 10 8 12
14 10 9 12
14 10 7 11
14 10 8 11
14 10 9 11
9 10 5 10
10 10 5 10
11 10 5 10
16 10 5 10
1000000000000000000 17 14 999999999999999985
1000000000000000000 17 15 999999999999999985


Sample output:
No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
Yes
No
No
Yes

Submission status: Time Limit Exceeded

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

for _ in range(INT()):
    a, b, c, d = MAP()
    if a < b or d < b:
        No()
        continue
    stock = a
    S = set()
    while 1:
        if stock >= b ** 2:
            stock = (a - c) % b + c
            if stock <= c:
                stock += d
        if stock < b:
            No()
            break
        if stock in S:
            Yes()
            break
        else:
            S.add(stock)
        stock -= b
        if stock <= c:
            stock += d

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
for i in range(T):
    A, B, C, D = map(int, input().split())
    if A < B:
        print('No')
        continue
    if B > D:
        print('No')
        continue
    if C >= B - 1:
        print('Yes')
        continue
    A = A % B
    D = D % B
    first_A = A
    while True:
        if A > C:
            print('No')
            break
        A = (A + D) % B
        if A == first_A:
            print('Yes')
            break

Submission status: Time Limit Exceeded

Solution code:
def Yes():
  print("Yes")
def No():
  print("No")

def gcd(x, y):
  n = min(x, y)
  for i in range(n, 0, -1):
    if x % i == 0 and y % i == 0:
      return i

def lcm(x, y):
  return (x * y) // gcd(x,y)

def main():
  T = int(input())
  AD = [list(map(int, input().split())) for i in range(T)]

  for i in AD:
    A = i[0]
    B = i[1]
    C = i[2]
    D = i[3]
    if A < B:
      No()
      continue
    if B > D:
      No()
      continue
    add = D - B
    d = gcd(B, D)
    
    C += 1
    t = C % d
    k = A % d
    if k == t:
      LD = C
    elif k > t:
      LD = C + k - t
    else:
      LD = C + d - t + k
    if LD < B:
      No()
    else:
      Yes()

main()

Submission status: Time Limit Exceeded

Solution code:
t = int(input())

for i in range(t):
	a,b,c,d = map(int,input().split())
	e =[]
	if a >= b:
		a = a%b
		a += b
	if d >= b:
		d = d%b
		d += b
	while True:
		f = a-b
		if f < 0:
			print('No')
			break
		elif f in e:
			print('Yes')
			break
		else:
			e.append(f)
			if f <= c:
				a = f+d
			else:
				a = f

Submission status: Time Limit Exceeded

Solution code:
t = int(input())

def sub(a, b, c, d):
	values = [a]
	i = a
	while (True):
		i = i % b if i > b else i - b
		if i < 0:
			return 'No'
		if i <= c:
			i += d
		if i in values:
			return 'Yes'
		values.append(i)
	return 'No'

def main():
	for i in range(0, t):
		a, b, c, d = map(int, input().split(' '))
		print(sub(a, b, c, d))

main()

Submission status: Time Limit Exceeded

Solution code:
T = int(input())

for i in range(T):
    A, B, C, D = list(map(int, input().split()))
    L = []
    while True:
        if A < B:
            print('No')
            break
        A = A - B * (A // B)
        if A in L:
            print('Yes')
            break
        else:
            L.append(A)
        if A <= C:
            A += D

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
A = [list(map(int, input().split())) for _ in range(t)]
for i in range(t):
    state = True
    a,b,c,d = A[i]
    keep = []
    keep.append(a)
    if a < b:
        state = False
    else:
        while True:
            if a < b:
                state = False
                break
            a -= b
            if a > c:
                if (a-c)%b:
                    a -= (((a-c)//b) + 1)*b
                    if a < 0:
                        state = False
                        break
                else:
                    a = c
            a += d
            if keep.count(a) >= 2:
                temp1 = keep.index(a)
                temp2 = keep[temp1+1:].index(a) + temp1+1
                if keep[temp1:temp2] == keep[temp2:]:
                    break
            keep.append(a)
    if state:
        print('Yes')
    else:
        print('No')

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
A = [list(map(int, input().split())) for _ in range(t)]
for i in range(t):
    state = True
    a,b,c,d = A[i]
    keep = []
    keep.append(a)
    if a < b:
        state = False
    else:
        a -= b
        if a <= c:
            a += d
        keep.append(a)
        while True:
            if a < b:
                state = False
                break
            a -= b
            if a <= c:
                a += d
            if a in keep:
                if keep[keep.index(a)-1] == keep[-1]:
                    break
            keep.append(a)
    if state:
        print('Yes')
    else:
        print('No')

Submission status: Time Limit Exceeded

Solution code:
import sys

iT = int(input())
aR = [[int(x) for x in sLine.split()] for sLine in sys.stdin.readlines()]
#わからんから解説見た。解説見たけどgcdの下りが自分で導出できんかったのでとりあえずバカループでなんとかしてみた。これで通るかな。
def f調1(iA,iB,iC,iD):
    if iA < iB or iD < iB:
        return False
    if iC >= iB :
        return True
    iJ = iA % iB
    iK = iD % iB
    i = 1
    while True:
        j = iJ + i * iK
        if iB <= j:
            return True
        else:
            if iC < j:
                return False
        if iK == 0:
            return True
        i += 1
    return True

for eR in aR:
    if f調1(*eR) :
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
T=int(input().strip())
ABCD= [list(map(int, input().split())) for i in range(T)]
ans="No Yes".split()
for A,B,C,D in ABCD:
    if A<B or D<B:
        print("No")
        continue
    if B<=C:
        print("Yes")
        continue
    # B<=A, B<=D, B>C
    rest=set()
    while True:
        if A<B:
            print("No")
            break
        A=A%B
        if A in rest:
            print("Yes")
            break
        rest.add(A)
        if A<=C:
            A+=D

Submission status: Time Limit Exceeded

Solution code:
def solve(numbers):
	num=numbers[0]
	num-=numbers[1]
	if num<0:
		return 'No'
	if num<=numbers[2]:
		num+=numbers[3]
	while num != numbers[0]:
		num-=numbers[1]
		if num<0:
			break
		if num<=numbers[2]:
			num+=numbers[3]
	else:
		return 'Yes'
	return 'No'
	
n=int(input())
for i in range(n):
	print(solve(list(map(int,input().split()))))

Submission status: Time Limit Exceeded

Solution code:

for i in range(int(input())):
    temp=[]
    a,b,c,d = map(int,input().split())

    while(1):
        if a>=b:
            a=a-b
            if a in temp:
                flag=1
                break
            else:
                temp.append(a)

            if a<=c:
                a+=d
        else:
            flag=0
            break

    if flag==0:
        print('No')
    elif flag==1:
        print('Yes')

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8

def getInput():
    in_row = [ int(x) for x in input().split(" ") ]
    return in_row

def judgeTakahashi(in_row):
    A = in_row[0] # initial stock
    B = in_row[1] # initial Takahashi
    C = in_row[2]
    D = in_row[3]
    loop = []

    if A < B:
        return False

    # daytime - purchase
    while A not in loop:
        loop.append(A)
        A %= B

        # night - fill
        if A <= C:
            A += D

        if A < B:
            return False
    
    return True
    
    
if __name__ == "__main__":
    N = int(input())
    for i in range(N):
        in_row = getInput()
        result = "Yes" if judgeTakahashi(in_row) else "No"
        print(result)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
t = int(input())
a = []
b = []
c = []
d = []
for k in range(t):
    aa,bb,cc,dd = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)
    c.append(cc)
    d.append(dd)

aa = a
bb = b
cc = c
dd = d

def check(a,b,c,d):
    if a < b:
        ans = 'No'
        return ans
    if b > d:
        ans = 'No'
        return ans
    else:
        if c >= b:
            ans = 'Yes'
            return ans
    ans = 'Yes'
    rec = []
    for k in range(10000000000000):
        temp = int(np.mod(a + k * d,b))
        if temp > c:
            ans = 'No'
            break
        if temp in rec:
            break
        rec.append(temp)
    return ans

for a,b,c,d in zip(aa,bb,cc,dd):
    print(check(a,b,c,d))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
for i in range(N):
    A,B,C,D = list(map(int,input().split(" ")))
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a
    if(B>D or B>A):
        print("No")
    else:
        for i in range(B // gcd(B, D)):
            if(A+i*D)%B>C:
                print("No")
                break
        else:
            print("Yes")

Submission status: Time Limit Exceeded

Solution code:
T = int(input())

for i in range(T):
    a, b, c, d = map(int, input().split())
    used = []
    while True:
        if a < b:
            end = False
            break
        else:
            a %= b
            if a <= c:
                
                if a not in used:
                    used.append(a)
                else:
                    end = True
                    break
                a += d
                
            else:
                if a not in used:
                    used.append(a)
                else:
                    end = True
                    break
    
    if end:
        print('Yes')
    else:
        print('No')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
grid =[]

def checktrue(a, b, c, d):
    zaiko = set()
    zaiko.add(a)
    tmpzaiko = a
    plusD = d % b
    while True:
        if b > d or a < b:
            return "No"
        if b <= c:
            return "Yes"
        tmpzaiko %= b
        if tmpzaiko > c or tmpzaiko + d < b:
            return "No"
        else:
            tmpzaiko += plusD
            if tmpzaiko in zaiko:
                return "Yes"
            else:
                zaiko.add(tmpzaiko)
for el in range(n):
    array = list(map(int, input().split()))
    print(checktrue(array[0], array[1], array[2], array[3]))

Submission status: Time Limit Exceeded

Solution code:
T = int(input())

ABCD=[]
for _ in range(T):
    ABCD.append([int(x) for x in input().split()])


for A,B,C,D in ABCD:
    
    if A < B:
        #print("AB")
        print("No")
        continue
    
    if C >= B and B <= D:
        #print("CB")
        print("Yes")
        continue

    #a = (A-C)%B
    a = (((A - C)//B+1)*B - (A-C))%B

    l = []
    while True:
        #print(a)
        if a > C:
            print("No")
            break
        if (D-a)//B > 1:
            a = (D-a)%B
        else:
            a = B - (D-a)
            if a<0:
                a+=B
#        a = ((D-a)//B+1)*B - (D-a)
        if a in l:
            print("Yes")
            break
        l.append(a)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
if __name__ == "__main__":
    T = input().rstrip().split(' ')
    T = int(T[0])
    for t in range(T):
        line = input().rstrip().split(' ')
        A = int(line[0])
        B = int(line[1])
        C = int(line[2])
        D = int(line[3])
        A_list = []
        flag = True

        if B > A:
            print('No')
            continue
        if B > D:
            print('No')
            continue
        if C > B:
            print('Yes')
            continue

        while True:
            if A in A_list:
                flag = True
                break
            A_list.extend([A])
            if A < B:
                flag = False
                break
            q, A = divmod(A, B)
            for i in range(q-1):
                if A + B > C:
                    break
                A += B
            A_list.extend([A + B])
            if A <= C:
                A += D
        if flag:
            print('Yes')
        else:
            print('No')

Submission status: Time Limit Exceeded

Solution code:
def gcd(X,Y):
  if X < Y:
    tmp = X
    X = Y
    Y = tmp
  if Y == 0:
    return 0
  while X % Y != 0:
    r = X % Y
    X = Y
    Y = r
  return Y
T = int(raw_input())
Q = [map(int, raw_input().split()) for _ in range(T)]
for q in Q:
  A,B,C,D = q
  if B > A:
    print 'No'
  elif B > D:
    print 'No'
  elif B <= C:
    print 'Yes'
  else:
    A = A%B
    D = D%B
    g = gcd(B,D)
    maximum = A
    if g != 0:
      while maximum < B:
        maximum += g
      maximum -= g
    if maximum > C:
      print 'No'
    else:
      print 'Yes'

Submission status: Time Limit Exceeded

Solution code:
for _ in range(int(input())):
  a, b, c, d = map(int, input().split())

  delta = d - b

  if delta < 0:
    print("No")
    continue

  b2 = b - c

  if a < b:
    print("No")
    continue

  if b2 <= 1:
    print("Yes")
    continue

  pos = ((a-c)%b)

  s = set()
  s.add(pos)

  for _ in range(b):
    pos += delta
    pos = pos%b
    # print(pos)

    if pos > 0 and pos < b2:
      print("No")
      break

    # print(pos, delta)
    if pos in s:
      print("Yes")
      break

    s.add(pos)


  else:
    print("Yes")

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
a = []
for i in range(T):
    a.append(list(map(int, input().split())))
for r in a:
    s, b, c, d = r[0], r[1], r[2], r[3]
    mod_list = []
    if (s < b) or (b>d):
        print('No')
        continue
    if (c>b):
        print('Yes')
        continue
    while True:
        s = s%b
        if s > c:
            print('No')
            break
        s += d
        if s in mod_list:
            print('Yes')
            break
        mod_list.append(s)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    T = int(input())

    for num in range(T):
        array = list(map(int, input().split()))
        A = array[0]
        B = array[1]
        C = array[2]
        D = array[3]
        result = []

        if A < B or B > D or A % B > C:
            print("No")
            continue
        elif B == D:
            print("Yes")
            continue

        morning = A
        noon = morning % B

        while True:
            if noon <= C:
                night = noon + D
            else:
                night = noon

            if night - B < 0:
                print("No")
                break

            result.append(noon)
            morning = night
            noon = morning % B
            if noon in result:
                print("Yes")
                break
            if noon == morning:
                print("No")
                break

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    T = int(input())

    for num in range(T):
        array = list(map(int, input().split()))
        A = array[0]
        B = array[1]
        C = array[2]
        D = array[3]
        result = []

        if A < B or B > D or A % B > C:
            print("No")
            continue

        morning = A
        noon = morning % B

        while True:
            if noon <= C:
                night = noon + D
            else:
                night = noon

            if night - B < 0:
                print("No")
                break

            result.append(noon)
            morning = night
            noon = morning % B
            if noon in result:
                print("Yes")
                break
            if noon == morning:
                print("No")
                break

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
A = []
B = []
C = []
D = []
for _ in range(T):
        a,b,c,d = map(int,input().split())
        A.append(a)
        B.append(b)
        C.append(c)
        D.append(d)

for i in range(T):
        if A[i]<B[i] or D[i]<B[i]:
                print("No")
                continue
        tmp = []
        Amod = A[i]%B[i]
        Dmod = D[i]%B[i]
        if Dmod == 0:
                Dmod += D[i]
        while True:
                dNum = (((C[i]-Amod)//Dmod)+1)*(Dmod)+Amod
                if dNum < B[i]:
                        print("No")
                        break
                elif dNum in tmp:
                        print("Yes")
                        break
                else:
                        tmp.append(dNum)
                        Amod = dNum%B[i]

Submission status: Time Limit Exceeded

Solution code:
t=int(input())
query=[]
for i in range(t):
    query.append(list(map(int,input().split())))



def cal(a,b,c,d,cnt):
    if cnt%2==0:
        a=a-b
    else:
        if a<=c:
            a=a+d
    return a

yn=[]
list_a=[[]for i in range(t)]
for i in range(t):
    a=query[i][0]
    list_a[i].append(a)
    cnt=0
    cnt2=0
    while 1:
        a=cal(a,query[i][1],query[i][2],query[i][3],cnt)
        if a<0:
            yn.append(0)
            break
        

        if cnt%2==0: 
            cnt2+=1
            list_a[i].append(a)
            if a in list_a[i][:cnt2]:
                yn.append(1)
                break
        cnt+=1
        
for i in range(t):
    if yn[i]==0:
        print('No')
    elif yn[i]==1:
        print('Yes')

Submission status: Time Limit Exceeded

Solution code:
def gcd (a,b):
	if a < b:
		a,b = b,a
	return a if b == 0 else gcd(b,a%b)
T = int(input())
for i in range(T):
	A,B,C,D = map(int,input().split(" "))
	if A < B:
		print("No")
		continue

	if D < B:
		print("No")
		continue

	if A > C:
		init = C + ((A - C) % B)
		if init != C:
			init -= B
		if init < 0:
			print("No")
			continue
	else:
		init = A

	if C >= (B-1):
		print("Yes")
		continue

	dif = (D - B)
	if dif == 0:
		print("Yes")
		continue
	i = C - ((C - init) % dif)
	m = B % dif

	k = 0
	flg = False
	while m * k < dif:
		if i + dif - m*k < B:
			print("No")
			flg = True
			break
		k += 1
	if not flg:
		print("Yes")

Submission status: Time Limit Exceeded

Solution code:
def judge(a,b,c,d):
    #calc border
    if a-c<0:
        border = 0
    else:
        if (a-c)%b == 0:
            border = int((a-c)/b)
        else:           
            border = int((a-c)/b)+1

    start = a-border*b

    return start
    
def check(a,b,c,d):
    if a-b<0:
        return False
    if d<b:
        return False
    if c>=b and d>=b:
        return True
#    if b%2==0 and d%2==0:
#        if (c-b+2)<0:
#            return False
#    elif (c-b+1)<0:
#        return False
    
    ans = judge(a,b,c,d)
    b_list = []
    while not ans in b_list:
        b_list.append(ans)
        if ans <= c:
            a = ans+d
        else:
            a = ans
        if a-b<0:
            return False

        ans = judge(a,b,c,d)
        if ans < 0:
            return False
    
    return True
  
N = int(input())
ABCD = []
for i in range(N):
    ABCD.append(map(int, input().split()))


for a,b,c,d in ABCD:
    if check(a,b,c,d):
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
T=input()
for i in range(T):
	A,B,C,D=map(int,raw_input().split())
	if A<B:
		print "No"
	elif B>D:
		print "No"
	elif abs(B-C)==1:
		print "Yes"
	else:
		A=A%B

		while A<=B:
			if (D-B)%B==0:
				if A>C and A<B:
					print "No"
					break
				else:
					print "Yes"
					break
			elif (D-B)%B==1:
					print "No"
					break

			A+=(D-B)%B
			if A>C and A<B:
				print "No"
				break
		else:
			print "Yes"

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
t=[list(map(int,input().split()))for i in range(n)]
for i in range(len(t)):
    x=t[i]
    b=[]
    g=False
    if x[1]>x[3] or x[0]<x[1]:print("No");continue
    while 1:
        if x[0]<x[1]:print("No");break
        l=len(b)
        b = list(set(b+[x[0]]))
        x[0]%=x[1]
        if len(b)==l and g:print("Yes");break
        g=True
        if x[0]<=x[2]:x[0]+=x[3]

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
a = []
for i in range(T):
    a.append(list(map(int, input().split())))
for r in a:
    s, b, c, d = r[0], r[1], r[2], r[3]
    s_list = []
    while True:
        if (s < b) or (b>d):
            print('No')
            break
        s = s%b
        s_list.append(s)
        if c < s < b:
            print('No')
            break

        s = s+d
        if s in s_list:
            print('Yes')
            break
        s_list.append(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
if __name__ == "__main__":
    T = input().rstrip().split(' ')
    T = int(T[0])
    for t in range(T):
        line = input().rstrip().split(' ')
        A = int(line[0])
        B = int(line[1])
        C = int(line[2])
        D = int(line[3])
        A_list = []
        flag = True
        for i in range(5000):
            if A in A_list:
                flag = True
                break
            A_list.extend([A])
            if A < B:
                flag = False
                break
            q, A = divmod(A, B)
            for i in range(q-1):
                if A + B > C:
                    break
                A += B
            A_list.extend([A + B])
            if A <= C:
                A = A + D
        if flag:
            print('Yes')
        else:
            print('No')

Submission status: Time Limit Exceeded

Solution code:
from math import ceil
from collections import defaultdict
t = int(input())

# (b,c,d) -> {A -> No/Yes, ...}
memo = defaultdict(dict)
for i in range(t):
    a,b,c,d = map(int, input().split())

    if a < b:
        print('No')
        continue

    if d >= b and c+1 >= b:
        print('Yes')
        continue

    key = (b,c,d)
    if key in memo:
        for A, ans in memo[key].items():
            #print(a,A)
            if a in A:
                print(ans)
                continue

    A = set([a])
    while(True):
        if (a-c)%b == 0:
            x = (a-c)//b
        else:
            x = ceil((a-c)/b)
        if a-b*x < 0:
            print('No')
            memo[key][frozenset(A)] = 'No'
            break
        next_a = a-b*x+d
        if next_a < b:
            print('No')
            memo[key][frozenset(A)] = 'No'
            break
        if next_a in A:
            print('Yes')
            memo[key][frozenset(A)] = 'Yes'
            break
        A.add(next_a)
        if len(A) >= d:
            print('No')
            memo[key][frozenset(A)] = 'No'
            break
        a = next_a

Submission status: Time Limit Exceeded

Solution code:



aa=int(input())
N=[0 for i in range(aa)]
for i in range(aa):
  bb=input()
  #print(b)
  N[i]=bb.split()


def so(p,q):
  while p!=q:
    if p>q:
      p-=q
    else:
      q-=p
  return p


#print(so(11,5))

def aaa(n):

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

  e=a-a//b*b
  f=d-d//b*b
  
  if e*f>0 :
    s=so(e,f)
  else :
    s=f


  if a<b or d<b :
    return "No"
  
  elif b<=c :
    return "Yes"
  else:
  
    if f!=0 :
      ss=b//s*s
      if ss==b:
        ss-=s

      if ss>c:
        return "No"
          
    

        #print(e)
        #print(f)
  #print("while")
    else:
      if e>c :
        return "No"

  return "Yes"

for i in range(aa):
  print(aaa(i))

Submission status: Time Limit Exceeded

Solution code:
T=input()
for i in range(T):
	A,B,C,D=map(int,raw_input().split())
	if A<B:
		print "No"
	elif B>D:
		print "No"
	else:
		A=A%B

		while A<=B:
			if (D-B)%B==0:
				if A>C and A<B:
					print "No"
					break
				else:
					print "Yes"
					break

			A+=(D-B)%B
			if A>C and A<B:
				print "No"
				break
		else:
			print "Yes"

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

# stdin = open('sample.txt')

def spam(A, B, C, D):
    if A < B:
            print('No')
            return

    a = A % B
    for i in range(500000):
        if a <= C:
            a += D
        if a < B:
            print('No')
            return
        a -= B
    print('Yes')


def main():
    T = int(stdin.readline().rstrip())
    for _ in range(T):
        A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]
        spam(A, B, C, D)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  t = int(input())
  for _ in range(t):
    a, b, c, d = map(int, input().split())
    if (a < b or d < b):
      print("No")
      continue
    if (c >= b - 1):
      print("Yes")
      continue
  
    a %= b
    if a > c:
      print("No")
      continue
    d %= b
    if d == 0:
      print("Yes")
      continue
    if b % d != 0:
      print("No")
      continue
    for i in range(b // d):
      a = (a + d) % b
      if a > c:
        print("No")
        break
    else:
      print("Yes")
main()

Submission status: Time Limit Exceeded

Solution code:
import math
t = int(input())
for i in range(t):
  a,b,c,d = [int(x) for x in input().split()]
  if a < b or b > d:
    print("No")
    continue
  elif b == d:
    if a%b <= c:
      print("Yes")
    else:
      print("No")
    continue
  else:
    if b <= c:
      print("Yes")
      continue
    else:
      l =[]
      tmp = a
      buf = math.ceil(c/b)*b-b
      first = tmp % b + buf
      while True:
        payoff = tmp % b + buf
        if payoff > c:
          print("No")
          break
        if payoff in l or payoff < first:
          print("Yes")
          break
        else:
          l.append(payoff)
        tmp = payoff + d

Submission status: Time Limit Exceeded

Solution code:
t=int(input())
for i in range(t):
    a,b,c,d = list(map(int,input().split()))
    st = []
    if a-b > 0 and c>=b and d >=b:
        print('Yes')
        continue
        
    if a > c*100:    
        if a-b > c:
            a = a%b
            while a<=c:
                a+=b
    if d > c*100:
        if d-b>c:
            d = d%b
            while d<=c:
                d+=b

    while True:
        if a-b < 0:
            print('No')
            break
        
        a -= b
        if a <= c:
            a += d
        if a in st:
            print('Yes')
            break
        st.append(a)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

# stdin = open('sample.txt')

def spam(A, B, C, D):
    if A < B:
            print('No')
            return

    a = A % B
    for i in range(1000000):
        if a <= C:
            a += D
        if a < B:
            print('No')
            return
        a -= B
    print('Yes')


def main():
    T = int(stdin.readline().rstrip())
    for _ in range(T):
        A, B, C, D = [int(x) for x in stdin.readline().rstrip().split()]
        spam(A, B, C, D)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def check(a,b,c,d):
	if a < b:
		print('No')
		return True
	if d < b:
		print('No')
		return True
	return False

T = int(input())
query = []
for i in range(T):
	query.append(list(map(int, input().split())))

for q in query:
	flg = True
	a = q[0]
	b = q[1]
	c = q[2]
	d = q[3]

	if check(a,b,c,d):
		continue

	if a % b > c % b:
		p = a % b + (c % b) * b
	else:
		p = a % b + (c % b - 1) * b

	if d % b > c % b:
		r = d % b + (c % b) * b
	else:
		r = d % b + (c % b - 1) * b

	pre = -1
	for i in range(b):
		tmp = (p + i*r) % b
		if pre == tmp:
			break
		if tmp > c:
			print('No')
			flg = False
			break
		pre = tmp
	if flg:
		print('Yes')

Submission status: Time Limit Exceeded

Solution code:
T = int(input())

for i in range(T):
  a, b, c, d = map(int, input().split())

  #dがbより小さければ減り続けるのでNo
  #aがbより小さければいきなり買えない
  if d<b or a<b:
    print('No')
    continue

  #cがb-1以上だと足りないときは常に補ってくれる
  if b-1<=c:
    print('Yes')
    continue
  
  #d補充した分からbずつ買って余りが出る、かつそれがアンラッキーゾーンより狭いと必ず買えない日が出てくる
  if d%b<b-c and d%b!=0:
    print('No')
    continue

  #在庫がc以上を保った最小の
  for j in range(c):
    if a>c:
      a = c+(a-c)%b
    if a>c and a<b:
      print('No')
      break
    else:
      if a==c:
        a = a-b
    if d%b!=0:
      if b%(d%b)==0 and b-(b-a)%(d%b)<=c:
        print('Yes')
        break
    a+=d
    if j==c-1:
      print('Yes')

Submission status: Time Limit Exceeded

Solution code:
import math

n = int(input())
a = [0]*n
for i in range(n):
	a[i] = list(map(int, input().split()))

for i in range(n):
	A, B, C, D = a[i]
	if D < B:
		print('No')
		continue
	if A < B:
		print('No')
		continue
	if B - C < 2 and B - C >= 0:
		print('Yes')
		continue

	f = False
	A = A % B
	if A < C:
		A += B*math.floor(C/B)

	D = D % B
	if D < C:
		D += B*math.floor(C/B)

	init = A
	while True:
		if A <= C:
			A += D
		else:
			A -= B

		if A < 0:
			print('No')
			break

		if init == A:
			print('Yes')
			break

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
for i in range(T):
    ans = "Yes"
    a, b, c, d = map(int, input().split())
    if b > a or b > d:
        ans = "No"
    hoge = []
    while(len(hoge) == len(set(hoge))):
        a = a % b
        if a > c:
            ans = "No"
            break
        a += d
        hoge.append(a)
    print(ans)