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 numpy as np

N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]

a_ = np.array(a)

def checking(A,B,C,D):
    X = A
    Y = A

    while X >= B:
        Y = X - B
        if Y <= C:
            X = Y + D
        else:
            X = Y

        if X == A:
            res = "Yes"
            break

    if X<=B:
        res = "No"

    return res

for s in range(N):
    A_ = a_[s,0]
    B_ = a_[s,1]
    C_ = a_[s,2]
    D_ = a_[s,3]
    result = checking(A_, B_ , C_ , D_)
    print(result)

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



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




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 a<b or d<b :
    return "No"
  
  elif b<=c :
    return "Yes"
  else:
  
    if f!=0 :
      
      
      for j in range(b//f):
        if e>c :
          return "No"
          
        e=(e+f)%b

        #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:
#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:
            sur = a % b
            while sur != a:
                a += d
                if c < a % b:
                    print("No")
                    break
                a %= b
            else:
                print("Yes")

Submission status: Time Limit Exceeded

Solution code:
t = int(input())

def no():
    print("No")

def yes():
    print("Yes")

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

    dif = d - b
    min_n = a - b

    if (a < b or d < b or a % b > c):
        no()
        continue
    if dif != 0 and min_n != 0 and b > c:
        already = []
        start = a
        done = False
        while True:
            count = start // b
            end = start - count * b
            if end in already:
                break
            if b > end > c:
                no()
                done = True
                break
            already.append(end)
            start = end + d
        if done:
            continue

    yes()

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-1:
        print('Yes')
        continue
    if a%2 == 0 and b%2 == 0 and d %2 == 0 and b-c == 2:
        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:
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
        while(B*j+i>C):
            j=j+1
        rest = B*(j)+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:
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
            mod = A % B
            q = int((A - mod) / B)
            A = mod
            for i in range(q-1):
                if A + B > C:
                    break
                A += B
            A_list.append(A + B)
            #n = np.floor((A-C)/B)
            #print(n)
            #A = A - n * B
            #print(f"night:{A}")
            if A <= C:
                A = A + D
        if flag:
            print('Yes')
        else:
            print('No')

Submission status: Time Limit Exceeded

Solution code:
# B - rng_10s
# https://beta.atcoder.jp/contests/agc026/tasks/agc026_b

# B_rng_10s.py

T = int(input())


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


    left = a
    appeared = [a]

    if a < b:
        result = "No"
    if a % b > c:
        result = "No"
    else:
        left -= b
    while left%b not in appeared[:len(appeared)-1]:
        if left % b > c:
            result = "No"
            break
        else:
            left = left % b
            appeared.append(left)
            if left <= c:
                #appeared.append(left)
                left += d
                #appeared.append(left)
                if left < b:
                    result = "No"
                    break
            #appeared.append(left)

    print(result)

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
a = []
b = []
c = []
d = []
for i in range(t):
    tmp = list(map(int, input().split(" ")))
    a.append(tmp[0])
    b.append(tmp[1])
    c.append(tmp[2])
    d.append(tmp[3])

for i in range(t):
    if(a[i] < b[i]): print("No")
    elif(b[i] > d[i]): print("No")
    elif(b[i] <= c[i]): print("Yes")
    else:
        remain = a[i] - (a[i] // b[i]) * b[i]
        increase = d[i] - b[i]
        if(increase == 0):
            if(c[i] >= remain): print("Yes")
            elif(b[i] > remain): print("No")
            else: print("Yes")
        else:
            history = [-1]
            flag = False
            while((remain in history) == False):
                history.append(remain)
                if(c[i] >= remain): remain += ((c[i] - remain) // increase) * increase
                if(c[i] >= remain): remain += increase
                if(remain < b[i]):
                    print("No")
                    flag = True
                    break
                remain = remain - (remain // b[i]) * b[i]
            if(flag == False): print("Yes")

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
llist = []
for i in range(T):
    L = list(map(int, input().split()))
    llist.append(L)
for i in range(T):
    A = llist[i][0]
    B = llist[i][1]
    C = llist[i][2]
    D = llist[i][3]
    b = False
    N = A%B
    if A>=B:
        while N >= 0:
            if N<=C:
                N += D
            if N==A:
                b = True
                break
            if N>A+D:
                b = True
                break
            N %= B
    if b:
        print("Yes")
    else:
        print("No")

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
        if flag == 1:
            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])
        A_list = []
        flag = False
        while True:
            if A in A_list:
                flag = True
                break
            A_list.append(A)
            if A < B:
                break
            mod = A % B
            q = int((A - mod) / B)
            A = mod
            for i in range(q-1):
                if A + B > C:
                    break
                A += B
            A_list.append(A + B)
            #n = np.floor((A-C)/B)
            #print(n)
            #A = A - n * B
            #print(f"night:{A}")
            if A <= C:
                A = A + D
        if flag:
            print('Yes')
        else:
            print('No')

Submission status: Time Limit Exceeded

Solution code:
# B - rng_10s
# https://beta.atcoder.jp/contests/agc026/tasks/agc026_b

# B_rng_10s.py

T = int(input())


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


    left = a
    appeared = [a]

    if a < b:
        result = "No"
    if a % b > c:
        result = "No"
    else:
        left -= b
    while left%b not in appeared[:len(appeared)-1]:
        if left % b > c:
            result = "No"
            break
        else:
            left = left % b
            if left <= c:
                appeared.append(left)
                left += d
                appeared.append(left)
                if left < b:
                    result = "No"
                    break
            appeared.append(left)

    print(result)

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]
    if (s < b) or (b>d):
        print('No')
        continue


    s_list = []
    while True:
        s_list.append(s)
        if s > b*2:
            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 <= 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:
T = int(input())
llist = []
for i in range(T):
    L = list(map(int, input().split()))
    llist.append(L)
for i in range(T):
    A = llist[i][0]
    B = llist[i][1]
    C = llist[i][2]
    D = llist[i][3]
    b = False
    N = A%B
    if A>=B:
        while N >= 0:
            if N<=C:
                N += D
            if N==A:
                b = True
                break
            if N>A+D:
                b = True
                break
            N -= B
    if b:
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

  flg = False
  s = [a]
  t = a % b
  while (a >= b and d >= b):
    if(t > c):
      break
    else:
      t += d
      if(t in s):
        flg = True
        break
      else:
        s.append(t)
        t = t % b
  if(flg):
    print("Yes")
  else:
    print("No")

  #if( (a<b) or (d<b) or ( a > b and a%b > c) or ( d > b and ((a%b)+d)%b > c) or b > h or (a % d > c and b > c) or  (d %b > c and b > c) or (h % b > c and b > c)):
  del(s)

Submission status: Time Limit Exceeded

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

T = int(input())
L = []

for i in range(T):
     L.append(list(map(int, input().split())))
P=0
F=0
for i in range(T):
     A=L[i]
     #print(A)
     P=A[0]
     X = P%A[1]
     if A[0]<A[1]:
         F=1
         print("No")
     elif (X>A[2]) and (F==0):
         F=1
         print("No")
     elif (A[2]+A[3]<A[0]) and (F==0):
         F=1
         print("No")
     P=X+A[3]
     for i in range(100000):
         P=P-A[1]
         if (P<0) and (F==0):
             F=1
             print("No")
             break;
         if P<=A[2]:
             P = P+A[3]
     if F==0:
         print("Yes")
     P=0
     F=0

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
for i in range(t):
    able = "Yes"
    a,b,c,d = map(int, input().split())
    if a - b < 0:
        able = "No"
    else:
        x=a
        if a-c >= 0:
            x = c+(a-c)%b
        if x <= c:
            x+=d
        for i in range(150000):
            x-=b
            if x >= 0:
                if x <= c:
                    x+=d
            else:
                able = "No"
                break
    print(able)

Submission status: Time Limit Exceeded

Solution code:
# B - rng_10s
# https://beta.atcoder.jp/contests/agc026/tasks/agc026_b

# B_rng_10s.py

T = int(input())


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


    left = a
    appeared = [a]

    if a < b:
        result = "No"
    if a % b > c:
        result = "No"
    else:
        left -= b
    while left not in appeared[:len(appeared)-1] and result != "No":
        if left % b > c:
            result = "No"
        else:
            left = left % b
            if left <= c:
                appeared.append(left)
                left += d
                if left < b:
                    result = "No"
            appeared.append(left)

    print(result)

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
for _ in range(T):
    A, B, C, D = map(int, input().split())
    if B>D:
        print('No')
        continue
    pre = set()
    if A//B>0:
        A = A%B
        A += B
    if D//B>0:
        D = D%B
        D += B
    pre.add(A)
    while 1:
        A = A-B
        if A < 0:
            print('No')
            break
        if  A <= C:
            A += D
        if A in pre:
            print('Yes')
            break
        pre.add(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) > 9000:
				if min(hist) > a:
					result.append("No")
					break
			if len(hist) > 10000:
				result.append("Yes")
				break
for i in range(int(t)):
	print(result[i])

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
		while s:
			if add >= 0:
				if a > 2*b:
					print('Yes')
					s = False
				if c >= b:
					print('Yes')
					s = False
				elif a+add <= c:
					if a+add in check:
						print('Yes')
						s = False
					else:
						check.append(a+add)
						a = a+add
				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())
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
        if c>=b and d >=b:
            print('Yes')
            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:
