Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
a,v = list(map(int,input().split()))
b,w = list(map(int,input().split()))
t = int(input())

output = "NO"

for i in range(t):
  if a > b:
    a -= v
    b -= w
  elif a < b:
    a += v
    b += w
  
  if a == b:
    output="YES"
    break

print(output)

Fixed submission:
a,v = list(map(int,input().split()))
b,w = list(map(int,input().split()))
t = int(input())

import sys
dis = abs(a-b)
tume = v-w 
if v <= w:
  print("NO")
  sys.exit()

if dis%tume == 0 and dis//tume <= t:
  print("YES")
elif dis <= (t-1)*tume:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
um_list = list(input().split())
a = int(num_list[0])
b = int(float(num_list[1])*100)

print((a*b)//100)

Fixed submission:
from decimal import Decimal
num_list = list(input().split())
a = Decimal(num_list[0])
b = Decimal(num_list[1])

print(int(a*b))

Buggy python submission (verdict: Runtime Error):
num_list = map(input().split())
a = int(num_list[0])
b = float(num_list[1])
print(int(a*b))


Fixed submission:
from decimal import Decimal
num_list = list(input().split())
a = Decimal(num_list[0])
b = Decimal(num_list[1])

print(int(a*b))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ln = input()
sec_set = set()
first_set = set()

for first in range(n):
    if ln[first] in first_set:
        continue
    first_set.add(ln[first])
    for second in range(first+1,n):
        for third in range(second+1,n):
            sec_set.add(ln[first]+ln[second]+ln[third])

print(len(sec_set))

Fixed submission:
n = input()
ln = input()

n=int(n)
sec_set = set()

for i in range(10):
    first = ln.find(str(i))
    if first == -1:
        continue
    for j in range(10):
        second = ln[(first+1):].find(str(j))
        if second == -1:
            continue
        for k in range(10):
            third = ln[(first + second+2):].find(str(k))
            if third == -1:
                continue
            else:
                sec_set.add(str(i)+str(j)+str(k))
print(len(sec_set))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ln = input()
sec_set = set()

for first in range(n):
    for second in range(first+1,n):
        for third in range(second+1,n):
            sec_set.add(ln[first]+ln[second]+ln[third])

print(len(sec_set))

Fixed submission:
n = input()
ln = input()

n=int(n)
sec_set = set()

for i in range(10):
    first = ln.find(str(i))
    if first == -1:
        continue
    for j in range(10):
        second = ln[(first+1):].find(str(j))
        if second == -1:
            continue
        for k in range(10):
            third = ln[(first + second+2):].find(str(k))
            if third == -1:
                continue
            else:
                sec_set.add(str(i)+str(j)+str(k))
print(len(sec_set))
Buggy python submission (verdict: Runtime Error):
n,m = list(map(int,input().split()))
l_list = [list(map(int,input().split())) for i in range(m)] 
p = list(map(int,input().split()))
output = 0

for i in range(2 ** n):
  lights = []
  for j in range(n+1):
    if ((i >> j) & 1):
      lights.append(j+1)
      
  for l in l_list:
    if len(set(l[1:]) & set(lights)) % 2 != p[l[0]-1] or len(set(l[1:]) & set(lights)) ==0:
      break
  else:
    output += 1
print(output)

Fixed submission:
n,m = list(map(int,input().split()))
l_list = [list(map(int,input().split())) for i in range(m)] 
p = list(map(int,input().split()))
output = 0

for i in range(2 ** n):
  lights = []
  for j in range(n+1):
    if ((i >> j) & 1):
      lights.append(j+1)
      
  for inde, l in enumerate(l_list):
    if len(set(l[1:]) & set(lights)) % 2 != p[inde]:
      break
  else:
    output += 1
print(output)

Buggy python submission (verdict: Runtime Error):
n,m = list(map(int,input().split()))
l_list = [list(map(int,input().split())) for i in range(m)] 
p = list(map(int,input().split()))
output = 0

for i in range(2 ** n):
  lights = []
  for j in range(n+1):
    if ((i >> j) & 1):
      lights.append(j+1)
      
  for l in l_list:
    if len(set(l[1:]) & set(lights)) % 2 != p[l[0]-1]:
      break
  else:
    output += 1
print(output)

Fixed submission:
n,m = list(map(int,input().split()))
l_list = [list(map(int,input().split())) for i in range(m)] 
p = list(map(int,input().split()))
output = 0

for i in range(2 ** n):
  lights = []
  for j in range(n+1):
    if ((i >> j) & 1):
      lights.append(j+1)
      
  for inde, l in enumerate(l_list):
    if len(set(l[1:]) & set(lights)) % 2 != p[inde]:
      break
  else:
    output += 1
print(output)
Buggy python submission (verdict: Runtime Error):

n=input()
a_1 = [int(i) for i in input().split()]
a_2 = [int(i) for i in input().split()]

output = 0
for i in range(1,n+1):
    tmp = sum(a_1[:i]) + sum(a_2[i-1:])
    if tmp > output:
        output = tmp

print(output)

Fixed submission:
n=int(input())
a_1 = [int(i) for i in input().split()]
a_2 = [int(i) for i in input().split()]

output = 0
for i in range(1,n+1):
    tmp = sum(a_1[:i]) + sum(a_2[i-1:])
    if tmp > output:
        output = tmp

print(output)
Buggy python submission (verdict: Runtime Error):
x = int(input())

#2回で3~11を作れる
count = 0
if x = 7:
  print(2)

count += (x//11)*2
x = x%11
if x<7:
  count += 1
elif x>7:
  count+=2
else:
  count += 3


print(count)

Fixed submission:
x = int(input())
#2回で3~11を作れる
count = 0
if x == 7:
  count=2
  x = 0
count += (x//11)*2
x = x%11
if x==0:
  count += 0
elif x>6:
  count+=2
else:
  count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
import re
output ="NO"
s = re.sub('dreamer', '', s)
s = re.sub('dream', '', s)
s = re.sub('eraser', '', s)
s = re.sub('erase', '', s)
if s == "":
    output = "YES"
print(output)

Fixed submission:
s = input()

import re
output ="NO"
s = re.sub('eraser', '', s)
s = re.sub('erase', '', s)
s = re.sub('dreamer', '', s)
s = re.sub('dream', '', s)
if s == "":
    output = "YES"
print(output)
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[list(map(int,list(input()))) for i in range(N)]

s=1
for i in range(N):
    s = s*A[i]
    if s > 10**18:
        s = -1
        print('-1')
        break
if s != -1:
    print(s)

Fixed submission:
N=int(input())
A=list(map(int, input().split()))

s=1
if min(A) == 0:
    s = 0
    print(s)
elif max(A) > 10**18:
    s = -1
    print(s)
else:
    for i in range(N):
        s = s*A[i]
        if s > 10**18:
            s = -1
            print('-1')
            break
    if s != -1:
        print(s)
Buggy python submission (verdict: Runtime Error):
C = str(input())

a = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in len(a):
    if a[i] == C:
        print(a[i+1])

Fixed submission:
C = str(input())

a = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in range(len(a)):
    if a[i] == C:
        print(a[i+1])
Buggy python submission (verdict: Time Limit Exceeded):
A,B,K = map(int,input().split())

for i in range(K):
    if A > 0:
        A = A - 1
    elif B > 0:
        B = B - 1

print("{} {}".format(A, B))

Fixed submission:
A,B,K = map(int,input().split())

K1 = K - A

if K >= A:
    A = 0
else:
    A = A - K

if K1 > 0:
    if B >= K1:
        B = B - K1
    else:
        B = 0
        

print("{} {}".format(A, B))
Buggy python submission (verdict: Runtime Error):
S=int(input())

count = 0

l=[]

l = list(S)

a = len(l)

b = a - 1

for i in range(a):
    if l[i] != l[b-i]:
        count = count + 1

count = count/2

print(int(count))

Fixed submission:
S=str(input())

count = 0

l=[]

l = list(S)

a = len(l)

b = a - 1

for i in range(a):
    if l[i] != l[b-i]:
        count = count + 1

count = count/2

print(int(count))

Buggy python submission (verdict: Runtime Error):
S=str(input())

count = 0

l = list(S)

a = len(S)

b = a - 1

for i in a:
    if l(i) != l(b-i):
        count = count + 1

count = count/2

print(count)

Fixed submission:
S=str(input())

count = 0

l=[]

l = list(S)

a = len(l)

b = a - 1

for i in range(a):
    if l[i] != l[b-i]:
        count = count + 1

count = count/2

print(int(count))
Buggy python submission (verdict: Runtime Error):
s=input()
n=len(s)
li=[]
c=0
for i in range(n):
	if(s[i]=='R'):
		c=c+1
    else:
		li.append(c)
		c=0
li.append(c)
print(max(li))

Fixed submission:
s=input()
n=len(s)
c=0
li=[]
for i in range(n):
  if(s[i]=='R'):
    c=c+1
  else:
    li.append(c)
    c=0
li.append(c)
print(max(li))
Buggy python submission (verdict: Time Limit Exceeded):
import math
a,b,n=map(int,input().split())
li=[]
for i in range(n+1):
    c=math.floor((a*i)/b)-(a*math.floor(i/b))
    li.append(c)
print(max(li))

Fixed submission:
import math
a,b,n=map(int,input().split())
d=min(n,b-1)
c=math.floor((a*d)/b)-(a*math.floor(d/b))
print(c)

Buggy python submission (verdict: Time Limit Exceeded):
import math
a,b,n=map(int,input().split())
li=[]
for i in range(n+1):
    c=math.floor((a*i)/b)-(a*math.floor(i/b))
    li.append(c)
print(max(li))
  

Fixed submission:
import math
a,b,n=map(int,input().split())
d=min(n,b-1)
c=math.floor((a*d)/b)-(a*math.floor(d/b))
print(c)
Buggy python submission (verdict: Runtime Error):
try:
  s,w=map(int,input().split())
  if(w>=s):
    print("unsafe")
   else:
    print("safe")
except:
  pass

Fixed submission:
s, w = map(int, input().split())
if w >= s:
  print('unsafe')
else:
  print('safe')
Buggy python submission (verdict: Runtime Error):
n = int(input())
d = {'AC':0,'WA':0,'TLE':0,'Re':0}
for j in range(n):
    i = input()
    d[i] += 1

for i in ['AC','WA','TLE','Re']:
    print('%s x %d' % (i, d[i]))

Fixed submission:
n = int(input())
d = {'AC':0,'WA':0,'TLE':0,'RE':0}
for j in range(n):
    i = input()
    d[i] += 1

for i in ['AC','WA','TLE','RE']:
    print('%s x %d' % (i, d[i]))
Buggy python submission (verdict: Runtime Error):
n,m,k = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))

def csum(l,ll):
    s = 0
    res = [0] * ll
    for i in range(ll):
        s += l[i]
        res[i] = s
    return res

csln = csum(ln,n)
cslm = csum(lm,m)

import numpy as np

lk = [k-x for x in csln if k-x >=0]
res = np.searchsorted(np.array(cslm), np.array(lk), side='right').
rmax = 0
for i,j in zip(range(1,len(lk)+1), res):
    rmax = max(rmax, i+j)
print(int(rmax))

Fixed submission:
n,m,k = map(int, input().split())
ln = list(map(int, input().split()))
lm = list(map(int, input().split()))

def csum(l,ll):
    s = 0
    res = [0] * ll
    for i in range(ll):
        s += l[i]
        res[i] = s
    return res

csln = csum(ln,n)
cslm = csum(lm,m)

import numpy as np

lk = [k] + [k-x for x in csln if k-x >=0]
res = np.searchsorted(np.array(cslm), np.array(lk), side='right')
rmax = 0
for i,j in zip(range(len(lk)+1), res):
    rmax = max(rmax, i+j)
print(rmax)
Buggy python submission (verdict: Runtime Error):
n = int(input())
d = {k:v for k,v in zip(range(26),'abcdefghijklmnopqrstuvwxyz')}
i = 1
while n > 26**i:
    n -= 26**i
    i += 1
res = ''
for j in range(i-1,-1,-1):
    jn, n = divmod(n, 26**j)
    if j >0:
        res += d[jn]
    else:
        res += d[jn-1]
print(res)

Fixed submission:
n = int(input())
d = {k:v for k,v in zip(range(26),'abcdefghijklmnopqrstuvwxyz')}
i = 1
while n > 26**i:
    n -= 26**i
    i += 1
res = ''
n -= 1
for j in range(i-1,-1,-1):
    jn, n = divmod(n, 26**j)
    res += d[jn]
print(res)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
n = int(input())
la = list(map(int, input().split()))
s = sum(la)
d = dict(Counter(la))

q = int(input())
for _ in range(q):
    b,c = map(int, input().split())
    nb = d.pop(b)
    delta = (c-b)*nb
    s += delta
    print(s)
    if c in d:
        d[c] += nb
    else:
        d[c] = nb

Fixed submission:
from collections import Counter
n = int(input())
la = list(map(int, input().split()))
s = sum(la)
d = dict(Counter(la))

q = int(input())
for _ in range(q):
    b,c = map(int, input().split())
    if b in d:
        nb = d.pop(b)
    else:
        nb = 0
    delta = (c-b)*nb
    s += delta
    print(s)
    if nb > 0:
        if c in d:
            d[c] += nb
        else:
            d[c] = nb
Buggy python submission (verdict: Runtime Error):
x,n = map(int, input().split())
l = list(map(int, input().split()))
l_dis = [num - x for num in l]

def s_func(x):
    if x < 0:
        return abs(x) - 0.5
    else:
        return abs(x)
    
l_dis = sorted(l_dis, key=s_func)
if not l_dis:
    print(x)
elif l_dis[0] != 0:
    print(x)
else:
    i = 1
    j = 1
    b = -1
    while True:
        if l_dis[i] != j*b:
            print(x + j*b)
            break
        i += 1
        b *= -1
        if l_dis[i] != j*b:
            print(x + j*b)
            break
        i += 1
        b *= -1
        j += 1

Fixed submission:
x,n = map(int, input().split())
l = list(map(int, input().split()))
l_dis = [num - x for num in l]

def s_func(x):
    if x < 0:
        return abs(x) - 0.5
    else:
        return abs(x)
    
l_dis = sorted(l_dis, key=s_func)
if len(l_dis)==0:
    print(x)
elif l_dis[0] != 0:
    print(x)
else:
    i = 1
    j = 1
    b = -1
    while True:
        if i > len(l_dis)-1 or l_dis[i] != j*b:
            print(x + j*b)
            break
        i += 1
        b *= -1
        if i > len(l_dis)-1 or l_dis[i] != j*b:
            print(x + j*b)
            break
        i += 1
        b *= -1
        j += 1
Buggy python submission (verdict: Runtime Error):
n, m = map(int,input().split())
p = [[] for x in range(n)]
r = [-2] + [-1] * (n - 1)
for _ in range(m):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    p[a].append(b)
    p[b].append(a)
def bfs(room):
    global r
    l = []
    for iroom in p[room]:
        if r[iroom] == -1:
            r[iroom] = room
            l.append(iroom)
    for iroom in l:
        bfs(iroom)
bfs(0)

if -1 in r:
    print('No')
else:
    print('Yes')
    for i in r[1:]:
        print(i+1)

Fixed submission:
n, m = map(int,input().split())
p = [[] for x in range(n)]
r = [-2] + [-1] * (n - 1)
for _ in range(m):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    p[a].append(b)
    p[b].append(a)

from collections import deque
dq = deque()
dq.append(0)

def bfs(room):
    l = []
    global r
    for iroom in p[room]:
        if r[iroom] == -1:
            r[iroom] = room
            l.append(iroom)
    return l

while len(dq) > 0:
    c = dq.popleft()
    ll = bfs(c)
    dq.extend(ll)

if -1 in r:
    print('No')
else:
    print('Yes')
    for i in r[1:]:
        print(i+1)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
r = 0
b = [0]*2019
m = b[:]
for i in range(3,l):
    m2 = b[:]
    for j in range(1,2019):
        j2 = (j*10+int(s[i])) % 2019
        m2[j2] += m[j]
    m2[int(s[i-3:i+1]) % 2019] += 1
    r += m2[0]
    m = m2
print(r)

Fixed submission:
s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
r = 0
b = [0]*2019
m = b[:]
for i in range(3,l):
    m2 = b[:]
    for j in range(1,2019):
        j2 = (j*10+int(s[i])) % 2019
        m2[j2] += m[j]
    m2[int(s[i-3:i+1]) % 2019] += 1
    r += m2[0]
    m = m2[:]
print(r)

Fixed submission:
s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
r = 0
m = [0]*2019
for i in range(3,l):
    num = int(s[i])
    m2 = [0]*2019
    for j in range(1,2019):
        j2 = (j*10+num) % 2019
        m2[j2] += m[j]
    m2[int(s[i-3:i+1]) % 2019] += 1
    r += m2[0]
    m2[0] = 0
    m = m2.copy()
print(r)

Fixed submission:
s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
r = 0
m = []
for i in range(3,l):
    num = int(s[i])
    m = [(x*10+num) % 2019 for x in m]
    m.append(int(s[i-3:i+1])%2019)
    r += m.count(0)
print(r)

Fixed submission:
s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
l = len(s)
r = 0
for i in range(l-3):
    for j in range(i+3,l):
        n = int(s[i:j+1])
        if n % 2019 == 0:
            r += 1
print(r)

Fixed submission:
s = input()
s_r = s[::-1]
lmod = [0]
cmod = 0
for n,i in enumerate(s_r):
    cmod += int(i)*pow(10,n,2019) % 2019
    cmod = cmod % 2019
    lmod.append(cmod)

from collections import Counter
r = 0
for _,i in Counter(lmod).items():
    r += i*(i-1)/2

print(int(r))
Buggy python submission (verdict: Runtime Error):
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(int,input().split()))
lst_r = sorted(lst_r, ascending=False)[:x]
lst_g = sorted(lst_g, ascending=False)[:y]
lst_all = sorted(lst_r + lst_g + lst_n, ascending=False)[:x+y]
print(sum(lst_all))

