Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin 
from collections import defaultdict, Counter


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()

ans = 0 


# 2 cases
if P == 2 or P == 5:
	digitdiv = []
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i + 1

else:
	#
	count = Counter()
	prefix = []
	ten = 1
	mod = 0
	for i in range(N):
		x = (int(S[N - i - 1])*ten + mod) % P 
		prefix.append(x)
		count[x] += 1
		ten *= 10
		mod = x
	prefix.append(0)
	count[0] += 1
	for val in count.values():
		ans += val*(val - 1)//2


print (ans)





Fixed submission:
from sys import stdin 
from collections import defaultdict, Counter


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()

ans = 0 


# 2 cases
if P == 2 or P == 5:
	digitdiv = []
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i + 1

else:
	#
	count = Counter()
	prefix = []
	ten = 1
	mod = 0
	for i in range(N):
		x = (int(S[N - i - 1])*ten + mod) % P 
		prefix.append(x)
		count[x] += 1
		ten = (ten * 10) % P 
		mod = x
	prefix.append(0)
	count[0] += 1
	for val in count.values():
		ans += val*(val - 1)//2


print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin 
from collections import defaultdict, Counter


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()

ans = 0 


# 2 cases
if P == 2 or P == 5:
	digitdiv = []
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i + 1

else:
	#
	prefix = []
	for i in range(N):
		x = int(S[i:]) % P 
		prefix.append(x)
	prefix.append(0)
	count = Counter()
	for i in range(N + 1):
		count[int(prefix[i])] += 1

	for val in count.values():
		ans += val*(val - 1)//2


print (ans)





Fixed submission:
from sys import stdin 
from collections import defaultdict, Counter


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()

ans = 0 


# 2 cases
if P == 2 or P == 5:
	digitdiv = []
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i + 1

else:
	#
	count = Counter()
	prefix = []
	ten = 1
	mod = 0
	for i in range(N):
		x = (int(S[N - i - 1])*ten + mod) % P 
		prefix.append(x)
		count[x] += 1
		ten = (ten * 10) % P 
		mod = x
	prefix.append(0)
	count[0] += 1
	for val in count.values():
		ans += val*(val - 1)//2


print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin 
from collections import defaultdict 


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()


tens_mod = []
count = 1
for i in range(N - 1):
	count *= 10 % P 
	tens_mod.append(count)

ans = 0
# dp: length/right pos index
dp = defaultdict(lambda:[0]*N)
for i in range(N):
	x = int(S[i])
	dp[1][i] = x % P
	if x % P == 0:
		ans += 1

# length of segment
for i in range(2, N + 1):
	# right pos index 
	for j in range(i - 1, N):
		x = (dp[i - 1][j - 1]*10 + dp[1][j]) % P
		if x == 0:
			ans += 1
		dp[i][j] = x

print (ans)





Fixed submission:
from sys import stdin 
from collections import defaultdict, Counter


N, P = map(int, stdin.readline().split())
S, = stdin.readline().split()

ans = 0 


# 2 cases
if P == 2 or P == 5:
	digitdiv = []
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i + 1

else:
	#
	count = Counter()
	prefix = []
	ten = 1
	mod = 0
	for i in range(N):
		x = (int(S[N - i - 1])*ten + mod) % P 
		prefix.append(x)
		count[x] += 1
		ten = (ten * 10) % P 
		mod = x
	prefix.append(0)
	count[0] += 1
	for val in count.values():
		ans += val*(val - 1)//2


print (ans)