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

s = eval(input())

num = 0

s1 = [0]*(n+1)

ans = 0

if p == 2 or p == 5:

	for i in range(len(s)):

		if int(s[i])%p == 0:

			ans += i+1	

	print(ans)

	exit()

ans = 0

arr = [0]*p

s = s[::-1]

z = 1

for i in range(n):

	s1[i+1] = s1[i] + int(s[i])*z

	z = z * 10 %p

	s1[i+1] %=p

	arr[s1[i]] += 1

arr[s1[n]]+=1

#print(s1)

for i in range(p):

	ans += arr[i] * (arr[i] - 1) // 2

print(ans)

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



ans = 0

s = eval(input())

if p == 2 or p == 5:

    for i in range(len(s)):

        n = int(s[i])

        if n%p == 0:

            ans += i+1

    print(ans)

    exit()



r = [0]*p

r[0] = 1

t = 1

z = 0

for i in reversed(s):

    z = int(i) * t + z

    t *= 10

    t %= p

    z %= p

    r[z] += 1



ans = sum(i*(i-1)//2 for i in r)

print(ans)

n, p = list(map(int, input().split()))
s = eval(input())
num = 0
s1 = [0] * (n + 1)
ans = 0
if p == 2 or p == 5:
    for i in range(len(s)):
        if int(s[i]) % p == 0:
            ans += i + 1
    print(ans)
    exit()
ans = 0
arr = [0] * p
s = s[::-1]
z = 1
for i in range(n):
    s1[i + 1] = s1[i] + int(s[i]) * z
    z = z * 10 % p
    s1[i + 1] %= p
    arr[s1[i]] += 1
arr[s1[n]] += 1
# print(s1)
for i in range(p):
    ans += arr[i] * (arr[i] - 1) // 2
print(ans)

n, p = list(map(int, input().split()))
ans = 0
s = eval(input())
if p == 2 or p == 5:
    for i in range(len(s)):
        n = int(s[i])
        if n % p == 0:
            ans += i + 1
    print(ans)
    exit()
r = [0] * p
r[0] = 1
t = 1
z = 0
for i in reversed(s):
    z = int(i) * t + z
    t *= 10
    t %= p
    z %= p
    r[z] += 1
ans = sum(i * (i - 1) // 2 for i in r)
print(ans)

+ans = 0 -num = 0 -s1 = [0] * (n + 1) -ans = 0 -        if int(s[i]) % p == 0: +        n = int(s[i]) +        if n % p == 0: -ans = 0 -arr = [0] * p -s = s[::-1] -z = 1 -for i in range(n): -    s1[i + 1] = s1[i] + int(s[i]) * z -    z = z * 10 % p -    s1[i + 1] %= p -    arr[s1[i]] += 1 -arr[s1[n]] += 1 -# print(s1) -for i in range(p): -    ans += arr[i] * (arr[i] - 1) // 2 +r = [0] * p +r[0] = 1 +t = 1 +z = 0 +for i in reversed(s): +    z = int(i) * t + z +    t *= 10 +    t %= p +    z %= p +    r[z] += 1 +ans = sum(i * (i - 1) // 2 for i in r)