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:
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
      first = tmp % b + math.ceil(c/b)*b-b
      while True:
        payoff = tmp % b + math.ceil(c/b)*b-b
        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:
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 = False 
        for i in range(A + D):
            if A in A_list:
                flag = True
                break
            A_list.extend([A])
            if A < B:
                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:
t=int(input())
for i in range(t):
    a,b,c,d=map(int,input().split())
    if a<b:
        print('No')
    else:
        aa=a
        flag=0
        cnt=(aa-c)//b
        aa=aa-b*(cnt-1)
        while flag==0:
            aa=aa-b
            if aa>c and aa<b:
                flag+=1
            elif aa>c:
                pass
            else:
                aa+=d
                if aa==a:
                    flag=flag-1
                else:
                    pass
        if flag==1:
            print('No')
        else:
            print('Yes')

Submission status: Time Limit Exceeded

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

def judge(l):
    A = l[0]
    B = l[1]
    C = l[2]
    D = l[3]
    init = set()
    while(1):
        if A-B < 0:
            return 'No'
        else:
            i=A%B
            if i > C:
                return 'No'
        j=0
        while(B*j+i>C):
            j=j+1
        rest = B*(j)+i+D
        A = rest
        if A in set(init):
            break
        else:
            init.add(A)
    #         rest = A-B*(i+1)
    #         rest=rest+D
    #     A=rest
    return 'Yes'

ans = list(map(judge,a))
for a in ans:
    print(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
    # elif B <= C:
    #     print('Yes')
    #     return

    a = A % B
    for i in range(100000):
        if a <= C:
            a += D
        if a < B:
            print('No')
            return
        a = 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:
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
      while True:
        payoff = tmp % b + math.ceil(c/b)*b-b
        if payoff > c:
          print("No")
          break
        if payoff in l:
          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 = [int(i) for i in input().split()]

    memo = set()
    while True:
        if A in memo:
            print("Yes")
            break

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

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

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

        n = 1 + (C - A + B) // (D - B)
        r = (A - B) + n * (D - B)
        if r < B:
            print("No")
            break
        else:
            memo.add(A)
            A = B + r % B

    else:
        print("Yes")

    # print(A, B, C, D)

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
    # elif B <= C:
    #     print('Yes')
    #     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

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

Submission status: Time Limit Exceeded

Solution code:
from math import ceil
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 and c+1 >= b:
        print('Yes')
        continue

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li=[]
re=[]
for i in range(n):
	li.append(list(map(int,input().split())))
	if li[i][0] < li[i][1]:
		re.append("No")
	elif li[i][1] > li[i][3]:
		re.append("No")
	elif li[i][2] >=li[i][1]:
		re.append("Yes")
	else:
		Lim=li[i][0]%li[i][1]
		a=li[i][3]-li[i][1]
		if a == 0:
			pass
		else:
			while Lim+a<li[i][1]:
				Lim=Lim+a
		if li[i][2] >= Lim:
			re.append("Yes")
		else:
			re.append("No")
for value in re:
 print(value)

Submission status: Time Limit Exceeded

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

for i in range(T):
    A = a[i][0]
    B = a[i][1]
    C = a[i][2]
    D = a[i][3]
    sa = D - B

    kei = A
    while kei - B <= C:
        kei += sa
    
    if kei - B < B:
        print('No')
    else:
        print('Yes')

Submission status: Time Limit Exceeded

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

def judge(l):
    A = l[0]
    B = l[1]
    C = l[2]
    D = l[3]
    init = []
    while(1):
        if A-B < 0:
            return 'No'
        else:
            i=A%B
            if i > C:
                return 'No'
        j=0
        j = C//B
        while(B*j+i>C):
            j=j+1
        if j-1<0:
            j=1
        rest = B*(j-1)+i+D
        A = rest
        if A in set(init):
            break
        else:
            init.append(A)
    #         rest = A-B*(i+1)
    #         rest=rest+D
    #     A=rest
    return 'Yes'

ans = list(map(judge,a))
for a in ans:
    print(a)

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"
        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())
a = [0 for i in range(t)]
b = [0 for i in range(t)]
c = [0 for i in range(t)]
d = [0 for i in range(t)]
for i in range(t):
    a[i], b[i], c[i], d[i] = map(int, input().split())