def main():
    N = int(input())
    ABCD = [list(map(int, input().split())) for i in range(N)]
    for A,B,C,D in ABCD:
        # 在庫が買う本数以下 or 在庫追加が買う本数以下
        if A < B or D < B:
            print("No")
            continue

        A %= B
        # 在庫が買う本数以下になり、補給出来ない
        if C < A:
            print("No")
            continue
        if B == D:
            print("Yes")
            continue

        tmp = D-B
        flag = True
        for i in range(C+1,B):
            if (i-A) % tmp == 0:
                flag = False
                break
        print("Yes" if flag else "No")


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
grid =[]
for i in range(n):
    array = list(map(int, input().strip().split()))
    grid.append(array)

def checktrue(a, b, c, d):
    zaiko = []
    zaiko.append(a)
    tmpzaiko = a
    plusD = d % b
    while True:
        if b > d or a < b:
            return "No"
        tmpzaiko %= b
        if tmpzaiko > c:
            return "No"
        else:
            #もし在庫が補充量を下回ってたら在庫を追加する
            if tmpzaiko <= c:
                tmpzaiko += plusD
            if tmpzaiko in zaiko:
                return "Yes"
            else:
                zaiko.append(tmpzaiko)
for el in range(n):
    a = grid[el][0]
    b = grid[el][1]
    c = grid[el][2]
    d = grid[el][3]
    print(checktrue(a, b, c, d))

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

        if a - b > b:
            a -= b * int(a / b)
            a += b

        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:
