Buggy python submission (verdict: Time Limit Exceeded):
# from collections import Counter
n, p = map(int, input().split())
s = input()
s += '0'
p_cnt = 0

div_dic = dict(zip(range(p), [0] * p))

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            p_cnt += i+1
else:
    for i in range(n+1):
        div_dic[int(s[i:]) % p] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
print(int(p_cnt))

Fixed submission:
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
        
else:
    s = s[::-1]
    div_dic = dict(zip(range(p), [0] * p))
    tmp = 0

    for i in range(n):
        tmp += s[i] * pow(10, i, p)
        tmp %= p
        div_dic[tmp] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
    p_cnt += div_dic.get(0)

print(p_cnt)

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

div_dic = dict(zip(range(p), [0] * p))

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            p_cnt += i+1
else:
    for i in range(n+1):
        div_dic[int(s[i:]) % p] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1) / 2



print(int(p_cnt))

Fixed submission:
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
        
else:
    s = s[::-1]
    div_dic = dict(zip(range(p), [0] * p))
    tmp = 0

    for i in range(n):
        tmp += s[i] * pow(10, i, p)
        tmp %= p
        div_dic[tmp] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
    p_cnt += div_dic.get(0)

print(p_cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n, p = map(int, input().split())
s = input()
s += '0'
p_cnt = 0
div_list = []

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            p_cnt += i+1
else:
    for i in range(n+1):
        num = int(s[i:])
        div_list.append(num % p)

    div_ol = Counter(div_list)
    for v in div_ol.values():
        p_cnt += v * (v - 1) / 2



print(int(p_cnt))

Fixed submission:
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
        
else:
    s = s[::-1]
    div_dic = dict(zip(range(p), [0] * p))
    tmp = 0

    for i in range(n):
        tmp += s[i] * pow(10, i, p)
        tmp %= p
        div_dic[tmp] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
    p_cnt += div_dic.get(0)

print(p_cnt)

Buggy python submission (verdict: Runtime Error):
from collections import Counter
n, p = map(int, input().split())
s = input()
s += '0'
p_cnt = 0
div_list = []

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
else:
    for i in range(n+1):
        num = int(s[i:])
        div_list.append(num % p)

    div_ol = Counter(div_list)
    for v in div_ol.values():
        p_cnt += v * (v - 1) / 2



print(int(p_cnt))

Fixed submission:
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
        
else:
    s = s[::-1]
    div_dic = dict(zip(range(p), [0] * p))
    tmp = 0

    for i in range(n):
        tmp += s[i] * pow(10, i, p)
        tmp %= p
        div_dic[tmp] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
    p_cnt += div_dic.get(0)

print(p_cnt)

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

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

print(p_cnt)

Fixed submission:
n, p = map(int, input().split())
s = [int(i) for i in input()]
p_cnt = 0

if p == 2 or p == 5:
    for i in range(n):
        if s[i] % p == 0:
            p_cnt += i+1
        
else:
    s = s[::-1]
    div_dic = dict(zip(range(p), [0] * p))
    tmp = 0

    for i in range(n):
        tmp += s[i] * pow(10, i, p)
        tmp %= p
        div_dic[tmp] += 1

    for v in div_dic.values():
        p_cnt += v * (v - 1)
    p_cnt //= 2
    p_cnt += div_dic.get(0)

print(p_cnt)