Buggy python submission (verdict: Runtime Error):
n, p = map(int, input().split())

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    li = [0] * (n + 1)
    counter = {}
    for i, x in enumerate(s[::-1]):
        li[i+1] = (li[i] + (int(x) * (10**(i+1)) % p)) % p
        counter[li[i+1]] = counter.get(li[i+1], 0) + 1
    ans = 0
    ans = sum(v * (v - 1) for v in counter.values()) // 2
    ans += counter[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    li = [0] * (n + 1)
    for i, x in enumerate(s[::-1]):
        li[i+1] = (li[i] + (int(x) * (10**(i+1)) % p)) % p

    ans = 0
    c = Counter(li[1:])
    ans = sum(v * (v - 1) for v in c.values()) // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if x % p == 0)
    print(ans)
else:
    li = [0] * (n + 1)
    for i, x in enumerate(s[::-1]):
        li[i+1] = (li[i] + (int(x) * (10**(i+1)) % p)) % p

    ans = 0
    c = Counter(li[1:])
    ans = sum(v * (v - 1) for v in c.values()) // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0] * (n + 1)
    for i, x in enumerate(s[::-1]):
        li[i+1] = (li[i] + (int(x) * (10**(i+1)) % p)) % p

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += v * (v-1)
    ans = ans // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(s[::-1]):
        li.append((li[-1] + int(x) * (10**i)) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += v * (v-1)
    ans = ans // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(s[::-1]):
        li.append((li[-1] + int(x) * (10**i) % p) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += v * (v-1)
    ans = ans // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(s[::-1]):
        li.append((li[-1] + int(x) * (10**i) % p) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += v * (v-1)
    ans = ans // 2
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(s[::-1]):
        li.append((li[-1] + int(x) * (10**i) % p) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += (v * (v-1) // 2)
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

s = input()

if 10 % p == 0:
    ans = 0
    for i, x in enumerate(s):
        if int(x) % p == 0:
            ans += i + 1
    print(ans)
else:
    li = [0]
    for i, x in enumerate(reversed(s)):
        li.append((li[-1] + int(x) * (10**i) % p) % p)

    ans = 0
    c = Counter(li[1:])
    for v in c.values():
        ans += (v * (v-1) // 2)
    ans += c[0]
    print(ans)


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

s = input()

if 10 % p == 0:
    ans = sum(i + 1 for i, x in enumerate(s) if int(x) % p == 0)
    print(ans)
else:
    counter = [0] * p
    counter[0] = 1
    cur = 0
    p10 = 1
    ans = 0
    for x in s[::-1]:
        cur = (cur + int(x) * p10) % p
        ans += counter[cur]
        counter[cur] += 1
        p10 *= 10
        p10 %= p
    print(ans)