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

ans = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
  	mod = [-1 for i in range(p)]
	mod[0] = 0
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1
        m10 = (10 * m10) % p

    for m in mod:
        ans += m*(m+1)//2

print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)

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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    m = 0
    for i in range(n):
        m += int(s[-1-i])*(10**(i))
        mp = m % p
        mod[mp] += 1
    for m in mod:
        ans += m*(m+1)//2

print(ans)

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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)

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

ans = 0
mod = {0: 0}

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    s = int(s)
    for i in range(1, n+1):
        m = s % (10**i) % p
        mod[m] = mod.get(m, -1)+1
    for m in mod.values():
        ans += m*(m+1)//2

print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)

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

ans = 0
mod = [0 for i in range(p)]
mod[0] = 1

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    s = int(s)
    for i in range(1, n+1):
        m = s % (10**i) % p
        ans += mod[m]
        mod[m] += 1

print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)

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

ans = 0
mod = [0 for i in range(p)]
mod[0] = 1

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    s = int(s)
    for i in range(1, n+1):
        m = s % (10**i) % p
        ans += mod[m]
        mod[m] += 1

print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+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(1, n+1):
    for j in range(n-i+1):
        if int(s[j:j+i]) % p == 0:
            ans += 1


print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+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(1, n+1):
    for j in range(n-i+1):
        if int(s[j:j+i]) % p == 0:
            ans += 1


print(ans)


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

ans = 0
mod = [-1 for i in range(p)]
mod[0] = 0

if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
else:
    mp = 0
    m10 = 1
    for i in range(n-1, -1, -1):
        m10 = (10 * m10) % p
        mp = (mp + int(s[i]) * m10) % p
        mod[mp] += 1

    for m in mod:
        ans += m*(m+1)//2

print(ans)