Fixed submission:
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

while len(lst_n)>0:
    flg = lst_r > lst_g
    cur = -1 * heappop(lst_n)
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
    else:
        break
print(sum(lst_g)+sum(lst_r))

Buggy python submission (verdict: Runtime Error):
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

flg = lst_r > lst_g


while len(lst_n)>=0:
    cur = -1 * heappop(lst_n)
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
        flg = lst_r > lst_g
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
        flg = lst_r > lst_g
    else:
        break
print(sum(lst_g)+sum(lst_r))

Fixed submission:
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

while len(lst_n)>0:
    flg = lst_r > lst_g
    cur = -1 * heappop(lst_n)
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
    else:
        break
print(sum(lst_g)+sum(lst_r))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
q = int(input())
for i in range(q):
    ii = input().split()
    if ii[0] == '1':
        s = s[::-1]
    else:
        if ii[1] == '1':
            s = ii[2] + s
        else:
            s += ii[2]
print(s)

Fixed submission:
s = input()
q = int(input())
seq1 = ''
seq2 = ''
flg = 1
for i in range(q):
    ii = input().split()
    if ii[0] == '1':
        seq1, seq2 = seq2, seq1
        flg *= -1
    else:
        if ii[1] == '1':
            seq1 += ii[2]
        else:
            seq2 += ii[2]
