Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sp = [0]*n
  sp[n-1] = s[n-1]%p
  cnt[sp[n-1]] += 1
  for i in range(1,n):
    sp[n-i-1] = (sp[n-i]+s[n-i-1]*(10**i%p))%p
    cnt[sp[n-i-1]] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)

Fixed submission:
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sum = 0
  for i in range(n):
    sum = (sum+s[n-i-1]*pow(10,i,p))%p
    cnt[sum] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sp = [0]*n
  sp[n-1] = s[n-1]%p
  for i in range(1,n):
    sp[n-i-1] = (sp[n-i]+s[n-i-1]*(10**i%p))%p
  cj = sp.count(0)
  ans += int(cj*(cj+1)/2)
  for j in range(1,p):
      cj = sp.count(j)
      ans += int(cj * (cj-1)/2)
print(ans)

Fixed submission:
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sum = 0
  for i in range(n):
    sum = (sum+s[n-i-1]*pow(10,i,p))%p
    cnt[sum] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
ans = 0

if p == 2:
  for i in range(n):
    if s[i] == '0' or s[i] == '2' or s[i] == '4' or s[i] == '6' or s[i] == '8' :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] == '0' or s[i] == '5':
      ans += i + 1
else:
  sp = [0]*n
  for i in range(n):
    sp[i] = int(s[i:n])%p
  cj = sp.count(0)
  ans += int(cj*(cj+1)/2)
  for j in range(1,p):
      cj = sp.count(j)
      ans += int(cj * (cj-1)/2)
print(ans)

Fixed submission:
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sum = 0
  for i in range(n):
    sum = (sum+s[n-i-1]*pow(10,i,p))%p
    cnt[sum] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = list(input())
ans = 0

for i in range(n):
  for j in range(i,n):
    if int(''.join(s[i:j+1]))%p == 0:
      ans += 1
print(ans)

Fixed submission:
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sum = 0
  for i in range(n):
    sum = (sum+s[n-i-1]*pow(10,i,p))%p
    cnt[sum] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
ans = 0

for i in range(n):
  for j in range(i,n):
    if int(s[i:j+1])%p == 0:
      ans += 1
print(ans)

Fixed submission:
n,p = map(int,input().split())
s = list(map(int,list(input())))
ans = 0
cnt = [0]*p
cnt[0] = 1

if p == 2:
  for i in range(n):
    if s[i] in [0,2,4,6,8] :
      ans += i + 1
elif p == 5:
  for i in range(n):
    if s[i] in [0,5]:
      ans += i + 1
else:
  sum = 0
  for i in range(n):
    sum = (sum+s[n-i-1]*pow(10,i,p))%p
    cnt[sum] += 1
  for j in range(p):
    cj = cnt[j]
    ans += cj * (cj-1)//2
print(ans)