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

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"
        if self.a % self.b + self.a == self.d:
            return "Yes"
        return "No"

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

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(10000):
        if _a - b > c:
            _a = _a - (((_a - c) // b) - 1) * b
        _a = _a - b
        if _a < 0:
            is_ok = False
            break
        if _a <= c:
            _a += d
    if is_ok:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
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
            A = A % B + B
            if A <= C:
                A = A + D
        if flag:
            print('Yes')
        else:
            print('No')

Submission status: Wrong Answer

Solution code:
T = int(input())
for i in range(T):
    A,B,C,D = [int(i) for i in input().split()]
    S = set()
    num = A
    S.add(num)
    while(True):
        num -= B
        if num < 0:
            print("No")
            break
        else:
            if num <= C:
                num += D
            if num > C:
                num = (num - C) % B + C
                if num == C:
                    num+=C
            if num in S:
                print("Yes")
                break
            S.add(num)

Submission status: Wrong Answer

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(1000):
        if _a - b > c:
            _a = _a - (((_a - c) // b) - 1) * b
        _a = _a - b
        if _a < 0:
            is_ok = False
            break
        if _a <= c:
            _a += d
    if is_ok:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
t = int(input())
for i in range(t):
    a, b, c, d = map(int, input().split())
    rec = [a]
    flag = False
    num = 0
    while True and num < 100000:
        if a < b:
            break
        else:
            a = a % b
        if a <= c:
            a += d
        if a in rec:
            flag = True
            break
        num += 1
        rec.append(a)
    if flag:
        print("YES")
    else:
        print("NO")

Submission status: Wrong Answer

Solution code:
T=int(input())

for cnt in range(T):
  A,B,C,D=map(int,input().split())
  if(B>D or A < B):
    print("No")
    continue
  
  A = A % B + B
  Zaiko = A
  lista = []
  
  i = 0
  while(i < 1000):
    Zaiko = Zaiko - B
    if(Zaiko < 0):
      print("No")
      break
    if(Zaiko <= C):
      Zaiko += D
      Zaiko = Zaiko % B + B
    if(Zaiko in lista):
      print("Yes")
      break
    lista.append(Zaiko)
    i += 1
    if(i == 1000):
      print("Yes")
  #print(A,B,C,D)

Submission status: Wrong Answer

Solution code:
n = int(input())
for i in range(n):
    result = 0
    line = input().split()
    if int(line[0])%int(line[1])<=int(line[2]) and (int(line[0])%int(line[1])+int(line[3]))-int(line[1])>=0:
        print("yes")
    else:
        print("no")

Submission status: Wrong Answer

Solution code:
T = int(input())
set = []
for i in range(T):
    set.append([int(j) for j in input().split()])

for i in range(T):
    if set[i][0] < set[i][1] or set[i][1] > set[i][3]:
        print("No")
    elif set[i][1] == set[i][3]:
        if set[i][0] - set[i][1] > set[i][2] and set[i][0] % set[i][1] != 0:
            print("No")
        else:
            print("Yes")
    else:
        E = set[i][0] - set[i][1]
        F = set[i][3] - set[i][1]
        G = (set[i][2] - E)//F
        if set[i][0] - 2 * set[i][1] + (G + 1)*F >= 0:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def gcd(a, b):
    """greatest common divisor"""
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

def solve(f):
    t = f.read_int()
    ans = []

    def solve2():
        a, b, c, d = f.read_int_list()

        g = gcd(gcd(gcd(a, b), c), d)
        a, b, c, d = a/g, b/g, c/g, d/g

        if d < b: return 'No'
        if a < b: return 'No'
        if b <= c+1: return 'Yes'
        g2 = gcd(b, d)

        lo = ((c+1)-a)%g2
        if lo == 0 or lo + (b-c-1) >= g2: return 'No'

        return 'Yes'

    for _ in xrange(t):
        ans.append(solve2())

    return "\n".join(ans)


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Wrong Answer

Solution code:
def main():
    def gcd(x, y):
        while y:
            y, x = x % y, y
        return x
    t = int(input())
    for _ in range(t):
        A, B, C, D = map(int, input().split())
        gcf = gcd(gcd(A, B), gcd(C, D))
        A, B, C, D = A // gcf, B // gcf, C // gcf, D // gcf
        if (A >= B and C >= B and D >= B):
            print("Yes")
            continue
        elif B > D or B > A or (D > B and B > C + 1 and D % B) or (A % B > C and C < B):
            print("No")
            continue
        print("Yes")
    return 0
main()

Submission status: Wrong Answer

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]:
            ans = "no"

        for j in range(B[i]):
            if (A[i] + (j*D[i])) % B[i] > C[i]:
                ans = "no"

        print(ans)





if __name__ == "__main__":
        main()

Submission status: Wrong Answer

Solution code:
t = input()
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])
	re = c + d - 2*b
	if re < 1:
		print("No")
	else:
		print("Yes")

Submission status: Wrong Answer

Solution code:
import numpy as np

def check(a,b,c,d):
    z = a
    ans = 'Yes'
    if a < b:
        ans = 'No'
        return ans
    z_list = [z]
    for k in range(100):
        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

cd_rec = []
z_rec  = []
ans_rec = []
for a,b,c,d in zip(aa,bb,cc,dd):
    
    ans,z_list = check(a,b,c,d)
#     if [c,d] in cd_rec:
        
        
    cd_rec.append([c,d])
    z_rec.append(z_list)
    ans_rec.append(ans)
    
    print(ans)

Submission status: Wrong Answer

Solution code:
def canbuy(A,B,C,D):
    X=A
    if B>D:
        return False
    for i in range(10**5):
        X-=B*max([(X-C)//B,1])
        if i==0:
            Y=X
        if X<0:
            return False
        if X<=C:
            X+=D
        if X==A or (X==Y and i!=0):
            return True
    return True
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):
    if canbuy(a[i],b[i],c[i],d[i]):
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
t = int(raw_input())
for _ in xrange(t):
    a, b, c, d = map(int, raw_input().split())
    if a < b or b > d:
        print 'No'
        continue
    a -= b
    if a > c:
        a -= (a - c + b - 1) / b * b
    if a < 0:
        print 'No'
    elif d % b == 0 or c >= b:
        print 'Yes'
    else:
        d %= b
        a += (c - a + d) / d * d
        print 'No' if a < b else 'Yes'

Submission status: Wrong Answer

Solution code:
T = int(input())
set = []
for i in range(T):
    set.append([int(j) for j in input().split()])

for i in range(T):
    if set[i][0] < set[i][1] or set[i][1] > set[i][3]:
        print("No")
    elif set[i][1] == set[i][3]:
        if set[i][0] - set[i][1] > set[i][2] and set[i][0] < 2*set[i][1]:
            print("No")
        else:
            print("Yes")
    else:
        E = set[i][0] - set[i][1]
        F = set[i][3] - set[i][1]
        G = (set[i][2] - E)//F
        if set[i][0] - 2 * set[i][1] + (G + 1)*F >= 0:
            print("Yes")
        else:
            print("No")

Submission status: Wrong Answer

Solution code:
t = int(input())
r = []
for i in range(t):
    a,b,c,d = map(int, input().split())
    m = []
    a = min(a, a % b + b * ((b // c) + 1))
    while b <= a:
        print(a)
        m.append(a)
        a -= b
        if a <= c:
            a += min(d, b + d % b)
        if a in m:
            break
    if a in m:
        r.append('Yes')
    else:
        r.append('No')

print('\n'.join(r))

Submission status: Wrong Answer

Solution code:
import numpy as np

def check(a,b,c,d):
    z = a
    z_list = [z]
    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



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

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

Submission status: Wrong Answer

Solution code:
def canbuy(A,B,C,D):
    X=A
    for i in range(10**5):
        X-=B*max([(X-C)//B,1])
        if X<0:
            return False
        if X<=C:
            X+=D
        if X==A:
            return True
    return True
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):
    if canbuy(a[i],b[i],c[i],d[i]):
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
def main():
    def gcd(x, y):
        while y:
            y, x = x % y, y
        return x
    t = int(input())
    for _ in range(t):
        A, B, C, D = map(int, input().split())
        gcf = gcd(gcd(A, B), gcd(C, D))
        A, B, C, D = A // gcf, B // gcf, C // gcf, D // gcf
        if B > D or B > A or (D > B and B > C + 1 and D % B) or (A % B > C and C < B):
            print("NO")
            continue
        print("YES")
    return 0
main()

Submission status: Wrong Answer

Solution code:
def canbuy(A,B,C,D):
    X=A
    for i in range(10**4):
        X-=B*max([(X-C)//B,1])
        if X<0:
            return False
        if X<=C:
            X+=D
        if X==A:
            return True
    return True
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):
    if canbuy(a[i],b[i],c[i],d[i]):
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:

def calc():
    a, b, c, d = map(int, raw_input().split())
    if a < b:
        return 'No'
    if d < b:
        return 'No'
    for _ in range(10000):
        a %= b
        if a > c:
            return 'No'
        a += d
    return 'Yes'


def main():
    t = input()
    for _ in range(t):
        print calc()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
t = int(input())

for i in range(t):
    able = "Yes"
    l = list(map(int, input().split()))
    if l[0]-l[1] <0:
        able = "No"
    if (l[0]-l[2])%l[1]+l[2] < l[1]:
        able = "No"
    if l[3] < l[1]:
        able = "No"
    print(able)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
t = int(input())
for _ in range(t):
    a,b,c,d = map(int,input().split())

    if a < b or b > d:
        print("No")
        continue
    if b == d:
        if c >= a:
            print("Yes")
        elif a - ((a-c-1)//b+1)*b < 0:
            print("No")
        else:
            print("Yes")
        continue
    if c >= b:
        print("Yes")
        continue

    count = 0
    s = set()
    check = True
    while count < 10**6:
        x = max((a-c-1)//b,0)+1
        a -= x*b
        if a < 0:
            check = False
            break
        if a in s:
            break
        s.add(a)
        a += d
        count += 1
    if check:
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)
t = int(input())
for _ in range(t):
    a,b,c,d = map(int,input().split())

    if a < b or b > d:
        print("No")
        continue
    if b == d:
        if c >= a:
            print("Yes")
        elif a - ((a-c-1)//b+1)*b < 0:
            print("No")
        else:
            print("Yes")
        continue
    if c >= b:
        print("Yes")
        continue

    count = 0
    s = set()
    check = True
    while True:
        x = max((a-c-1)//b,0)+1
        a -= x*b
        if a < 0:
            check = False
            break
        if a in s:
            break
        s.add(a)
        a += d
        #count += 1
    if check:
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
def Golf():*a,=map(int,open(0))
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)
 
YN=['YES','NO'];Yn=['Yes','No']
mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
class Tree:
    def __init__(self,inp_size=None,ls=None,init=True,index=0):
        self.LCA_init_stat=False
        self.ETtable=[]
        if init:
            if ls==None:
                self.stdin(inp_size,index=index)
            else:
                self.size=len(ls)+1
                self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def stdin(self,inp_size=None,index=1):
        if inp_size==None:
            self.size=int(input())
        else:
            self.size=inp_size
        self.edges,_=GI(self.size,self.size-1,index=index)
        return
    
    def listin(self,ls,index=0):
        self.size=len(ls)+1
        self.edges,_=GI(self.size,self.size-1,ls,index=index)
        return
 
    def __str__(self):
        return  str(self.edges)
 
    def dfs(self,x,func=lambda prv,nx,dist:prv+dist,root_v=0):
        q=deque()
        q.append(x)
        v=[-1]*self.size
        v[x]=root_v
        while q:
            c=q.pop()
            for nb,d in self.edges[c]:
                if v[nb]==-1:
                    q.append(nb)
                    v[nb]=func(v[c],nb,d)
        return v
 
    def EulerTour(self,x):
        q=deque()
        q.append(x)
        self.depth=[None]*self.size
        self.depth[x]=0
        self.ETtable=[]
        self.ETdepth=[]
        self.ETin=[-1]*self.size
        self.ETout=[-1]*self.size
        cnt=0
        while q:
            c=q.pop()
            if c<0:
                ce=~c
            else:
                ce=c
                for nb,d in self.edges[ce]:
                    if self.depth[nb]==None:
                        q.append(~ce)
                        q.append(nb)
                        self.depth[nb]=self.depth[ce]+1
            self.ETtable.append(ce)
            self.ETdepth.append(self.depth[ce])
            if self.ETin[ce]==-1:
                self.ETin[ce]=cnt
            else:
                self.ETout[ce]=cnt
            cnt+=1
        return
    
    def LCA_init(self,root):
        self.EulerTour(root)
        self.st=SparseTable(self.ETdepth,init_func=min,init_idl=inf)
        self.LCA_init_stat=True
        return
    
    def LCA(self,root,x,y):
        if self.LCA_init_stat==False:
            self.LCA_init(root)
        xin,xout=self.ETin[x],self.ETout[x]
        yin,yout=self.ETin[y],self.ETout[y]
        a=min(xin,yin)
        b=max(xout,yout,xin,yin)
        id_of_min_dep_in_et=self.st.query_id(a,b+1)
        return self.ETtable[id_of_min_dep_in_et]
 
class SparseTable: # O(N log N) for init, O(1) for query(l,r)
    def __init__(self,ls,init_func=min,init_idl=float('inf')):
        self.func=init_func
        self.idl=init_idl
        self.size=len(ls)
        self.N0=self.size.bit_length()
        self.table=[ls[:]]
        self.index=[list(range(self.size))]
        self.lg=[0]*(self.size+1)
        
        for i in range(2,self.size+1):
            self.lg[i]=self.lg[i>>1]+1
 
        for i in range(self.N0):
            tmp=[self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) for j in range(self.size)]
            tmp_id=[self.index[i][j] if self.table[i][j]==self.func(self.table[i][j],self.table[i][min(j+(1<<i),self.size-1)]) else self.index[i][min(j+(1<<i),self.size-1)] for j in range(self.size)]
            self.table+=[tmp]
            self.index+=[tmp_id]
    
    # return func of [l,r)
    def query(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        return self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))])
    
    # return index of which val[i] = func of v among [l,r)
    def query_id(self,l,r):
        if r>self.size:r=self.size
        #N=(r-l).bit_length()-1
        N=self.lg[r-l]
        a,b=self.index[N][l],self.index[N][max(0,r-(1<<N))]
        if self.table[0][a]==self.func(self.table[N][l],self.table[N][max(0,r-(1<<N))]):
            b=a
        return b
    
    def __str__(self):
        return str(self.table[0])
 
    def print(self):
        for i in self.table:
            print(*i)
 
class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]
 
    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
 
show_flg=False
show_flg=True
ans=0

T=I()

for _ in range(T):
    a,b,c,d=LI()
    z=[]
    night=a
    ans='Yes'
    while True:
        day=night
        k=max(1,(day-c)//b)
        day-=b*k
        night=day+d if day<=c else day
        
        if (day,night) in z:
            z+=[(day,night)]
            break
        
        z+=[(day,night)]
        
        if day<0:# or len(z)>10**5:
            ans='No'
            break
    print(ans)#,z[:20])

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
for _ in range(t):
  a, b, c, d = map(int, input().split())
  zaiko = a
  rereki = [a]
  while True:
    zaiko -= b
    if zaiko < 0:
      ans = 'No'
      break
    if zaiko <= c:
      zaiko += d
    if zaiko in rereki:
      ans = 'Yes'
      break
    rereki.append(zaiko)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
t = int(input())
for _ in range(t):
  a, b, c, d = map(int, input().split())
  zaiko = a
  while True:
    zaiko -= b
    if zaiko < 0:
      ans = 'No'
      break
    if zaiko <= c:
      zaiko += d
    if zaiko == a:
      ans = 'Yes'
      break
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import math
t = int(input())
# t <= 300,a,b,c,d <= 10**18
# a-k*b <= c になったら+d をする。ことを繰り返し、尽きるケースがあるかを答える
# ダメになるケースは d<b ならば絶対に続かない　補給しても減るから
# また、何回か補給したタイミングで、s > c and s < k になるケースがあれば終了する
# sがどんな値をとるかは、循環しない中でどの値をとるかを記録しておけばわかる。
# 補給される直前の値をリストで保持してそれに含まれるかを確かめればOK
# この方針だと長さb のリストを保持する必要があって、O(b*t)でTLE
# 数学的に考えられるはず。
# c+d,c-b+d+1 の中でb以下になるケースに陥ったら負け
# 解説をみると、コーナーケースを覗くと、mod で捉えることができる。
# mod B で見た時に、modB >C になるケースが存在すればダメ
# A+k*D modB がC を超えるタイミングがあればそこで-B がきて、補充が来る前に負の数になる

for i in range(t):
    a,b,c,d = map(int,input().split())
    under = -1 + math.ceil(a/b)
    if a < b: print("No")
    elif d < b: print("No")
    elif c >= b:print("Yes")
    else:
        for j in range(b):
            if (a+j*d) % b > c:
                print("No")
                break
            if j == b-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 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:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

T = ir()

def check(a, b, c, d, i):
    if a < b or d < b:
        print('No')
    else:
        a %= b
        if d > b:
            d %= b
            d += b        
        X = np.array([a] + [d] * 10 ** 5)
        Xcum = (X.cumsum() % b)
        Xcum[Xcum<=c] += d
        bool = np.any(Xcum < b)
        print('No' if bool else 'Yes')

for i in range(T):
    check(*(lr() + [i]))    

# 10

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt

T = int(input())
for _ in range(T):
  A,B,C,D = list(map(int, input().split()))
  # 初日にゼロ、もしくは補給よりも消費が多い。
  if A < B or B > D:
    print("No")
    continue
  # 補給が必ずまにあう
  if 2*B <= C:
    print("Yes")
    continue
  flg = True
  histo = set()
  for i in range(max(1000,int(sqrt(B)))):
    #print(i,A, A%B, A%B <= C, A%B+D)
    A = A%B+D
    if A in histo:
      break
    else:
      histo.add(A)
      
    if A%B > C:
      print("No")
      flg = False
      break
  if flg:
    print("Yes")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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