program divisible_substring
  implicit none
  integer :: n, p, i, j, d, k = 0, cnt(0:10000) = 0
  integer, dimension(0:200000) :: hash = 0, pow = 0
  character(200000) :: s
  integer(8) :: x = 0
  read(*,*) n, p
  read(*,*) s
  pow(0) = 1
  do i = 1, n
    d = ichar(s(i:i)) - 48
    hash(i) = mod(10 * hash(i - 1) + d, p)
    cnt(hash(i)) = cnt(hash(i)) + 1
    pow(i) = mod(10 * pow(i - 1), p)
  end do
  do i = 1, n
    d = ichar(s(i:i)) - 48
    j = mod(k + d, p)
    cnt(j) = cnt(j) - 1
    x = x + cnt(k)
    k = mod(k + d * pow(n - i), p)
  end do
  write(*,'(i0)') x
end program divisible_substring