Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input().split())))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)


Fixed submission:
x,y,a,b,c=map(int,input().split())
p,q,r=[list(reversed(sorted(list(map(int,input().split()))))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Buggy python submission (verdict: Runtime Error):
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input())))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)


Fixed submission:
x,y,a,b,c=map(int,input().split())
p,q,r=[list(reversed(sorted(list(map(int,input().split()))))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Buggy python submission (verdict: Runtime Error):
x,y,a,b,c=map(int,input().split())
p,q,r=[reversed(sorted(list(map(int,input()))) for i in range(3))]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)

Fixed submission:
x,y,a,b,c=map(int,input().split())
p,q,r=[list(reversed(sorted(list(map(int,input().split()))))) for i in range(3)]
p=p[0:x]
q=q[0:y]
p.append(0)
q.append(0)
r.append(0)

s=0
i,j,k=0,0,0

for _ in range(x+y):
  if p[i]>=q[j] and p[i]>=r[k]:
    s+=p[i]
    i+=1
  elif q[j]>=r[k]:
    s+=q[j]
    j+=1
  else:
    s+=r[k]
    k+=1
    
print(s)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))

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

lcm = 1
for v in a:
    lcm = lcm * v // gcd(lcm, v)

MOD = 1000000007
ans = 0
for v in a:
    ans += lcm // v
    ans %= MOD;

print(ans)


Fixed submission:
MOD = 10**9+7

n = int(input())
a = list(map(int,input().split()))

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

def lcm(a,b):
    return a * b // gcd(a,b)

LCM = 1
for v in a:
    LCM = lcm(LCM,v)

LCM %= MOD

def pow(a,n,mod):
    ret = 1
    tmp = a
    while n:
        if n & 1:
            ret *= tmp
            ret %= mod
        tmp *= tmp
        tmp %= mod
        n >>= 1
    return ret

def inv(a,mod):
    return pow(a,mod-2,mod)


ans = 0
for v in a:
    ans += LCM * inv(v,MOD)
    ans %= MOD;

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from fractions import gcd
 
 
N, M = list(map(int, input().split()))
S = input()
T = input()
N = len(S)
M = len(T)
s = []
t = []
L = int(N * M / gcd(N, M))
 
 
x = [0 for i in range(L)]
 
tmplen = N if N >= M else M
 
if tmplen == N:
    for i in range(tmplen):
        num = int(i * (L / N))
        x[num] = S[i]
    otherlen = M
    for j in range(otherlen):
        num = int(j * (L/M))
        if x[num] != 0 and x[num] != T[j]:
            L = -1
            break
else:
    for i in range(tmplen):
        num = int(i * (L / M))
        x[num] = T[i]
    otherlen = N
    for j in range(otherlen):
        num = int(j * (L/N))
        if x[num] != 0 and x[num] != S[j]:
            L = -1
            break
print(L)


Fixed submission:
import numpy as np
from fractions import gcd
 
 
N, M = list(map(int, input().split()))
S = input()
T = input()

g = gcd(N, M)
l = int(N * M / g)

for i in range(g):
    if S[int(N/g*i)] != T[int(M/g*i)]:
        l = -1
        break

print(l)
Buggy python submission (verdict: Runtime Error):
n = int(input())
r = int(input())
 
if r < 10:
  print(n-(100*(10-r)))
else:
  print(n)


Fixed submission:
n,r = map(int,input().split())
 
if n < 10:
  print(r+(100*(10-n)))
else:
  print(r)

Buggy python submission (verdict: Runtime Error):
n = int(input())
r = int(input())
 
if n < 10:
  print(n-(100*(10-r)))
else:
  print(n)


Fixed submission:
n,r = map(int,input().split())
 
if n < 10:
  print(r+(100*(10-n)))
else:
  print(r)

Buggy python submission (verdict: Runtime Error):
n = int(input())
r = int(input())
 
if n < 10:
  print(n-(100*(10-k)))
else:
  print(n)

Fixed submission:
n,r = map(int,input().split())
 
if n < 10:
  print(r+(100*(10-n)))
else:
  print(r)

Buggy python submission (verdict: Runtime Error):
n = int(input())
r = int(input())