print(seq1[::-1] + s[::flg] + seq2)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

n, p = map(int,input().split())
s = input()

res = 0

if p not in [2,5]:
    crem = 0
    dic_p = defaultdict(lambda : 0)
    for i,subs in enumerate(s[::-1]):
        crem += (int(subs) * 10**i) % p
        crem %= p

        if crem == 0:
            res += 1
        res += dic_p[crem]

        dic_p[crem] += 1
        
else:
    for i,subs in enumerate(s[::-1]):
        if int(subs) % p == 0:
            res += n - i
    
print(res)

Fixed submission:
from collections import defaultdict

n, p = map(int,input().split())
s = input()

res = 0

if p not in [2,5]:
    crem = 0
    
    crem_10 = [1]
    for i in range(1,n):
        crem_10 += [(crem_10[-1] * (10%p)) % p]
        
    dic_p = defaultdict(lambda : 0)
    for i,subs in enumerate(s[::-1]):
        crem += (int(subs) % p * crem_10[i]) % p
        crem %= p

        if crem == 0:
            res += 1
        res += dic_p[crem]

        dic_p[crem] += 1
        
else:
    for i,subs in enumerate(s[::-1]):
        if int(subs) % p == 0:
            res += n - i
    
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()[::-1]
res = 0
for start in range(n):
    num = 0
    for i,end in enumerate(range(start,n)):
        num += (int(s[end]) * 10**i)
        num %= p
        if num == 0:
            res += 1