# cook your dish here
#import numpy as np
t =int(input())

for i in range(0,t):
    a,b,c,d = map(int,input().split())
    
    l = [a]
    if a < b:
        print('No')
    elif a-c <= 0:
        if a >= b and d >=b:
            print('Yes')
        else:
            print('No')
    else:
        
        for j in range(0,100001):
            if a-c <= 0:
                if a >= b and d >=b:
                    print('Yes')
                else:
                    print('No')
                break
            if (a-c)%b == 0:
                n = (a-c)//b
            else:
                n = (a-c)//b + 1
            if a- n*b < 0:
                print('No')
                break
            if  a-n*b+d in l:
                print('Yes')
                break
            l.append(a-n*b+d)
            a = a-n*b+d
            if j == 100000:
                print('Yes')
                break

Submission status: Time Limit Exceeded

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

from math import ceil, floor

def solve():
    n = int(input())
    al = [[int(i) for i in input().split()] for _ in range(n)]
    ans = []
    for i in range(n):
        l = set([])
        a, b, c, d = al[i]
        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
            if t > c:
                ff = ceil((t / b) - (c / b))
                t = t - b * ff
            if t in l:
                ans.append('Yes')
                break
            if t < 0:
                ans.append('No')
                break
            l.add(t)
            a = t + d
    print('\n'.join(ans))

if __name__=="__main__":
    solve()

Submission status: Time Limit Exceeded

Solution code:
#B
import math
T=int(input())
for i in range(T):
    check=0
    A,B,C,D=map(int,input().split())
    A_list=[]
    if B>D:
        print('No')
        check=1
    while check==0:
        k=math.ceil((A-C)/B) if A>=C else 1
        A-=k*B
        if A<0:
            print('No')
            break
        elif A<=C:
            A+=D
            if (A in A_list or A==0):
                print('Yes')
                break
            A_list.append(A)

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

    if b - c == 1:
        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:
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 = A
    init = []
    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 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:
T = int(input())

a = [0]*T
b = [0]*T
c = [0]*T
d = [0]*T

for i in range(T):
    s = input().rstrip().split(' ')
    a[i] = int(s[0])
    b[i] = int(s[1])
    c[i] = int(s[2])
    d[i] = int(s[3])
    
for i in range(T):
# for i in [0,1,2,3]:
    NoFlag=0
#     print("i={}".format(i))
    if a[i] < b[i]:
        print("No")

    elif d[i] < b[i]:
        print("No")

    elif b[i] <= c[i]+1:
        print("Yes")
        
    else:
#         print(a[i])
#         a[i] = a[i] - (c[i] // b[i]) *b[i]
        a[i] = a[i]%b[i] +b[i]
        for ii in range(c[i]):
            a[i] = a[i]-b[i]
            if a[i]<0:
                NoFlag=1
                print("No")
                break
            
            if a[i] <= c[i]:
                a[i] = a[i] + d[i]
#                 a[i] = a[i]%b[i] +b[i]
                a[i] = a[i] - (c[i] // b[i]) *b[i]
            
        if NoFlag ==0:
            print("Yes")

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
grid =[]
for i in range(n):
    array = list(map(int, input().strip().split()))
    grid.append(array)

def checktrue(a, b, c, d):
    zaiko = []
    zaiko.append(a)
    tmpzaiko = a
    while True:
        if b > d or a < b:
            return "No"
        tmpzaiko %= b
        if tmpzaiko > c:
            return "No"
        else:
            #もし在庫が補充量を下回ってたら在庫を追加する
            if tmpzaiko <= c:
                tmpzaiko += d
            if tmpzaiko in zaiko:
                return "Yes"
            else:
                zaiko.append(tmpzaiko)
for el in range(n):
    a = grid[el][0]
    b = grid[el][1]
    c = grid[el][2]
    d = grid[el][3]
    print(checktrue(a, b, c, d))

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
            if t > c:
                ff = ceil((t / b) - (c / b))
                t = t - b * ff
            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 _ 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)*(min(0,c//(d-b)-10000))
        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:
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] or b[1] > b[3]:
		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 = []