if n < 10:
  print(n-(100*(10-n)))
else:
  print(n)

Fixed submission:
n,r = map(int,input().split())
 
if n < 10:
  print(r+(100*(10-n)))
else:
  print(r)
Buggy python submission (verdict: Runtime Error):
n=int(input())
k=int(input())
cnt = 0

while(n > 0):
  n /= k
  cnt += 1

print(cnt)

Fixed submission:
n,k=map(int,input().split())

cnt = 0

while(n > 0):
  n //= int(k)
  cnt += 1
  int(n)

print(cnt)
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())
h = list(map(int,input().split()))

h.sort()

for i in range(k):
    h[n-1-i] = 0

s = sum(h)

print(s)


Fixed submission:
n,k = map(int,input().split())
h = list(map(int,input().split()))

h.sort()

if k > n:
    print(0)
    exit()

for i in range(k):
    h[n-i-1] = 0

s = sum(h)

print(s)
Buggy python submission (verdict: Runtime Error):
k,x = int(input().split())

if 500 * k > x :
    print("Yes")
else :
    print("No")

Fixed submission:
k,x = map(int,input().split())

if k * 500 >= x:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int,input().split())
     
for i in range(k):
  if a == 0 and b == 0:
    break
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1
     
print(str(a) + " " + str(b))

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

A -= K
if(A < 0):
    B -= abs(A)
    A = 0
    if(B < 0):
        B = 0

print(str(A) + " " + str(B))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int,input().split())
     
for i in range(k):
  if a == b == 0 : break
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1
     
print(str(a) + " " + str(b))

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

A -= K
if(A < 0):
    B -= abs(A)
    A = 0
    if(B < 0):
        B = 0

print(str(A) + " " + str(B))

Buggy python submission (verdict: Time Limit Exceeded):
a,b,k = map(int,input().split())
     
for i in range(k):
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1
     
print(str(a) + " " + str(b))

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

A -= K
if(A < 0):
    B -= abs(A)
    A = 0
    if(B < 0):
        B = 0

print(str(A) + " " + str(B))

Buggy python submission (verdict: Runtime Error):
a,b,k = input().split()

for i in range(k):
  if a > 0:
    a -= 1
  elif b > 0:
    b -= 1

print(a + " " + b)

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

A -= K
if(A < 0):
    B -= abs(A)
    A = 0
    if(B < 0):
        B = 0

print(str(A) + " " + str(B))
Buggy python submission (verdict: Runtime Error):
import math

A,B = map(int,input().split())

R = (A * B) / math.gcd(A, B)

print(int(R))

Fixed submission:

A,B = map(int,input().split())

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

R = (A * B) / gcd(A, B)

print(int(R))

Buggy python submission (verdict: Runtime Error):
import math

A,B = map(int,input().split())

R = (A * B) // math.gcd(A, B)

print(R)

Fixed submission:

A,B = map(int,input().split())

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

R = (A * B) / gcd(A, B)

print(int(R))
Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
C = int(input())

C = C - (A - B)
if C < 0:
    C = 0
print(C)

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

C = C - (A - B)
if C < 0:
    C = 0
print(C)

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
C = int(input())

C =- (A - B)
print(C)

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

C = C - (A - B)
if C < 0:
    C = 0
print(C)

Buggy python submission (verdict: Runtime Error):
A = imput()
B = imput()
C = imput()

C =- (A - B)
print(C)

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

C = C - (A - B)
if C < 0:
    C = 0
print(C)
Buggy python submission (verdict: Runtime Error):
a = input().split()
h, n = int(a[0]), int(a[1])
nums = input().split()
nums = list(map(int, nums))
x = max(nums)
xx = nums.index(x)
del nums[xx]
y = max(nums)
z = x + y

if z >= h:
  print("yes")
else:
  print("no")

Fixed submission:
a = input().split()
h, n = int(a[0]), int(a[1])
nums = input().split()
nums = list(map(int, nums))
x = h - sum(nums)
if x <= 0:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Runtime Error):
a = input().split()
h, n = int(a[0]), int(a[1])
nums = input().split()
nums = list(map(int, nums))
nums = sorted(nums, reverse=True)
x = nums[0] + nums[1]
if x >= h:
  print("yes")
