Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left
from itertools import accumulate

n,q = li()
a = list(li())
x = [ni() for _ in range(q)]


cum_nor = list(accumulate(a))
cum_odd = list(accumulate(a[::2]))
cum_evn = list(accumulate(a[1::2]))
cum_kog = [cum_odd[i//2] if i%2==0 else cum_evn[i//2] for i in range(n)]

def check(a:list, tak:int, pivot:int):
    rng = a[-tak] - pivot
    left = bisect_left(a, pivot-rng)
    right = bisect_left(a, pivot+rng)
    aok = right-left
    if aok >= tak-1:
        return True
    else:
        return False
    
def binsearch(a:list,xi:int):
    low = 1
    high = len(a)
    while high-low > 1:
        mid = (high+low) // 2
        if check(a,mid,xi):
            low = mid
        else:
            high = mid
            
    return low

for xi in x:
    tak = binsearch(a,xi)
    ans = cum_nor[-1] - cum_nor[-tak-1]
    if 2*tak < n:
        ans += cum_kog[-2*tak-1] 
    
    print(ans)

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left
from itertools import accumulate

n,q = li()
a = list(li())
x = [ni() for _ in range(q)]


cum_nor = list(accumulate(a))
cum_odd = list(accumulate(a[::2]))
cum_evn = list(accumulate(a[1::2]))
cum_kog = [cum_odd[i//2] if i%2==0 else cum_evn[i//2] for i in range(n)]

# 高橋君が左からtak個取れるならTrue そうでないならFalse
def check(a:list, tak:int, pivot:int):
    if tak > (len(a)+1)//2:
        return False
    
    if pivot-a[-2*tak+1] > a[-tak]-pivot:
        return False
    else:
        return True
        
    
def binsearch(a:list,xi:int):
    low = 1
    high = len(a)
    while high-low > 1:
        mid = (high+low) // 2
        if check(a,mid,xi):
            low = mid
        else:
            high = mid
            
    return low


for xi in x:
    tak = binsearch(a,xi)
    ans = cum_nor[-1] - cum_nor[-tak-1]
    if 2*tak < n:
        ans += cum_kog[-2*tak-1] 
    
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
h = list(li())

cost = [float("inf")]*(n)
cost[0] = 0

for i in range(n-1):
    cnt = 1
    while i+cnt < n and cnt <= k:
        cost[i+cnt] = min(cost[i+cnt],
                          cost[i] + abs(h[i+cnt]-h[i]))
        
        cnt += 1
             
print(cost[-1])

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,k = li()
h = list(li())

cost = [float("inf")]*(n)
cost[0] = 0

for i in range(n-1):
    cnt = 1
    while i+cnt < n and cnt <= k:
        cost[i+cnt] = min(cost[i+cnt],
                          cost[i] + abs(h[i+cnt]-h[i]))
        
        cnt += 1
             
print(cost[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = " " + ns()
t = " " + ns()

lens = len(s)
lent = len(t)

lcs = [[""]*lent for _ in range(lens)]

for i in range(1,lens):
    for j in range(1,lent):
        if s[i] == t[j]:
            lcs[i][j] = lcs[i-1][j-1] + s[i]
            
        else:
            if len(lcs[i-1][j]) > len(lcs[i][j-1]):
                lcs[i][j] = lcs[i-1][j]
            else:
                lcs[i][j] = lcs[i][j-1]
                
print(lcs[-1][-1])

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def print_lcs (a, b, lcs, i, j, cur):
    if i==0 or j==0:
        return cur
    
    elif a[i] == b[j]:
        ans = print_lcs(a,b,lcs,i-1,j-1,cur+a[i])
        return ans
    
    else:
        if lcs[i-1][j] >= lcs[i][j-1]:
            return print_lcs(a,b,lcs,i-1,j,cur)
        else:
            return print_lcs(a,b,lcs,i,j-1,cur)
        


s = " " + ns()
t = " " + ns()

lens = len(s)
lent = len(t)

lcs = [[0]*lent for _ in range(lens)]
ans = ""
for i in range(1,lens):
    for j in range(1,lent):
        if s[i] == t[j]:
            lcs[i][j] = lcs[i-1][j-1] + 1

            
        else:
            if lcs[i-1][j] > lcs[i][j-1]:
                lcs[i][j] = lcs[i-1][j]
            else:
                lcs[i][j] = lcs[i][j-1]
                
print(print_lcs(s,t,lcs,lens-1,lent-1,"")[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())

dp = [[0]*(n+1) for _ in range(n+1)]

for length in range(1,n+1):
    for stidx in range(n-length+1):
        # 次郎の手番
        if(n-length)%2:
            dp[stidx][stidx+length] = min(dp[stidx+1][stidx+length] - a[stidx],
                                          dp[stidx][stidx+length-1] - a[stidx+length-1])
        # 太郎の手番
        else:
            dp[stidx][stidx+length] = max(dp[stidx+1][stidx+length] + a[stidx],
                                          dp[stidx][stidx+length-1] + a[stidx+length-1])
            
print(dp[0][n])

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
a = list(li())

dp = [[0]*(n+1) for _ in range(n+1)]

for length in range(1,n+1):
    for stidx in range(n-length+1):
        # 次郎の手番
        if(n-length)%2:
            dp[stidx][stidx+length] = min(dp[stidx+1][stidx+length] - a[stidx],
                                          dp[stidx][stidx+length-1] - a[stidx+length-1])
        # 太郎の手番
        else:
            dp[stidx][stidx+length] = max(dp[stidx+1][stidx+length] + a[stidx],
                                          dp[stidx][stidx+length-1] + a[stidx+length-1])
            
print(dp[0][n])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


def bg(n:int):
    
    if n == 0:
        yield 'P'
        
    else:
        yield 'B'
        
        gen = bg(n-1)
        while True:
            try:
                yield next(gen)
            except:
                break

        
        yield 'P'
        
        gen = bg(n-1)
        while True:
            try:
                yield next(gen)
            except:
                break
        
        yield 'B'
        

n,x = li()

cnt = 0
ans = 0

s = bg(n)
while True:
    try:
        char = next(s)
        if char == 'P' and cnt < x:
            ans += 1
            
        cnt += 1
    except:
        break
            
print(ans)

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

a = [1]
p = [1]
for _ in range(51):
    a.append(3 + 2*a[-1])
    p.append(1 + 2*p[-1])

def burger(level: int, x: int):
    if level == 0:
        if x <= 0:
            return 0
        else:
            return 1
    
    if x == 1:
        return 0
    
    elif 1 < x <= 1+a[level-1]:
        return burger(level-1, x-1)
    
    elif x == 2+a[level-1]:
        return p[level-1] + 1
    
    elif 2+a[level-1] < x <= 2+2*a[level-1]:
        return p[level-1] + 1 + burger(level-1, x-1-a[level-1]-1)
    
    elif x == 3+2*a[level-1]:
        return 2*p[level-1] + 1
    
n,x = li()    
print(burger(n,x))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
py = []
for _ in range(m):
    py.append(list(li()))

order = [[] for _ in range(n+1)]

for p,y in sorted(py, key=lambda x:x[1]):
    order[p].append(y)

cnt = [0]*(n+1)

for p,y in py:
    cnt[p] += 1
    ans = str(p).zfill(6) + str(order[p].index(y)+1).zfill(6)
    print(ans)

Fixed submission:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,m = li()
py = []
for _ in range(m):
    py.append(list(li()))

order = [{} for _ in range(n+1)]
cnt = [0]*(n+1)

for p,y in sorted(py, key=lambda x:x[1]):
    order[p].update({y: cnt[p]})
    cnt[p] += 1


for p,y in py:
    cnt[p] += 1
    ans = str(p).zfill(6) + str(order[p][y]+1).zfill(6)
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,x = li()
a = list(li())

# 係数リスト
coef = [5] + [2*i + 5 for i in range(n+1)]

a = a[::-1]
a_cum = [0] + list(accumulate(a))

ans = float("inf")
# 往復回数決め打ち
for k in range(1,n+1):
    cost = (n+k) * x
    i = 0
    while i*k <= n:
        cost += coef[i]*(a_cum[min((i+1)*k, n)] - a_cum[i*k])
        i += 1

    ans = min(ans, cost)
    
print(ans)

Fixed submission:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,x = li()
a = list(li())

# 係数リスト
coef = [5] + [2*i + 5 for i in range(n+1)]

a = a[::-1]
a_cum = [0] + list(accumulate(a))

ans = float("inf")
# 往復回数決め打ち
for k in range(1,n+1):
    cost = (n+k) * x
    i = 0
    while i*k <= n:
        cost += coef[i]*(a_cum[min((i+1)*k, n)] - a_cum[i*k])
        i += 1

    ans = min(ans, cost)
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,x = li()
a = list(li())

# 係数リスト
coef = [5] + [2*i + 5 for i in range(n+1)]

a = a[::-1]

ans = float("inf")
# 往復回数決め打ち
for k in range(1,n+1):
    cost = sum([coef[i//k]*a[i] for i in range(n)]) + (n+k)*x
    ans = min(ans, cost)
    
print(ans)

Fixed submission:
import sys
stdin = sys.stdin

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,x = li()
a = list(li())

# 係数リスト
coef = [5] + [2*i + 5 for i in range(n+1)]

a = a[::-1]
a_cum = [0] + list(accumulate(a))

ans = float("inf")
# 往復回数決め打ち
for k in range(1,n+1):
    cost = (n+k) * x
    i = 0
    while i*k <= n:
        cost += coef[i]*(a_cum[min((i+1)*k, n)] - a_cum[i*k])
        i += 1

    ans = min(ans, cost)
    
print(ans)
Buggy python submission (verdict: Runtime Error):
h,w = map(int, input().split())
field = [input() for _ in range(h)]

# 行を圧縮 
for hi in range(h-1):
    all_white = True
    if field[hi].count("#") > 0:
        all_white = False
    
    if all_white:
        del field[hi]
    
        
field_char = [list(field[hi]) for hi in range(len(field))]
# 列を圧縮
has_black_list = []
for wj in range(w):
    has_black = False
    for hi in range(len(field)):
        if field_char[hi][wj] == "#":
            has_black = True
            
    
    if has_black:
        has_black_list.append(wj)
        
field_ans = [["" for wj in range(len(has_black_list))] for hi in range(len(field))]
for j, wj in enumerate(has_black_list):
    for hi in range(len(field)):
        field_ans[hi][j] = field[hi][wj]
        
        

for hi in range(len(field)):
    print("".join(field_ans[hi]))

Fixed submission:
import sys
sdin = sys.stdin.readline

h,w = map(int, sdin().split())
field = []
for hi in range(h):
    field.append(sdin())
    
f2 = []
for hi in range(h):
    if field[hi].count("#") > 0:
        f2.append(field[hi])
    
f2_char = [list(f2[hi]) for hi in range(len(f2))]

# 列を圧縮
has_black_list = []
for wj in range(w):
    has_black = False
    for hi in range(len(f2)):
        if f2_char[hi][wj] == "#":
            has_black = True    
    
    if has_black:
        has_black_list.append(wj)
        
field_ans = [["" for wj in range(len(has_black_list))] for hi in range(len(f2))]
for j, wj in enumerate(has_black_list):
    for hi in range(len(f2)):
        field_ans[hi][j] = f2_char[hi][wj]
    

for hi in range(len(f2)):
    print("".join(field_ans[hi]))
Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())

print((a-1)*(b-1))

Fixed submission:
a,b = map(int, input().split())
print((a-1)*(b-1))
Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())

not1 = 0
for i in range(len(s)):
    if s[i] != "1":
        not1 = i
        first_not1 = s[i]
        break

if k <= not1:
    print(1)
else:
    print(first_not1)

Fixed submission:
s = input()
k = int(input())

#s = "11111"
#k = 5

not1 = len(s)
for i in range(len(s)):
    if s[i] != "1":
        not1 = i
        first_not1 = s[i]
        break

if k <= not1:
    print(1)
else:
    print(first_not1)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
import sys
sdin = sys.stdin.readline

n,m,q = map(int, sdin().split())

lr = []
for i in range(m):
    l,r = map(int, sdin().split())
    lr.append(tuple([l-1, r-1]))
        
pq = []
for _ in range(q):
    p,q = map(int, sdin().split())
    pq.append(tuple([p-1, q-1]))


# n×nフィールドを用意して各列車のl,r点をプロット
field = [[0 for _ in range(n)] for _ in range(n)]
for l, r in lr:
    field[l][r] += 1
    
# fieldのx方向の累積和を取る
field_cum = [list(accumulate(field[i])) for i in range(n)]    

# 各クエリに対し、rがpからqの間に入っているものを足し合わせる
for p,q in pq:
    ans = 0
    for i in range(p,q+1):
        ans += field_cum[i][q]
    print(ans)

Fixed submission:
from itertools import accumulate
import sys
sdin = sys.stdin.readline

n,m,q = map(int, sdin().split())

lr = []
for i in range(m):
    l,r = map(int, sdin().split())
    lr.append(tuple([l-1, r-1]))
        
pq = []
for _ in range(q):
    p,q = map(int, sdin().split())
    pq.append(tuple([p-1, q-1]))


# n×nフィールドを用意して各列車のl,r点をプロット
field = [[0 for _ in range(n)] for _ in range(n)]
for l, r in lr:
    field[l][r] += 1
    
# fieldのx方向の累積和を取る
field_cum = [list(accumulate(field[i])) for i in range(n)]    

# 各クエリに対し、rがpからqの間に入っているものを足し合わせる
for p,q in pq:
    ans = 0
    for i in range(p,q+1):
        ans += field_cum[i][q]
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
mod = 10**9+7

num_q = [0]
res = 0
for i in range(N):
    if S[i] == "?":
        res += 1
    num_q.append(res)

# それぞれi番目までのa,a-b,a-b-cの数
dp_a = [0]*(N+1)
dp_ab = [0]*(N+1)
dp_abc = [0]*(N+1)

for i in range(1,N+1):
    if S[i-1] == "A":
        dp_a[i] = dp_a[i-1] + 3**num_q[i-1]
        dp_ab[i] = dp_ab[i-1]
        dp_abc[i] = dp_abc[i-1]
        
    elif S[i-1] == "B":
        dp_a[i] = dp_a[i-1]
        dp_ab[i] = dp_ab[i-1] + dp_a[i-1]
        dp_abc[i] = dp_abc[i-1]
        
    elif S[i-1] == "C":
        dp_a[i] = dp_a[i-1]
        dp_ab[i] = dp_ab[i-1]
        dp_abc[i] = dp_abc[i-1] + dp_ab[i-1]
        
    elif S[i-1] == "?":
        dp_a[i] = dp_a[i-1] * 3 + 3**num_q[i-1]
        dp_ab[i] = dp_ab[i-1] * 3 + dp_a[i-1] 
        dp_abc[i] = dp_abc[i-1] * 3 + dp_ab[i-1]
        
    dp_a[i] %= mod
    dp_ab[i] %= mod
    dp_abc[i] %= mod
        
print(dp_abc[N])



Fixed submission:
S = input()
#S = "????C?????B??????A???????"
N = len(S)
mod = 10**9+7

num_q = [1]
res = 1
for i in range(N):
    if S[i] == "?":
        res = res*3 % mod
    num_q.append(res)
    

# それぞれi番目までのa,a-b,a-b-cの数
dp_a = [0]*(N+1)
dp_ab = [0]*(N+1)
dp_abc = [0]*(N+1)

for i in range(1,N+1):
    if S[i-1] == "A":
        dp_a[i] = dp_a[i-1] + num_q[i-1]
        dp_ab[i] = dp_ab[i-1]
        dp_abc[i] = dp_abc[i-1]
        
    elif S[i-1] == "B":
        dp_a[i] = dp_a[i-1]
        dp_ab[i] = dp_ab[i-1] + dp_a[i-1]
        dp_abc[i] = dp_abc[i-1]
        
    elif S[i-1] == "C":
        dp_a[i] = dp_a[i-1]
        dp_ab[i] = dp_ab[i-1]
        dp_abc[i] = dp_abc[i-1] + dp_ab[i-1]
        
    elif S[i-1] == "?":
        dp_a[i] = dp_a[i-1] * 3 + num_q[i-1]
        dp_ab[i] = dp_ab[i-1] * 3 + dp_a[i-1] 
        dp_abc[i] = dp_abc[i-1] * 3 + dp_ab[i-1]
        
    dp_a[i] %= mod
    dp_ab[i] %= mod
    dp_abc[i] %= mod
        
print(dp_abc[N])
Buggy python submission (verdict: Time Limit Exceeded):
def judge(a,b,c,d):
    #calc border
    if a-c<0:
        border = 0
    else:
        if (a-c)%b == 0:
            border = int((a-c)/b)
        else:           
            border = int((a-c)/b)+1

    start = a-border*b

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

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


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

Fixed submission:

def findGCD(a,b):
    if a%b == 0:
        return b
    else:
        return findGCD(b,a%b)


def judge(a,b,c,d):
    if a<b or d<b:
        return False
    elif c >= b:
        return True
    else:
        g = findGCD(b,d)
        thr = b-g+a%g
        if thr > c:
            return False
        else:
            return True
        
T = int(input())
ABCD = []
for i in range(T):
    ABCD.append(list(map(int, input().split())))
        
for a,b,c,d in ABCD:
    if judge(a,b,c,d):
        print("Yes")
    else:
        print("No")

Buggy python submission (verdict: Time Limit Exceeded):
def judge(a,b,c,d):
    #calc border
    if a-c<0:
        border = 0
    else:
        if (a-c)%b == 0:
            border = int((a-c)/b)
        else:           
            border = int((a-c)/b)+1

    start = a-border*b

    return start
    
def check(a,b,c,d):
    if a-b<0:
        return False
    if d<b:
        return False
    
    ans = judge(a,b,c,d)
    b_list = []
    while not ans in b_list:
        b_list.append(ans)
        if ans <= c:
            a = ans+d
        else:
            a = ans
        if a-b<0:
            return False

        ans = judge(a,b,c,d)
        if ans < 0:
            return False
    
    return True
 

N = int(input())
ABCD = []
for i in range(N):
    ABCD.append(map(int, input().split()))



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

Fixed submission:

def findGCD(a,b):
    if a%b == 0:
        return b
    else:
        return findGCD(b,a%b)


def judge(a,b,c,d):
    if a<b or d<b:
        return False
    elif c >= b:
        return True
    else:
        g = findGCD(b,d)
        thr = b-g+a%g
        if thr > c:
            return False
        else:
            return True
        
T = int(input())
ABCD = []
for i in range(T):
    ABCD.append(list(map(int, input().split())))
        
for a,b,c,d in ABCD:
    if judge(a,b,c,d):
        print("Yes")
    else:
        print("No")