#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

template<typename Arithmetic, typename Integral>
std::enable_if_t< std::is_unsigned<Integral>::value, Arithmetic>
ipow(Arithmetic bace, Integral n)
{
	//繰り返し二条法
	auto res = (Arithmetic)(1);
	while (n > 0) {
		if (n & 1) res *= bace;
		bace *= bace;
		n >>= 1;
	}
	return res;
}
constexpr bool is_prime(uint32_t N)
{
	if (N <= 1) {
		return false;
	}
	for (size_t i = 2; i * i <= N; ++i)
	{
		if (N % i == 0) {
			return false;
		}
	}
	return true;
}
uint64_t MOD;
class mint_base;
//mint_base_base型用の累乗関数
mint_base m_pow(mint_base x, uint64_t n)noexcept;
//mod計算を自動で行う整数テンプレートクラス
class mint_base
{
public:
	//static_assert(!(MOD <= 2), "MOD cannot be below 2.");
	//static_assert(MOD <= (0xFFFFFFFFFFFFFFFF / 2), "MOD is too big");//加算してオーバーフローしない
	//static_assert(MOD <= 0xFFFFFFFF, "MOD is too big");//乗算してオーバーフローしない
	mint_base operator+(const mint_base& other)const noexcept
	{
		auto v = *this;
		return v += other;
	}
	mint_base operator-(const mint_base& other)const noexcept
	{
		auto v = *this;
		return v -= other;
	}
	mint_base operator*(const mint_base& other)const noexcept
	{
		auto v = *this;
		return v *= other;
	}
	auto operator/(const mint_base& other)const noexcept
	{
		auto v = *this;
		return v /= other;
	}
	mint_base& operator+=(const mint_base& other) noexcept
	{
		a += other.a;
		if (MOD <= a) { a -= MOD; };
		return *this;
	}
	mint_base& operator-=(const mint_base& other) noexcept
	{
		if (a >= other.a) {
			a -= other.a;
		}
		else {
			a = (a + MOD) - other.a;
		}
		return *this;
	}
	mint_base& operator*=(const mint_base& other) noexcept
	{
#if 1
		a *= other.a;
		a %= MOD;
#else
		//MOD <= (MAXUINT64 / 2)条件下
		uint64_t b = other.a, v = 0;
		while (b > 0) {
			if (b & 1) {
				v += a;
				if (v >= MOD)v -= MOD;
			}
			a += a;
			if (MOD <= a)a -= MOD;
			b >>= 1;
		}
		a = v;
#endif
		return *this;
	}
	mint_base& operator/=(const mint_base& other) noexcept
	{
		return *this *= ~other;
	}
	mint_base operator+()const noexcept { return *this; }
	mint_base operator-()const noexcept
	{
		return{ MOD - a, mod_value_tag{} };
	}
	mint_base& operator++() noexcept
	{
		if (MOD <= ++a) { a = 0; };
		return *this;
	}
	mint_base& operator--() noexcept
	{
		if (a <= 0) { a = MOD; };
		--a;
		return *this;
	}
	mint_base operator++(int) noexcept
	{
		auto tmp = *this;
		++* this;
		return tmp;
	}
	mint_base operator--(int) noexcept
	{
		auto tmp = *this;
		--* this;
		return tmp;
	}
	mint_base operator~()const noexcept
	{
		return ipow(*this, e_phi - 1);
	}
	mint_base& operator=(const mint_base& other) noexcept
	{
		a = other.a;
		return *this;
	}
	explicit operator uint64_t()const noexcept
	{
		return a;
	}
	explicit operator unsigned()const noexcept
	{
		return (unsigned)a;
	}
	static  uint64_t getmod() noexcept
	{
		return MOD;
	}
	mint_base(uint64_t a_) noexcept :a(a_% MOD) {}
	mint_base()noexcept : a(0) {}
	struct mod_value_tag {};
	mint_base(uint64_t a_, mod_value_tag) :a(a_) {}