else:
  print("no")

Fixed submission:
a = input().split()
h, n = int(a[0]), int(a[1])
nums = input().split()
nums = list(map(int, nums))
x = h - sum(nums)
if x <= 0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)


def make_divisors(mn, n):
    divisors = [1]
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            if i >= mn:
                divisors.append(i)
            if i*i != n and n//i >= mn:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


N = int(input())
A = list(map(int, input().split()))
setA = set(A)
countA = Counter(A)
if countA[1] == 1:
    print(1)
    exit()

minA = min(A)

count = 0
for num in A:
    if countA[num] >= 2:
        continue
    divisors = make_divisors(minA, num)
    # print(num, divisors)
    if any((e in setA) for e in divisors):
        continue
    count += 1

print(count)


Fixed submission:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)


N = int(input())
A = list(map(int, input().split()))
A.sort()
countA = Counter(A)
maxA = A[-1]
# if countA[1] == 1:
#     print(1)
#     exit()

ef = [True]*(maxA+1)
count = 0
prev = -1
for num in A:
    if num == prev:
        continue
    if ef[num] and countA[num] == 1:
        count += 1

    b = 2*num
    while b <= maxA:
        ef[b] = False
        b += num

    prev = num

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)


def make_divisors(n):
    divisors = [1]
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i*i != n:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


N = int(input())
A = list(map(int, input().split()))
setA = set(A)
countA = Counter(A)
if countA[1] == 1:
    print(1)
    exit()

count = 0
for num in A:
    if countA[num] >= 2:
        continue
    divisors = make_divisors(num)
    if all((e not in setA) for e in divisors):
        count += 1

print(count)


Fixed submission:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
# from fractions import gcd

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)


N = int(input())
A = list(map(int, input().split()))
A.sort()
countA = Counter(A)
maxA = A[-1]
# if countA[1] == 1:
#     print(1)
#     exit()

ef = [True]*(maxA+1)
count = 0
prev = -1
for num in A:
    if num == prev:
        continue
    if ef[num] and countA[num] == 1:
        count += 1

    b = 2*num
    while b <= maxA:
        ef[b] = False
        b += num

    prev = num

print(count)
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
MOD = 10**9+7
# MOD = 998244353

n = int(input())
E = [list(map(int, input().split())) for i in range(n)]
d = defaultdict(int)
for e in E:
    if e[0] == 0 and e[1] == 0:
        d[(0, 0)] += 1
    elif e[0] == 0:
        k = 1 if e[1] > 0 else -1
        d[(0, k)] += 1
    elif e[1] == 0:
        k = 1 if e[0] > 0 else -1
        d[(k, 0)] += 1
    else:
        gcd = math.gcd(e[0], e[1])
        a, b = e[0]//gcd, e[1]//gcd
        if b < 0:
            a, b = -a, -b
        d[(a, b)] += 1

pd = 1
non = n
for k in d:
    if k[0] >= 0 and (-k[1], k[0]) in d:
        a, b = d[k], d[(-k[1], k[0])]
        pd = (pd*(pow(2, a, MOD) + pow(2, b, MOD) - 1))%MOD
        # print(a, b, pd)
        non -= (a+b)

# print(non, pd)
total = (pow(2, non, MOD)*pd - 1)%MOD
print(total)


Fixed submission:
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
MOD = 10**9+7
# MOD = 998244353

n = int(input())
E = [list(map(int, input().split())) for i in range(n)]
d = defaultdict(int)
for e in E:
    if e[0] == 0 and e[1] == 0:
        d[(0, 0)] += 1
    elif e[0] == 0:
        d[(0, 1)] += 1
    elif e[1] == 0:
        d[(1, 0)] += 1
    else:
        g = math.gcd(e[0], e[1])
        a, b = e[0]//g, e[1]//g
        if b < 0:
            a, b = -a, -b
        d[(a, b)] += 1

