module app;

import core.bitop;

import std.algorithm;
import std.array;
import std.bigint;
import std.container.rbtree;
import std.conv;
import std.stdio;
import std.string;
import std.traits;

struct Input
{
	int n, p;
	string s;
}

void parseInput(T)(out Input input, T file)
{
	with (file) with (input)
	{
		auto ar = readln().strip().split().map!(to!int).array();
		n = ar[0];
		p = ar[1];
		s = readln().strip();
	}
}

auto main2(Input* input)
{
	with (input)
	{
        ulong result = 0;
        if (p == 2 || p == 5)
        {
            foreach (i, c; s)
                if ((c - '0' - p) % p == 0)
                    result += i + 1;
        }
        else
        {
            int[] subMods;
            subMods.length = n;
            int[] rem2count;
            rem2count.length = p;

            for (int i = n - 1; i >= 0; i--)
            {
                Mod!int subNum = Mod!(int)(p, 0);
                subNum += 10;
                subNum ^^= n - 1 - i;
                subNum *= s[i] - '0';

                if (i < n - 1)
                    subNum += subMods[i + 1];

                if (subNum.value == 0)
                    result += 1;

                result += rem2count[subNum.value];

                subMods[i] = subNum.value;
                rem2count[subNum.value]++;
            }
        }
        return result;
	}
}

alias retType = ReturnType!main2;
static if (!is(retType == void))
{
	unittest { writeln("begin unittest"); }
	auto _placeholder_ = ReturnType!main2.init;

	unittest // example1
	{
		string example =
`4 3
3543`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 6);
	}

	unittest // example2
	{
		string example =
`4 2
2020`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 10);
	}

	unittest // example3
	{
		string example =
`20 11
33883322005544116655`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 68);
	}

	unittest { writeln("end unittest"); }

	void parseExample(out Input input, string example)
	{
		struct Adapter
		{
			string[] _lines;
			this(string input) { _lines = input.splitLines(); }
			string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
		}
		parseInput(input, Adapter(example));
	}
}

void printResult(T)(T result)
{
	static if (isFloatingPoint!T) writefln("%f", result);
	else writeln(result);
}

void main()
{
	Input input = void;
	parseInput(input, stdin);
	static if (is(retType == void))
		main2(&input);
	else
	{
		auto result = main2(&input);
		printResult(result);
	}
}

struct Mod(T)
if ((is(T : int) || is(T : long)))
{
    private
    {
        T _m;
        T _value;
    }
    invariant
    {
        assert(_m > 0);
        assert(_value >= 0);
        assert(_value < _m);
    }
    this() @disable;
    this(T m, T t)
    {
        _m = m;
        _value = t % m;
    }
    void value(T t) { _value = t % _m; }
    T value() const { return _value; }
    void opAssign(typeof(this) mod) { value = mod._value; }
    typeof(this) opBinary(string op)(T t)
    {
        static if (op == "+") { return Mod(_m, _value + t); }
        else static if (op == "-")
        {
            auto temp = _value - t % _m;
            if (temp < 0)
                temp += _m;
            return Mod(_m, temp);
        }
        else static if (op == "*") { return Mod(_m, _value * t); }
        else static if (op == "^^")
        {
            if (t < 0) assert(0);
            else if (t == 0) return Mod(_m, 1);
            else if (t == 1) return this;
            else if (t % 2 == 1) return this * (this ^^ (t - 1));
            else
            {
                auto temp = this ^^ (t / 2);
                return temp * temp;
            }
        }
        else static assert(0);
    }
    typeof(this) opBinary(string op)(typeof(this) mod) { return opBinary!op(mod._value); }
    void opOpAssign(string op)(T t) { this = this.opBinary!op(t); }
    void opOpAssign(string op)(typeof(this) mod) { opOpAssign!op(mod._value); }
    bool opEquals()(auto ref const T t) const { return _value == t; }
    bool opEquals()(auto ref const typeof(this) mod) const { return opEquals(mod._value); }
}