p = int(input().split()[1])

a = list(map(int, eval(input())))

if 10 % p == 0:

  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)

else:

  d = [1] + [0] * (p - 1)

  t, y = 0, 1

  for i, x in enumerate(reversed(a), 1):

    t = (t + x * y) % p

    d[t] += 1

    y = y * 10 % p;

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

print(r)

p = int(input().split()[1])

a = list(map(int, eval(input())))

if 10 % p == 0:

  r = sum(i for i, x in enumerate(a, 1) if x % p == 0)

else:

  d = [1] + [0] * (p - 1)

  s, y = 0, 1

  for x in reversed(a):

    s = (s + x * y) % p

    d[s] += 1

    y = y * 10 % p;

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

print(r)
p = int(input().split()[1])
a = list(map(int, eval(input())))
if 10 % p == 0:
    r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
    d = [1] + [0] * (p - 1)
    t, y = 0, 1
    for i, x in enumerate(reversed(a), 1):
        t = (t + x * y) % p
        d[t] += 1
        y = y * 10 % p
    r = sum(i * (i - 1) // 2 for i in d)
print(r)

p = int(input().split()[1])
a = list(map(int, eval(input())))
if 10 % p == 0:
    r = sum(i for i, x in enumerate(a, 1) if x % p == 0)
else:
    d = [1] + [0] * (p - 1)
    s, y = 0, 1
    for x in reversed(a):
        s = (s + x * y) % p
        d[s] += 1
        y = y * 10 % p
    r = sum(i * (i - 1) // 2 for i in d)
print(r)

-    t, y = 0, 1 -    for i, x in enumerate(reversed(a), 1): -        t = (t + x * y) % p -        d[t] += 1 +    s, y = 0, 1 +    for x in reversed(a): +        s = (s + x * y) % p +        d[s] += 1