pd = 1
non = n - d[(0, 0)]
for k in d:
    if k == (0, 0) or k == (1, 0):
        continue

    elif k == (0, 1) and (1, 0) in d:
        a, b = d[(0, 1)], d[(1, 0)]
        pd = (pd * (pow(2, a, MOD) + pow(2, b, MOD) - 1)) % MOD
        non -= (a + b)

    elif k[0] > 0 and (-k[1], k[0]) in d:
        a, b = d[k], d[(-k[1], k[0])]
        pd = (pd*(pow(2, a, MOD) + pow(2, b, MOD) - 1))%MOD
        # print(a, b, pd)
        non -= (a+b)

# print(non, pd)
total = (pow(2, non, MOD)*pd - 1 + d[(0, 0)])%MOD
print(total)
Buggy python submission (verdict: Runtime Error):
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
# MOD = 10**9+7
# MOD = 998244353


def count(idx, s):
    left = right = 0
    for i in range(len(s)):
        if s[i] == "(":
            left += 1
        else:
            if left > 0:
                left -= 1
            else:
                right += 1

    return left, right, idx


n = int(input())
S = [input() for i in range(n)]
A = [count(i, s) for i, s in enumerate(S)]
A.sort(key=lambda x: (x[1], x[1]-x[0]))

left_rem = 0
for left, right in A:
    left_rem -= right
    if left_rem < 0:
        print("No")
        exit()
    left_rem += left

if left_rem == 0:
    print("Yes")
else:
    print("No")




Fixed submission:
def count(i, s):
    left = right = 0
    for e in s:
        if e == "(":
            left += 1
        else:
            if left == 0:
                right += 1
            else:
                left -= 1

    return left-right, left, right, i


n = int(input())
S = [input() for i in range(n)]
s1 = []
s2 = []
for i, e in enumerate(S):
    c = count(i, e)
    # print(i, c)
    if c[0] >= 0:
        s1.append((c[2], c[3]))
    else:
        s2.append((-c[1], c[3]))

s1.sort()
s2.sort()
# print(s1, s2)
s = []
for e in s1:
    s.append(S[e[1]])
for e in s2:
    s.append(S[e[1]])
# print(s)

_, left, right, _ = count(0, "".join(s))
if left == right == 0:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
a,b,n = map(int, input().split())

if n < b:
    print(math.floor(a*n/b) - a*math.floor(n/b))
else:
    print(math.floor(a * (b-1) / b) - a * math.floor((b-1) / b))


Fixed submission:
import math

a,b,n = map(int, input().split())

if n < b:
    print(math.floor(a*n/b) - a*math.floor(n/b))
else:
    print(math.floor(a * (b-1) / b) - a * math.floor((b-1) / b))
Buggy python submission (verdict: Runtime Error):
n, k, c = map(int, input().split())
S = input()
pre = [0]*n
pre[0] = 1 if S[0] == "o" else 0
for i in range(1, n):
    if S[i] == "o" and i-c-1 >= 0:
        pre[i] = max(pre[i-1], pre[i-c-1]+1)
    else:
        pre[i] = pre[i-1]

suf = [0]*n
suf[-1] = 1 if S[-1] == "o" else 0
for i in range(n-2, -1, -1):
    if S[i] == "o" and i+c+1 < n:
        suf[i] = max(suf[i+1], suf[i+c+1]+1)
    else:
        suf[i] = suf[i+1]

# print(pre)
# print(suf)

if S[0] == "o" and suf[1] < k:
    print(1)

for i in range(1, n-1):
    if S[i] == "o" and pre[i-1]+suf[i+1] < k:
        print(i+1)

if S[-1] == "o" and pre[-2] < k:
    print(n)


Fixed submission:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**8)

n, k, c = map(int, input().split())
s = input()
left = [-1]*k
right = [-1]*k

d = w = 0
while w < k:
    if s[d] == "o":
        left[w] = d
        w += 1
        d += c+1
    else:
        d += 1

d, w = n-1, k-1
while w >= 0:
    if s[d] == "o":
        right[w] = d
        w -= 1
        d -= c+1
    else:
        d -= 1

# print(left, right)
for i in range(k):
    if left[i] == right[i]:
        print(left[i]+1)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
from fractions import gcd

NN = 202020
# MOD = 10**9+7
MOD = 998244353
INF = float("inf")