	static void set_e_phi() {
		e_phi = get_e_phi();
	}
private:
	static uint64_t get_e_phi()noexcept {
		//オイラー値の導出
		uint64_t temp = MOD;
		uint64_t m_ = MOD;
		for (uint64_t i = 2; i * i <= m_; ++i)
		{
			if (m_ % i == 0)
			{
				temp = temp / i * (i - 1);
				for (; m_ % i == 0; m_ /= i);
			}
		}
		if (m_ != 1)temp = temp / m_ * (m_ - 1);
		return temp;
	}
	static uint64_t e_phi;//オイラー値
	uint64_t a;
};
uint64_t mint_base::e_phi = 0;
//mint_base型用の累乗関数
mint_base m_pow(mint_base x, uint64_t n)noexcept
{
	mint_base res = 1;
	while (n > 0)
	{
		if (n & 1)res *= x;
		x *= x;
		n >>= 1;
	}
	return res;
}
//mint_baseの階乗計算
//O(x)時間が必要のため、fact_set関数を推奨する。
mint_base fact(const mint_base& x)noexcept
{
	mint_base res(1);
	for (uint64_t i = 1; i <= (uint64_t)x; ++i)
	{
		res *= i;
	}
	return res;
}
//mint_baseの階乗計算
//0からxまでの階乗を返す
//O(x)時間が必要
std::vector<mint_base> fact_set(mint_base x = mint_base(-1))
{
	mint_base res(1);
	std::vector<mint_base> set((uint64_t)(x)+1);
	set[0] = 1;
	for (uint64_t i = 1; i <= (uint64_t)x; ++i)
	{
		res *= i;
		set[i] = res;
	}
	return set;
}
//mint_base型のstreamへの出力
std::ostream& operator<<(std::ostream& os, mint_base i)
{
	os << (uint64_t)i;
	return os;
}
//mint_base型のstreamからの入力
std::istream& operator >> (std::istream& is, mint_base& i)
{
	uint64_t tmp;
	is >> tmp;
	i = tmp;
	return is;
}
typedef mint_base mint;
namespace mint_literal {
	mint operator""_mi(unsigned long long x)noexcept {
		return mint(x);
	}
}
using namespace mint_literal;
//mint_baseの階乗計算
//0からxまでの階乗を返す
//O(N)
template<int32_t X>
/*constexpr*/ std::array<mint_base, X + 1> fact_set_c()
{
	mint_base res(1);
	std::array<mint_base, X + 1> set;
	set[0] = 1;
	for (int32_t i = 1; i <= X; ++i)
	{
		res *= i;
		set[i] = res;
	}
	return set;
}

mint pow10_[300000];
mint inv10[300000];
mint sumsum[300000];
std::string S;
int32_t N, P;

int count[20000];

int main()
{
    using std::endl;
    in.sync_with_stdio(false);
    out.sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);

    in >> N >> P >> S;
	MOD = P;
	mint::set_e_phi();
	pow10_[0] = 1;
	for (size_t i = 1; i < 300000; i++)
	{
		pow10_[i] = pow10_[i - 1] * 10;
	}
	if (MOD == 2 || MOD == 5) {
		int64_t res = 0;
		for (int i = N-1; i >= 0; i--)
		{
			if ((S[i] - '0') % MOD == 0)
			{
				res += (i+1);
			}
		}
		out << res << endl;
		return 0;
	}
	//for (size_t i = 0; i < 300000; i++)
	//{
	//	inv10[i] = ~pow10_[i];
	//	assert((uint64_t)(inv10[i]) != 0);
	//}

	sumsum[0] = 0;
	for (size_t i = 0; i < S.size(); i++)
	{
		sumsum[i+1] = sumsum[i] + mint(S[i] - '0') * pow10_[(S.size()-i)-1];
		//sumsum[i] = sumsum[i - 1] + mint(S[i - 1] - '0') * pow10_[S.size() - (i)];
	}

	int64_t res = 0;
	for (int b = N; b >= 0; b--)
	{
		res += count[(uint64_t)sumsum[b]];
		++count[(uint64_t)sumsum[b]];
	}
	out << res << endl;


    return 0;
}
#endif


Measured memory_bytes: 7744