Buggy python submission (verdict: Time Limit Exceeded):
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s*=10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n,p = map(int,input().split())
	s = [int(c) for c in list(input().strip())][::-1]
	num = 0
	ans = 0
	if p == 2 or p == 5:
		for i in range(n):
			if s[i]%p == 0:
				ans += n-i
		print(ans)
		return
	arr = [0]*p
	arr[0]=1
	z = 1
	t = 0
	for ss in s:
		t = (t + z*ss)%p	
		z *=10 %p
		arr[t] += 1
	for l in arr:
		ans += l * (l - 1) // 2
	print(ans)
if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n,p = map(int,input().split())
	s = [int(c) for c in list(input().strip())]
	s = s[::-1]
	num = 0
	ans = 0
	if p == 2 or p == 5:
		for i in range(n):
			if s[i]%p == 0:
				ans += n-i
		print(ans)
		return
	arr = [0]*p
	arr[0]=1
	z = 1
	t = 0
	for ss in s:
		t = (t + z*ss)%p	
		z *=10 %p
		arr[t] += 1
	for l in arr:
		ans += l * (l - 1) // 2
	print(ans)
if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n,p = map(int,input().split())
	s = [int(c) for c in input()][::-1]
	num = 0
	ans = 0
	if p == 2 or p == 5:
		for i in range(n):
			if s[i]%p == 0:
				ans += n-i
		print(ans)
		return
	arr = [0]*p
	arr[0]=1
	z = 1
	t = 0
	for ss in s:
		t = (t + z*ss)%p	
		z *=10 %p
		arr[t] += 1
	for l in arr:
		ans += l * (l - 1) // 2
	print(ans)
if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n,p = map(int,input().split())
	s = [int(c) for c in input()] 
	num = 0
	ans = 0
	if p == 2 or p == 5:
		for i in range(n):
			if s[i]%p == 0:
				ans += i+1	
		print(ans)
		exit()
	arr = [0]*p
	s = s[::-1]
	arr[0]=1
	z = 1
	t = 0
	for ss in s:
		t = (t + z*ss)%p	
		z *=10 %p
		arr[t] += 1
	for l in arr:
		ans += l * (l - 1) // 2
	print(ans)
if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = [int(c) for c in input()] 
num = 0
ans = 0
if p == 2 or p == 5:
	for i in range(n):
		if s[i]%p == 0:
			ans += i+1	
	print(ans)
	exit()
arr = [0]*p
s = s[::-1]
z = 1
t = 0
for ss in s:
	t = (t + z*ss)%p	
	z *=10 %p
	#print(t)
	arr[t] += 1
arr[0]+=1
for l in arr:
	ans += l * (l - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = [int(c) for c in input()] 
num = 0
ans = 0
if p == 2 or p == 5:
	for i in range(n):
		if s[i]%p == 0:
			ans += i+1	
	print(ans)
	exit()
arr = [0]*p
s = s[::-1]
z = 1
t = 0
for ss in s:
	t = (t + z*ss)%p	
	z *=10 %p
	#print(t)
	arr[t] += 1
arr[0]+=1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
arr = [0]*p
s = s[::-1]
z = 1
t = 0
for ss in s:
	t = (t + z*int(ss))%p	
	z *=10 %p
	#print(t)
	arr[t] += 1
arr[0]+=1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	n,p = map(int,input().split())
	s = input()
	num = 0
	s1 = [0]*(n+1)
	ans = 0
	if p == 2 or p == 5:
		for i in range(len(s)):
			if int(s[i])%p == 0:
				ans += i+1	
		print(ans)
		return
	ans = 0
	arr = [0]*p
	s = s[::-1]
	z = 1
	for i in range(n):
		s1[i+1] = s1[i] + int(s[i])*z
		z *=10 %p
		s1[i+1] %=p
		arr[s1[i]] += 1
	arr[s1[n]]+=1
	#print(s1)
	for i in range(p):
		ans += arr[i] * (arr[i] - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
s1 = [0]*(n+1)
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
ans = 0
arr = [0]*p
s = s[::-1]
z = 1
for i in range(n):
	s1[i+1] = s1[i] + int(s[i])*z
	z *=10 %p
	s1[i+1] %=p
	arr[s1[i]] += 1
arr[s1[n]]+=1
#print(s1)
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
s1 = [0]*(n+1)
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
ans = 0
arr = [0]*p
s = s[::-1]
for i in range(n):
	s1[i+1] = s1[i] + int(s[i])*10**i
	s1[i+1] %=p
	arr[s1[i]] += 1
arr[s1[n]]+=1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
s1 = [0]*(n+1)
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
ans = 0
arr = [0]*p
for i in range(n+1):
	s1[i] = int(s)%(10**i)%p
	arr[s1[i]] += 1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
s1 = [0]*(n+1)
ans = 0
if p == 2 or p == 5:
	for i in range(len(s)):
		if int(s[i])%p == 0:
			ans += i+1	
	print(ans)
	exit()
ans = 0
for i in range(n+1):
	s1[i] = int(s)%(10**i)%p
arr = [0]*p
for i in range(n+1):
	arr[s1[i]] += 1
for i in range(p):
	ans += arr[i] * (arr[i] - 1) // 2
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
n,p = map(int,input().split())
s = input()
num = 0
ans = 0
for i in range(n):
	num = int(s[i])
	if num % p == 0:ans+=1
	for j in range(i+1,n):
		num = 10 * num + int(s[j])
		if num % p == 0:
			ans += 1
print(ans)


Fixed submission:
def main():
	N,P = map(int,input().split())
	S = [int(s) for s in list(input().strip())][::-1]
	num = 0
	ans = 0
	if P == 2 or P == 5:
		for i in range(N):
			if S[i]%P == 0:
				ans += N-i
		print(ans)
		return
	L = [0]*P
	L[0]=1
	t = 0
	s = 1
	for z in S:
		t = (z*s+t)%P	
		L[t]+=1
		s=s*10%P
	for l in L:
		ans += l * (l - 1) // 2
	print(ans)

if __name__ == '__main__':
	main()