n, k = map(int, input().split())
A = list(map(int, input().split()))
dp = [[[0]*3 for _ in range(k+1)] for _ in range(n+1)]
dp[0][0][0] = 1
for i in range(n):
    for j in range(k+1):
        dp[i+1][j][0] = (dp[i+1][j][0] + dp[i][j][0])%MOD
        dp[i+1][j][1] = (dp[i+1][j][1] + dp[i][j][0] + dp[i][j][1])%MOD
        dp[i+1][j][2] = (dp[i+1][j][2] + dp[i][j][0] + dp[i][j][1] + dp[i][j][2])%MOD
        if j + A[i] <= k:
            dp[i+1][j+A[i]][1] = (dp[i+1][j+A[i]][1] + dp[i][j][0] + dp[i][j][1])%MOD
            dp[i+1][j+A[i]][2] = (dp[i+1][j+A[i]][2] + dp[i][j][0] + dp[i][j][1])%MOD

print(dp[n][k][2])


Fixed submission:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

# MOD = 10 ** 9 + 7
MOD = 998244353
# sys.setrecursionlimit(10**8)

n, s = map(int, input().split())
A = list(map(int, input().split()))
dp = [[0]*(s+1) for _ in range(n+1)]

for i in range(n+1):
    dp[i][0] = i + 1

ans = 0
for i in range(n):
    for j in range(s + 1):
        if s > j > 0:
            dp[i + 1][j] += dp[i][j]
        if j + A[i] <= s:
            dp[i + 1][j + A[i]] += dp[i][j]
            dp[i + 1][j + A[i]] %= MOD

    ans += dp[i + 1][s] * (n - i)
    ans %= MOD

print(ans)
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
if a+b+2*math.sqrt(a)*math.sqrt(b) < c:
    print("Yes")
else:
    print("No")

Fixed submission:
a, b, c = map(int, input().split())
if c-a-b < 0:
    print("No")
elif 4*a*b < (c-a-b)**2:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
NN = 2020202
MOD = 10**9+7
INF = float("inf")


n, p = map(int, input().split())
S = input()
pl = len(str(p))
ans = 0
d = [0]*p

for i in range(len(S)):
    d[0] += 1
    nd = [0]*p
    a = int(S[i])
    for k, v in enumerate(d):
        ne = (k*10+a)%p
        nd[ne] += v
    ans += nd[0]
    d = nd
    # print(d)

print(ans)


Fixed submission:
NN = 2020202
MOD = 10**9+7
INF = float("inf")

n, p = map(int, input().split())
S = input()
pl = len(str(p))

if p == 2 or p == 5:
    count = 0
    for i in range(len(S)):
        if int(S[i])%p == 0:
            count += i+1
            # print(int(S[i]), count)

    print(count)

else:
    d = [0] * p
    d[0] = 1
    val = 0
    dec = 1

    for i in range(len(S)-1, -1, -1):
        val = (int(S[i])*dec+val)%p
        dec = (dec*10)%p
        d[val] += 1

    # print(d)
    ans = 0
    for e in d:
        ans += (e*(e-1))//2
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, Counter, namedtuple, deque
from bisect import bisect_left
from functools import reduce
import math

NN = 2020202
MOD = 10**9+7
INF = float("inf")


n, p = map(int, input().split())
S = input()
pl = len(str(p))
ans = 0
d = defaultdict(int)

for i in range(len(S)):
    d[0] += 1
    nd = defaultdict(int)
    for k,v in d.items():
        ne = (k*10+int(S[i]))%p
        if ne == 0:
            ans += v
        nd[ne] += v
    d = nd
    # print(d)

print(ans)


Fixed submission:
NN = 2020202
MOD = 10**9+7
INF = float("inf")

n, p = map(int, input().split())
S = input()
pl = len(str(p))

if p == 2 or p == 5:
    count = 0
    for i in range(len(S)):
        if int(S[i])%p == 0:
            count += i+1
            # print(int(S[i]), count)

    print(count)

else:
    d = [0] * p
    d[0] = 1
    val = 0
    dec = 1

    for i in range(len(S)-1, -1, -1):
        val = (int(S[i])*dec+val)%p
        dec = (dec*10)%p
        d[val] += 1

    # print(d)
    ans = 0
    for e in d:
        ans += (e*(e-1))//2
    print(ans)