module IntMap = Map.Make (struct
  type t = int
  let compare = compare
end)

let rec power ( * ) e m n =
  if n <= 0 then e
  else power ( * ) (if n land 1 = 0 then e else m * e) (m * m) (n lsr 1)

let () = Scanf.scanf "%d %d\n%s" @@ fun n p s ->
  let ( +^ ) x y = (x + y) mod p in
  let neg x = (p - x) mod p in
  let ( *^ ) x y = (x * y) mod p in
  let inv x = power ( *^ ) 1 x (p - 2) in
  let acc = Array.make (n + 1) 0 in
  let radix = Array.make (n + 1) 1 in
  for i = 0 to n - 1 do
    radix.(i + 1) <- 10 *^ radix.(i);
    acc.(i + 1) <- 10 *^ acc.(i) +^ (Char.code s.[n - i - 1] - Char.code '0');
  done;
  let ms = Array.make (n + 2) IntMap.empty in
  for i = n downto 0 do
    let x = neg (acc.(i) *^ inv radix.(i)) in
    ms.(i) <- IntMap.add x (1 + try IntMap.find x ms.(i + 1) with Not_found -> 0) ms.(i + 1)
  done;
  Printf.printf "%d\n" @@
  Array.fold_left ( + ) 0 @@
  Array.init n @@ fun i ->
    try IntMap.find acc.(i) ms.(i + 1) with Not_found -> 0