for i in range(t):
    count = 0
    if a[i] - b[i] > c[i]:
        tmp = a[i] - b[i] * (a[i] // b[i] - 1)
    else:
        tmp = a[i]
    m_set = set()
    while count < 10000000:
        m_set.add(tmp)
        if tmp < b[i]:
            print('No')
            break
        tmp -= b[i]
        if tmp <= c[i]:
            tmp += d[i]
            if tmp - b[i] > c[i]:
                tmp = tmp - b[i] * (tmp // b[i] - 1)
        s1 = set()
        s1.add(tmp)
        if s1.issubset(m_set):
            print('Yes')
            break
        # print(tmp)
        count += 1
    if count == 10000000:
        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
    
    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 = int(input())
for i in range(t):
	a,b,c,d = map(int,input().split())
	if a < b:
		print('No')
	else:
		a = a%b
		if d-b >= 0:
			add = (d-b)%b
		else:
			add = d-b
		check = [a]
		s = True
		before = -1
		while s:
			if a == before:
				print('Yes')
				s = False
			else:
				before = a
			if add >= 0:
				if add >= c:
					print('Yes')
					s = False
				elif c >= b:
					print('Yes')
					s = False
				elif a+add <= c:
					if a+add in check:
						print('Yes')
						s = False
					else:
						a = a+add*((c-a)//add+1)-b
						if a<0:
							print('No')
							s = False
				else:
					a = a+add-b
					if a < 0:
						print('No')
						s = False
					if a+add in check:
						print('Yes')
						s = False
					else:
						check.append(a+add)
						a = a+add
			else:
				print('No')
				s = False

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
        if B[i]<C[i]:
                print("Yes")
                continue
        k = 0
        tmp = []
        while True:
                dNum = (A[i] + k * D[i]) % B[i]
                if C[i] < dNum:
                        print("No")
                        break
                elif dNum in tmp:
                        print("Yes")
                        break
                else:
                        tmp.append(dNum)
                        k += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li=[]
re=[]
for i in range(n):
	li.append(list(map(int,input().split())))
	if li[i][0] < li[i][1]:
		 re.append("No")
	elif li[i][1] > li[i][3]:
		 re.append("No")
	else:
		Lim=li[i][0]%li[i][1]
		a=li[i][3]-li[i][1]
		if a == 0:
			pass
		else:
			while Lim+a<li[i][1]:
				Lim=Lim+a
		if li[i][2] >= Lim:
			re.append("Yes")
		else:
			re.append("No")
for value in re:
 print(value)

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=x[0]%x[1]
    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
        x[0]%=x[1]
        if x[0]==b and g:print("Yes");break
        g=True
        if x[0]<=x[2]:x[0]+=x[3]

Submission status: Time Limit Exceeded

Solution code:
def check(a, b, c, d):
    before = a - (a//b-1) * b
    diff = d - b

    if diff == 0 and a%b<=c:
        return "Yes"
    
    if diff <= 0 or a < b:
        return "No"

    for c_t in range(c+1, b):
        if (c_t + b - before) % diff == 0:
            # print("c_t:", c_t)
            return "No"
        
    else:
        return "Yes"

n = int(input())
A, B, C, D = [], [], [], []
for _ in range(n):
    a,b,c,d = list(map(int, input().split()))
    A.append(a)
    B.append(b)
    C.append(c)
    D.append(d)
    
for i in range(n):
    print(check(A[i], B[i], C[i], D[i]))

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])
        init_A = A
        A_list = []
        flag = False
        while True:
            if A in A_list:
                flag = True
                break
            A_list.extend([A])
            if len(A_list) > 5000:
                flag = True
                break
            if A < B:
                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:
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])
        init_A = A
        A_list = []
        flag = False
        while True:
            if A in A_list:
                flag = True
                break
            A_list.extend([A])
            if A < B:
                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:
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"
        tmpzaiko %= b
        if tmpzaiko > c:
            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:
n = int(input())
li=[]
re=[]
for i in range(n):
	li.append(list(map(int,input().split())))
	if li[i][0] >= li[i][1] and li[i][1] <= li[i][3]:
		Lim=li[i][0]%li[i][1]
		if li[i][1] == li[i][3]:
			pass
		else:
			while Lim+li[i][3]-li[i][1]<li[i][1]:
				Lim=Lim+li[i][3]-li[i][1]
		if li[i][2] >= Lim:
			re.append("Yes")
		else:
			re.append("No")
	else:
		re.append("No")
for value in re:
 print(value)

Submission status: Time Limit Exceeded

Solution code:
#
import sys
import math
import numpy as np
import itertools

