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:
T = int(input())
a = []
for i in range(T):
    a.append(list(map(int, input().split())))
for r in a:
    a, b, c, d = r[0], r[1], r[2], r[3]
    s = a


    s_list = []
    while True:
        s_list.append(s)


        if s > 100:
            s -= b*((s//b)-1)
            while s <= c:
                s+= c
        s -= b
        if s in s_list:
            print('Yes')
            break
        s_list.append(s)


        if s <0:
            print('No')
            break
        if s != s_list[-1]:
            if s in s_list:
                print('Yes')
                break

        if s <= c:
            s += d
            s_list.append(s)
            if s != s_list[-1]:
                if s in s_list:
                    print('Yes')
                    break

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def check_unique(list_tmp):
    L1 = len(list_tmp)
    L2 = len(list(set(list_tmp)))
    if L1 == L2:
        return True
    else:
        return False

def test(A,B,C,D):
    bottles = A
    bottles_log = []
    while bottles >0:
        bottles_log.append(bottles)
        #print(bottles)
        
        if check_unique(bottles_log):
            if bottles >= B:
                bottles = bottles -(bottles//B) * B
                if bottles <= C :
                    bottles += D 
            else:
                return("No")
            
        else:
            return("Yes")
        
    return("No")

for i in range (N):
    A,B,C,D =  map(int,input().split())
    print(test(A,B,C,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 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
  visited=[]
  while True:
    if a>c:
      a = c+(a-c)%b
    if a>c and a<b:
      print('No')
      break
    elif a == c:
      if c in visited:
        print('Yes')
        break
      visited.append(a)
    else:
      a = a-b
      if a in visited:
        print('Yes')
        break
      visited.append(a)
    a+=d

Submission status: Time Limit Exceeded

Solution code:
t=int(input())
for _ in range(t):
    a,b,c,d=map(int,input().split())
    e=b*(max((a-c)//b,1))
    a-=e
    while a>c:
        a-=b
    if a<0:
        print('No')
        continue
    if d<b:
        print('No')
        continue
    if c<=b and d>b:
        """
        while a<=c:
            a+=(d-b)
        """
        a+=(d-b)*(c//(d-b))
        while a<=c:
            a+=(d-b)
        while a-(d-b)>c:
            a-=(d-b)
        if a<b:
            print('No')
            continue
    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 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 = init
	while True:
		k = C - ((C - i)%dif)
		if k + dif < B:
			print("No")
			break
		l = B - ((B - i)%dif)
		i = (l + dif)%B
		if (init - i) % dif == 0:
			print("Yes")
			break

	#print(B//dif,C // dif)

Submission status: Time Limit Exceeded

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

from math import ceil, floor

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

if __name__=="__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
T = int(input())

for i in range(T):
  apple = list(map(int, input().split(" ")))
  if apple[0] < apple[1]:
    print("No")
  elif apple[1] > apple[3]:
    print("No")
  else:
    A = apple[0] % apple[1]
    B = apple[3] % apple[1]
    if A > apple[2]:
      print("No")
    elif B == 0:
      if A <= apple[2]:
        print("Yes")
      else:
        print("No")
    else:
      flag = 0
      while A < apple[1]:
        if A > apple[2]:
          flag = 1
        A += B 
      if flag == 1:
        print("No")
      else:
        print("Yes")

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
def upfunc(b,c,d,x):
    if (x-b-c)%b==0:
        return(b+c)
    else:
        return((x-b-c)%b + c)
def downfunc(b,c,d,x):
    if b==d:
        return(x)
    else:
        if (x-b-c)%(d-b) != 0:
            return((x-b-c)%(d-b) + b+c)
        else:
            return(d+c)
def simulate(b,c,d,x):
    if x>b+c:
        return(upfunc(b,c,d,x))
    else:
        return(downfunc(b,c,d,x))
def solve(a,b,c,d):
    if b>d:
        return("No")
    if b>a:
        return("No")
    if b<=c:
        return("Yes")
    appear = set()
    appear.add(a)
    while True:
        a = simulate(b,c,d,a)
        if a < b:
            return("No")
        if a in appear:
            return("Yes")
        appear.add(a)
for _ in range(N):
    a,b,c,d=map(int,input().split())
    print(solve(a,b,c,d))

Submission status: Time Limit Exceeded

Solution code:
t = input()
result = []
hist = []
for i in range(int(t)):
	data = input().split()
	a = int(data[0])
	b = int(data[1])
	c = int(data[2])
	d = int(data[3])
	hist.clear()
	while True:
		if a < b:
			result.append("No")
			break
		n = int((a / b))
		a -= b * n
		if a <= c:
			a += d
		if a in hist:
			result.append("Yes")
			break
		else:
			hist.append(a)
			if len(hist) < 20:
				if min(hist) > a:
					result.append("No")
					break
		if (len(hist) == 5) & (b < d) & (b < c):
			result.append("Yes")
			break
for i in range(int(t)):
	print(result[i])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from copy import deepcopy

ans=("")
t=int(input())
h=[]
for i in range(t):
    w=list(map(int,input().split()))
    h.append(w)
for i in range(t):
    a=h[i][0]
    b=h[i][1]
    c=h[i][2]
    d=h[i][3]
    f=a-b
    if(f<0):
        print("No")
        continue
    if(b==d):
        if(f>c):print("No")
        else:print("Yes")
        continue
    if(d-b==c):
        print("Yes")
        continue
    if(d-b>c):
        if(a-d<=c):
            print("Yes")
            continue
        else:
            print("No")
            continue   
    while(1):
        f+=d-b
        if(d-b<0):
            print("No")
            break
        if(f>=b):
            f-=d-b
            if(f<=c):ans=("Yes")
            else:ans=("No")
            print(ans)
            break

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 > c:
        a = a%b
        while a<=c:
            a+=b
    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:
t = input()
result = []
hist = []
for i in range(int(t)):
	data = input().split()
	a = int(data[0])
	b = int(data[1])
	c = int(data[2])
	d = int(data[3])
	hist.clear()
	while True:
		if a < b:
			result.append("No")
			break
		n = int((a / b))
		a -= b * n
		if a <= c:
			a += d
		if a in hist:
			result.append("Yes")
			break
		else:
			hist.append(a)
			if len(hist) < 50:
				if min(hist) > a:
					result.append("No")
					break
		if (len(hist) == 5) & (b < d) & (b < c):
			result.append("Yes")
			break
for i in range(int(t)):
	print(result[i])

Submission status: Time Limit Exceeded

Solution code:
def result(a, b, c, d):
    if a < b or d < b or a % b > c:
        return 'No'
    delta = d % b
    if c > b or delta == 0:
        return 'Yes'
    start = a % b
    while start >= d:
        start -= b
    current = start
    while True:
        current += delta
        if current > c:
            current = min(current % b, current-b)
            if current < 0:
                return 'No'
        if current == start:
            return 'Yes'


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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def check(a,b,c,d,z_list_rec):
    z = a
    ans = 'Yes'
    if a < b:
        ans = 'No'
        return ans,z_list_rec
    z_list = [z] + z_list_rec
    for k in range(10000000):
        x = (z - c)//b
        if x <= 0:
            x = 1
        z = z - b * x
        if z < 0:
            ans = 'No'
            break
        if z <= c:
            z += d
        
        if z in z_list:
            ans = 'Yes'
            break
        z_list.append(z)
#         print(z)
    return ans,z_list

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_rec(rec,bcd):
    z_list = []
    b = bcd[0]
    c = bcd[1]
    d = bcd[2]
    for k,cd in enumerate(rec[0][::-1]):
        if [b,c,d] == cd:
            z_list += (rec[1][::-1][k])
            break
    return z_list




cd_rec = []
z_rec  = []
ans_rec = []
rec = [[],[]]
for a,b,c,d in zip(aa,bb,cc,dd):
    z_list = check_rec(rec, [b,c,d])
    ans,z_list = check(a,b,c,d,z_list)
    rec[0].append([b,c,d])
    rec[1].append(z_list)

    print(ans)

Submission status: Time Limit Exceeded

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 random
import re
import string
import sys

def debug_print(s):
    pass
    # print(s, end="")

def solve2(A, B, C, D):
    def before_C(v):
        if v > C:
            tmp = v - ( (v - C) // B ) * B
            if tmp == C and tmp + B <= v:
                tmp += B
            return tmp
        else:
            return v
    if B > A:
        debug_print("(1)")
        return "No"
    if B > D:
        debug_print("(2)")
        return "No"
    if B <= C:
        debug_print("(3)")
        return "Yes"

    seen = set()
    while True:
        A = before_C(A)
        A -= B
        if A < 0:
            return "No"
        if A <= C:
            A += D
        if A in seen:
            return "Yes"
        seen.add(A)

def solve():
    A, B, C, D = map(int, input().split())
    return solve2(A, B, C, D)

    
T = int(input())
for t in range(T):
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

T=int(input())
Juice=[None]*T
for i in range(T):
    Juice[i]=list(map(int,input().split()))

#A最初の在庫,Bすぬけ君が買う本数,C以下の場合D追加


#A→A%B
def juicecheck(i):
    A=Juice[i][0]
    B=Juice[i][1]
    C=Juice[i][2]
    D=Juice[i][3]

    if A<B:
        print("No")
        return
    elif B>D:
        print("No")
        return
    elif B<C:
        print("Yes")
        return
    else:
        x=A%B
        y=D%B

        z=gcd(B,y)

        for i in range(C+1,B):
            if i%z==x%z:
                print("No")
                return

        print("Yes")
        return
                


        #(x+n*y)%B==[C+1,B)となるnがあれば"No"

for i in range(T):
    juicecheck(i)

Submission status: Time Limit Exceeded

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

from math import ceil, floor

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

if __name__=="__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
T=int(input())
 
Query=[]
for i in range(T):
  Query.append(list(map(int,input().split())))
 
for i in range(T):
  appear=[]
  appear.append(Query[i][0])
  while(True):
    if Query[i][0]%Query[i][1]>Query[i][2] or Query[i][0]<Query[i][1]:
      print('No')
      break;
    Query[i][0]=Query[i][0]%Query[i][1] #昼
    if Query[i][0] in appear:
      print('Yes')
      break;
    else:
      appear.append(Query[i][0])
    if Query[i][0]<=Query[i][2]: #夜
      Query[i][0]+=Query[i][3]

Submission status: Time Limit Exceeded

Solution code:
import math
T = int(input())
A, B, C, D = [0 for i in range(T)], [0 for i in range(T)], [0 for i in range(T)], [0 for i in range(T)]
for i in range(T):
    A[i], B[i], C[i], D[i] =  list(map(int, input().split()))

def judge(a, b, c, d):
    z = a
    cnt = 0
    re = "No"
    while z >= b:
        z -= b
        if z <= c:
            z += d
        cnt += 1
        if z == a:
            re = "Yes"
            break
    return re
            
for i in range(T):
    print(judge(A[i], B[i], C[i], D[i]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def check(a,b,c,d,z_list_rec):
    z = a
    ans = 'Yes'
    if a < b:
        ans = 'No'
        return ans,z_list_rec
    z_list = [z] + z_list_rec
    for k in range(10000000):
        x = (z - c)//b
        if x <= 0:
            x = 1
        z = z - b * x
        if z < 0:
            ans = 'No'
            break
        if z <= c:
            z += d
        
        if z in z_list:
            ans = 'Yes'
            break
        z_list.append(z)
#         print(z)
    return ans,z_list

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_rec(rec,cd):
    z_list = []
    c = cd[0]
    d = cd[1]
    for k,cd in enumerate(rec[0][::-1]):
        if [c,d] == cd:
            z_list += (rec[1][::-1][k])
            break
    return z_list




cd_rec = []
z_rec  = []
ans_rec = []
rec = [[],[]]
for a,b,c,d in zip(aa,bb,cc,dd):
    z_list = check_rec(rec, [c,d])
    ans,z_list = check(a,b,c,d,z_list)
    rec[0].append([c,d])
    rec[1].append(z_list)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
t=int(input())
for _ in range(t):
    a,b,c,d=map(int,input().split())
    e=b*(max((a-c)//b,1))
    a-=e
    while a>c:
        a-=b
    if a<0:
        print('No')
        continue
    if d<b:
        print('No')
        continue
    if c<=b and d>b:
        while a<=c:
            a+=(d-b)
        if a<b:
            print('No')
            continue
    print('Yes')

Submission status: Time Limit Exceeded

Solution code:
def hantei(a, b, c, d):
    z = a
    while True:
        zz = z
        if z < b:
            return False
        if z - b > c:
            z = (z - c) % b + c
        else:
            z -= b
        if z <= c:
            z += d
        if z == a or z == zz:
            return True

t = int(input())
for i in range(t):
    query = map(int, input().split())
    print("Yes" if hantei(*query) else "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 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
  visited=[]
  while True:
    a = c+(a-c)%b
    if a>c and a<b:
      print('No')
      break
    elif a == c:
      if c in visited:
        print('Yes')
        break
      visited.append(a)
    else:
      a = a-b
      if a in visited:
        print('Yes')
        break
      visited.append(a)
    a+=d

Submission status: Time Limit Exceeded

Solution code:
t = input()
result = []
hist = []
for i in range(int(t)):
	data = input().split()
	a = int(data[0])
	b = int(data[1])
	c = int(data[2])
	d = int(data[3])
	hist.clear()
	while True:
		if a < b:
			result.append("No")
			break
		n = int((a / b))
		a -= b * n
		if a <= c:
			a += d
		if a in hist:
			result.append("Yes")
			break
		else:
			hist.append(a)
		if (len(hist) > 5) & (b < d) & (b < c):
			result.append("Yes")
			break
for i in range(int(t)):
	print(result[i])

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
c = []
for i in range(a):
	b = list(map(int,input().split()))
	if ((b[0] % b[1] == 0 and b[1] <= b[2]) and b[1] != 1 )or b[0] < b[1]:
		print("No")
	else:
		pp = b[0] % b[1]
		while(1):
			if pp  < 0:
				print("No")
				break
			elif pp in c:
				print("Yes")
				break
			else:
				c.append(pp)
			if pp <= b[2]:
				pp += b[3]
			if pp // b[1] <= 0:
				pp -= b[1]
			else:
				pp %= b[1]

		
	c = []

Submission status: Time Limit Exceeded

Solution code:
def sarch2(a, b, c, d):
    key = d - b
    fst = snd = trd = fth = fith = 0
    if a - b > b:
        a -= b * int(a / b)
    while True:
        if a == fst or a == snd or a == trd or a == fth or a == fith:
            return "Yes"
        fith = fth
        fth = trd
        trd = snd
        snd = fst
        fst = a
        
        if a - b > b:
            a -= b * int(a / b)
            a += b

        if a - b > c:
            break

        a += key
        

        
    if a - 2 * b >= 0:
        return "Yes"
    else:
        return "No"

       
N = int(input())
data = []
for i in range(N):
    A, B, C, D = map(int, input().split())
    if A < B or B > D:
        print("No")
    else:
        print(sarch2(A, B, C, D))

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())
	zaiko = A
	while True:
		zaiko -= B
		if zaiko < 0:
			print("No")
			break
		if zaiko <= C:
			zaiko += D
			if zaiko == A:
				print("Yes")
				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:
            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 = []
for i in range(t):
	a.append(input().split())
	for j in range(4):
		a[i][j] = int(a[i][j])

def kou(x, y):
	if y > x:
		z = y
		y = x
		x = z
	while x % y != 0:
		x, y = y, x % y
	return y

def function(arr):
	if arr[0] < arr[1]:
		print('No')
	elif arr[1] > arr[3]:
		print('No')
	elif arr[1] - arr[2] <= 1:
		print('Yes')
	else:
		count = 0
		s = kou(arr[1], arr[3])
		flag = 0
		while flag == 0:
			for i in range(arr[2] + 1, arr[1]):
				if (arr[0] - i) % s == 0:
					count = 1
					flag = 1
				if i == arr[1] - 1:
					flag = 1

		if count == 1:
			print('No')
		else:
			print('Yes')

for i in range(t):
	function(a[i])

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
abcd_list = [[int(i) for i in input().split()] for _ in range(t)]

for i in range(len(abcd_list)):
    a, b, c, d = abcd_list[i]
    _a = a
    is_ok = True
    for j in range(100000):
        if d < b:
            is_ok = False
            break
            
        if _a - b > c:
            _a -= (((_a - c) // b) - 1) * b
        _a -= b
        if _a < 0:
            is_ok = False
            break
        if _a <= c:
            _a += d
    if is_ok:
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

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


def hantei(a, b, c, d):
    z = a
    while True:
        zz = z
        if z < b:
            return False
        if z - b > c:
            z = (z - c) % b + c
        else:
            z -= b
        if z <= c:
            z += d
        if z == a or z == zz:
            return True


for query in queries:
    print("Yes" if hantei(*query) else "No")

Submission status: Time Limit Exceeded

Solution code:
def sarch2(a, b, c, d):
    key = d - b
    aa = a
    A = 0
    if a - b > b:
        a -= b * int(a / b)
    while True:
        if a == A:
            return "Yes"
        A = a
        
        if a - b > b:
            a -= b * int(a / b)
            a += b

        if a - b > c:
            break

        a += key
        

        
    if a - 2 * b >= 0:
        return "Yes"
    else:
        return "No"

       
N = int(input())
data = []
for i in range(N):
    A, B, C, D = map(int, input().split())
    if A < B or B > D:
        print("No")
    else:
        print(sarch2(A, B, C, D))

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)):
    b=t[i][0]%t[i][1]
    g=False
    if t[i][1]>t[i][3] or t[i][0]<t[i][1]:print("No");continue
    while 1:
        if t[i][0]<t[i][1]:print("No");break
        t[i][0]%=t[i][1]
        if t[i][0]==b and g:print("Yes");break
        g=True
        if t[i][0]<=t[i][2]:t[i][0]+=t[i][3]

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import math
def solve():
    a, b, c, d = map(int, input().split())
    # 自明
    if d < b or a < b:
        return 'No'
    if c > b:
        return 'Yes'
    a %= b
    if a > c:
        return 'No'
    if d-b == 0:
        return 'Yes'
    cache = set([a])
    while True:
        t = ((c-a)//(d-b)+1)*(d-b)+a
        if c < t < b:
            return 'No'
        a = t % b
        if a in cache:
            return 'Yes'
        else:
            cache.add(a)

T = int(input())
for _ in range(T):
    print(solve())

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
ABCD = [list(map(int,input().split())) for _ in range(T)]

for a,b,c,d in ABCD:
    if a < b:
        print('No')
        continue
    if d < b:
        print('No')
        continue
    tmp = a % b
    if tmp > c:
        print('No')
        continue
    if d % b == 0:
        print('Yes')
        continue
    if c >= b:
        print('Yes')
        continue

    dp = [a]
    now = a
    for _ in range(c+2):
        if now < b:
            print('No')
            break
        now %= b
        if now in dp:
            print('Yes')
            break
        if now > c:
            print('No')
            break
        dp.append(now)
        now += d

Submission status: Time Limit Exceeded

Solution code:

def main():
    t = int(input())
    A=[]
    B=[]
    C=[]
    D=[]

    for i 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):
        ans = "Yes"

        if A[i] < B[i] or B[i] > D[i]:
            print("No")
            continue

        if B[i]-1 <= C[i]:
            print("Yes")
            continue

        lst = []

        tmp = (A[i] + D[i]) % B[i]
        if tmp > C[i]:
                print("No")
                continue

        lst.append(tmp)

        for j in range(1,B[i]):
            tmp = (tmp + D[i]) % B[i]

            if tmp > C[i]:
                ans = "No"
                break

            if tmp in lst:
                break

            lst.append(tmp)

        print(ans)


if __name__ == "__main__":
        main()

Submission status: Time Limit Exceeded

Solution code:
t = input()
result = []
hist = []
for i in range(int(t)):
	data = input().split()
	a = int(data[0])
	b = int(data[1])
	c = int(data[2])
	d = int(data[3])
	hist.clear()
	while True:
		if a < b:
			result.append("No")
			break
		n = int((a / b))
		a -= b * n
		if a <= c:
			a += d
		if a in hist:
			result.append("Yes")
			break
		else:
			hist.append(a)
		if len(hist) > 10000:
			result.append("Yes")
			break
for i in range(int(t)):
	print(result[i])

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




for i in range(aa):

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

  e=a-a//b*b
  f=d-d//b*b
  

  h=0 #YES=0 NO=1


  if a<b or d<b :
    h=1
  else:

  
    if f!=0 :
      while e<b:
        if e>c :
          h=1
          
        e+=f
        if h==1:
          break
        #print(e)
        #print(f)
  #print("while")
    else:
      if e>c :
        h=1

  

  if h==1:
    print("No")
  else:
    print("Yes")

Submission status: Time Limit Exceeded

Solution code:
t = input()
result = []
hist = []
for i in range(int(t)):
	data = input().split()
	a = int(data[0])
	b = int(data[1])
	c = int(data[2])
	d = int(data[3])
	hist.clear()
	while True:
		if a < b:
			result.append("No")
			break
		n = int((a / b))
		a -= b * n
		if a <= c:
			a += d
		if a in hist:
			result.append("Yes")
			break
		else:
			hist.append(a)
		if len(hist) > 10000:
			result.append("Yes")
			break
for i in range(int(t)):
	print(result[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    N = int(input().strip())
    T = [[int(y) for y in input().strip().split()] for x in range(0,N)]
    j=True
    for line in T:
      Z = line[0]
      judge = [Z]
      hikaku = [Z]
      n = 1
      while j:
        n += 1
        if Z < line[1]:
          print("No")
          break
        Z = Z - line[1]
        hikaku.append(Z)
        if len(hikaku) > 3:
          hikaku.pop(0)
          if judge == hikaku:
            print("Yes")
            break
        if n <3:
            judge.append(Z)
        if Z <= line[2]:
            Z += line[3]
        if n <3:
            judge.append(Z)
        hikaku.append(Z)
        if len(hikaku) > 3:
          hikaku.pop(0)
          if judge == hikaku:
            print("Yes")
            break


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

from math import ceil

def solve(A, B, C, D):
    zaiko = A
    visited = set([zaiko])
    while zaiko >= B:
        zaiko -= B
        if zaiko > C:
            zaiko -= ceil((zaiko - C) / B) * B
            if zaiko < 0 or zaiko > C: return False
        zaiko += D
        if zaiko in visited: return True
        visited.add(zaiko)
    return False


T = int(input())
for i in range(T):
    A, B, C, D = [int(t) for t in input().split()]
    print('Yes' if solve(A, B, C, D) else 'No')

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
a = []
for i in range(t):
	a.append(input().split())
	for j in range(4):
		a[i][j] = int(a[i][j])

def kou(x, y):
	if y > x:
		z = y
		y = x
		x = z
	while x % y != 0:
		x, y = y, x % y
	return y

def function(arr):
	if arr[0] < arr[1]:
		print('No')
	elif arr[1] > arr[3]:
		print('No')
	elif arr[1] - arr[2] <= 1:
		print('Yes')
	else:
		count = 0
		s = kou(arr[1], arr[3])
		for i in range(arr[2] + 1, arr[1]):
			if (arr[0] - i) % s == 0:
				count = 1
				break

		if count == 1:
			print('No')
		else:
			print('Yes')

for i in range(t):
	function(a[i])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

def check_unique(list_tmp):
    L1 = len(list_tmp)
    L2 = len(list(set(list_tmp)))
    if L1 == L2:
        return True
    else:
        return False

def test(A,B,C,D):
    bottles = A
    bottles_log = []
    while bottles >0:
        bottles_log.append(bottles)
        #print(bottles)
        
        if check_unique(bottles_log):
            if bottles >= B:
                bottles = bottles - B
                if bottles <= C :
                    bottles += D 
            else:
                return("No")
            
        else:
            return("Yes")
        
    return("No")

for i in range (N):
    A,B,C,D =  map(int,input().split())
    print(test(A,B,C,D))