print(res)

Fixed submission:
from collections import defaultdict

n, p = map(int,input().split())
s = input()

res = 0

if p not in [2,5]:
    crem = 0
    
    crem_10 = [1]
    for i in range(1,n):
        crem_10 += [(crem_10[-1] * (10%p)) % p]
        
    dic_p = defaultdict(lambda : 0)
    for i,subs in enumerate(s[::-1]):
        crem += (int(subs) % p * crem_10[i]) % p
        crem %= p

        if crem == 0:
            res += 1
        res += dic_p[crem]

        dic_p[crem] += 1
        
else:
    for i,subs in enumerate(s[::-1]):
        if int(subs) % p == 0:
            res += n - i
    
print(res)
Buggy python submission (verdict: Time Limit Exceeded):
n,a,b = map(int,input().split())
mod = 10**9+7

lfac = [1,1]
ifac = 1
for i in range(2,n+1):
    ifac = (ifac * i) % mod
    lfac.append(ifac)
    
def comb(n,m):
    res1 = pow(lfac[m],(mod-2),mod)
    res2 = pow(lfac[n-m],(mod-2),mod)
    return (((lfac[n] * res1) % mod) * res2) % mod

res = pow(2,n,mod)
res -= 1 + comb(n,a) + comb(n,b) + mod
print(res % mod)

Fixed submission:
from functools import reduce

n, a, b = map(int, input().split())
mod = 10**9 + 7

def fast_comb(a,b,mod):
    up = reduce(lambda x,y: x*y%mod, range(a,a-b,-1))
    down = reduce(lambda x,y: x*y%mod, range(1,b+1))
    return (up * pow(down, mod-2, mod)) % mod

print((pow(2,n,mod) - fast_comb(n,a,mod) - fast_comb(n,b,mod) - 1) % mod)