n = int(input())
a=[]
b=[]
c=[]
d=[]
for j in range(0,n):
    t1,t2,t3,t4 = (int(i) for i in input().split())  
    a.append(t1)
    b.append(t2)
    c.append(t3)
    d.append(t4)
    
for i in range(0,n):
    if a[i] < b[i]:
        print("No")
    elif d[i] < b[i]:
        print("No")
    else:
        flag=1
        for j in range(0,b[i]):
            p= (a[i]+j*d[i]) % b[i]
            if p==0:
                break
            elif p > c[i]:
                flag=0
                break
        if flag == 1:
            print("Yes")
        else:
            print("No")

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
T = int(input())
for i in range(T):
    a,b,c,d = map(int,input().split())
    if b > d:
        print("No")
    elif a < b:
        print("No")
    else:
        if c >= b:
            print("Yes")
        else:
            uni = d//b
            sur = a % b
            while sur != a:
                if (a-d) % b > a % b:
                    a += uni * d
                else:
                    a += d
                if c < a % b:
                    print("No")
                    break
                a %= b
            else:
                print("Yes")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


def slv3(A, B, C, D):
    # error_print(A, B, C, D)
    if A < B:
        # error_print('A')
        return 'No'

    if D < B:
        # error_print('D')
        return 'No'

    if C >= B:
        return 'Yes'

    s = set([])
    while True:
        # error_print(A)
        if A in s:
            error_print('Yes')
            return 'Yes'
        s.add(A)
        if A < B:
            error_print('No')
            return 'No'

        A %= B
        if A <= C:
            A += D

    return 'Yes'


def slv2(A, B, C, D):
        # error_print(A, B, C, D)
    if A < B:
        # error_print('A')
        return 'No'

    if D < B:
        # error_print('D')
        return 'No'

    if C >= B:
        return 'Yes'

    s = set([])
    while True:
        # print(A)
        if A in s:
            # error_print('Yes')
            return 'Yes'
        s.add(A)
        if A < B:
            # error_print('No')
            return 'No'

        A %= B
        # m = D % B
        # if m == 0:
        #     m = D
        # print('--', math.ceil((C-A) / D), C-A)
        A += int(math.floor((C-A) / D) + 1) * D
    return 'Yes'


@mt
def slv(T, Q):
    a = []
    for q in Q:
        a.append(slv2(*q))

    return '\n'.join(a)


def main():

    T = read_int()
    Q = [read_int_n() for _ in range(T)]
    print(slv(T, Q))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def divisor(b):
  lst = []
  init = 2
  while b > 1:
    for i in range(init, int(b ** (1 / 2)) + 1):
      if b % 2 == 0:
        lst.append(i)
        lst.append(b // i)
        b //= i
        init = i
    else:
      break
  return set(lst)
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):
      print("Yes")
      continue
  
    a %= b
    if a > c:
      print("No")
      continue
    d %= b
    lst = defaultdict(bool)
    while True:
      a %= b
      if a > c:
        print("No")
        break
      if lst[a]:
        print("Yes")
        break
      lst[a] = True
      a += d
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
t=int(input())
for _ in range(t):
    memo=defaultdict(int)
    a,b,c,d=map(int,input().split())
    if b>d or a<b:
        print("No")
    else:
        cur=a
        while True:
            if cur<c:
                cur-=b
            elif (cur-c)%b==0:
                cur=c
            else:
                cur=cur-((cur-c)//b)*b-b
            if cur<0:
                print("No")
                break
            if memo[cur]==0:
                memo[cur]+=1
            else:
                print("Yes")
                break
            if cur<=c:
                cur+=d

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])
        init_A = A
        A_list = []
        flag = False
        while True:
            if A in A_list:
                flag = True
                break
            A_list.append(A)
            if len(A_list) > 10000:
                flag = True
                break
            if A < B:
                break
            q, A = divmod(A, B)
            for i in range(q-1):
                if A + B > C:
                    break
                A += B
            A_list.append(A + B)
            if A <= C:
                A = A + D
        if flag:
            print('Yes')
        else:
            print('No')

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
        k = 0
        tmp = []
        while True:
                dNum = (A[i] + k * D[i]) % B[i]
                if C[i] < dNum:
                        print("No")
                        break
                elif dNum in tmp:
                        print("Yes")
                        break
                else:
                        tmp.append(dNum)
                        k += 1

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li=[]
re=[]
for i in range(n):
	li.append(list(map(int,input().split())))
for i in range(n):
	if li[i][0] >= li[i][1] and li[i][1] <= li[i][3]:
		Lim=li[i][0]%li[i][1]
		if li[i][1] == li[i][3]:
			pass
		else:
			while Lim+li[i][3]-li[i][1]<li[i][1]:
				Lim=Lim+li[i][3]-li[i][1]
		if li[i][2] >= Lim:
			re.append("Yes")
		else:
			re.append("No")
	else:
		re.append("No")
for value in re:
 print(value)

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
a = []
for i in range(t):
  a.append(list(map(int,input().split())))
for j in a:
  b = j[0]
  for k in range(j[1]):
    if j[0] < j[1]:
      print("No")
      break
    j[0] %= j[1]
    if j[0] <= j[2]:
      j[0] += j[3]
    else:
      print("No")
      break
    if j[0] < j[1]:
      print("No")
      break
    if k == j[1]-1:
      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())
  if d<b or a<b:
    print('No')
    continue
  if b-1<=c:
    print('Yes')
    continue
  if d%b<b-c and d%b!=0:
    print('No')
    continue
  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
    a+=d
    if j==c-1:
      print('Yes')

Submission status: Time Limit Exceeded

Solution code:
class query:
    def __init__(self, a, b, c, d):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
    def isEternal(self):
        if self.a < self.b:
            return "No"
        if self.a % self.b > self.c:
            return "No"
        if self.d < self.b:
            return "No"
        d = self.b - self.c
        if d <= 0:
            return "Yes"
        if self.b % d == 0 and self.d % d == 0:
            return "Yes"
        s = self.a % self.b
        db = self.d % self.b
        if db == 0:
            return "Yes"
        for i in range(self.b - 1):
            s += db
            if s >= self.b:
                return "Yes"
            if s > self.c:
                return "No"
        return "Yes"

if __name__ == "__main__":
    t = int(input())
    querys = [query(*[int(s) for s in input().split()]) for _ in range(t)]
    for q in querys:
        print(q.isEternal())

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
x = []
for i in range(N):
    a1,b1,c1,d1=[int(i) for i in input().split()]
    x.append([a1,b1,c1,d1])
for i in range(N):
  y = x[i]
  if y[0] < y[1]:
    print("No")
  elif y[1] > y[3]:
    print("No")
  elif y[1] > y[2]:
    ama = y[0] % y[1]
    mo = y[3] %  y[1]
    if mo == 0:
      if ama<= y[2]:
        print("Yes")
      else:
          print("No")
    elif mo <= y[1]-y[2]-1:
      print("No")
    else:
        to = y[1] % mo
        if to == 0:
          if ama<= y[2]:
            print("Yes")
          else:
              print("No")
        else:
          i = 0
          while True:
            if i >= to:
              print("Yes")
              break
            ama = (ama +y[1])%mo
            if ama <= y[1]-y[2]-1:
              print("No")
              break
            i+=1
  else:
    print("Yes")

Submission status: Time Limit Exceeded

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

from math import ceil

def solve():
    n = int(input())
    al = [[int(i) for i in input().split()] for _ in range(n)]
    ans = [None]*n
    for i in range(n):
        l = set([])
        a, b, c, d = al[i]
        if (b / d) > 1:
            ans[i] = 'No'
            continue
        if a <= c:
            if a < b:
                ans[i] = 'No'
                continue
            a = (a - b) + d + ceil(((a - b) + d) / c)
        while True:
            f = ceil((a / b) - (c / b))
            if f <= 0:
                ans[i] = 'No'
                break
            t = a - b * f
            if t > c:
                ff = ceil((t / b) - (c / b))
                t = t - b * ff
            if t < 0:
                ans[i] = 'No'
                break
            if t in l:
                ans[i] = 'Yes'
                break
            l.add(t)
            a = t + d
    print('\n'.join(ans))

if __name__=="__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
#
import sys
import math
import numpy as np
import itertools

n = int(input())
a=[]
b=[]
c=[]
d=[]
for j in range(0,n):
    t1,t2,t3,t4 = (int(i) for i in input().split())  
    a.append(t1)
    b.append(t2)
    c.append(t3)
    d.append(t4)
    
for i in range(0,n):
    if a[i] < b[i]:
        print("No")
    elif d[i] < b[i]:
        print("No")
    else:
        flag=1
        for j in range(0,b[i]):
            p= (a[i]+j*d[i]) % b[i]
            if p > c[i] and p != 0 :
                flag=0
                break
        if flag == 1:
            print("Yes")
        else:
            print("No")