Buggy python submission (verdict: Time Limit Exceeded):
N,P=map(int,input().split())
S=input()

a=[0]*(N+1)

if P!=2 and P!=5:
    for i in range(N):
        a[i] = int(S[i:])%P
    ans=0
    from collections import Counter
    C=Counter(a)
    for k,v in C.items():
        ans+=v*(v-1)//2
elif P==2:
    ans=0
    for i in range(N):
        if int(S[i])%2==0:
            ans+=i+1
elif P==5:
    ans=0
    for i in range(N):
        if int(S[i])==0 or int(S[i])==5:
            ans+=i+1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)
r = [0] * (N+1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = (int(S[N-i])*tenfactor+a[i-1])
        tenfactor*=10
        r[i]=a[i]%P

    ans = 0
    from collections import Counter
    C=Counter(r)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)
r = [0] * P

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = (int(S[N-i])*tenfactor+a[i-1])
        tenfactor*=10
        r[a[i] % P] += 1
    r[0] += 1
    ans = 0
    from collections import Counter
    C=Counter(r)
    for v,i in C.items():
        ans += (v * (v - 1) // 2)*i
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = list(input())

a = [0] * (N + 1)
r = [0] * P

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = (int(S[N-i])*tenfactor+a[i-1])
        tenfactor*=10
        r[a[i] % P] += 1
    r[0] += 1
    ans = 0
    for i in range(P):
        ans += r[i] * (r[i] - 1) // 2
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)
r = [0] * P

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = (int(S[N-i])*tenfactor+a[i-1])
        tenfactor*=10
        r[a[i] % P] += 1
    r[0] += 1
    ans = 0
    for i in range(P):
        ans += r[i] * (r[i] - 1) // 2
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=map(int,input().split())
S=input()

a=[0]*(N+1)
r=[0]*P

if P!=2 and P!=5:
    for i in range(N):
        a[i] = int(S[i:])
        temp=a[i]%P
        r[temp]+=1
    r[0]+=1
    ans=0
    for i in range(P):
        ans+=r[i]*(r[i]-1)//2
elif P==2:
    ans=0
    for i in range(N):
        if int(S[i])%2==0:
            ans+=i+1
elif P==5:
    ans=0
    for i in range(N):
        if int(S[i])==0 or int(S[i])==5:
            ans+=i+1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=map(int,input().split())
S=input()

a=[0]*(N+1)
r=[0]*P

if P!=2 and P!=5:
    for i in range(N):
        a[i] = int(S[i:])
    for i in range(N+1):
        temp=a[i]%P
        r[temp]+=1
    ans=0
    for i in range(P):
        ans+=r[i]*(r[i]-1)//2
elif P==2:
    ans=0
    for i in range(N):
        if int(S[i])%2==0:
            ans+=i+1
elif P==5:
    ans=0
    for i in range(N):
        if int(S[i])==0 or int(S[i])==5:
            ans+=i+1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,P=map(int,input().split())
S=input()

a=[0]*(N+1)
r=[0]*P

if P!=2 and P!=5:
    for i in range(N):
        a[i] = int(S[i:])
    for i in range(N+1):
        temp=a[i]%P
        r[temp]+=1
    ans=0
    for i in range(P):
        ans+=r[i]*(r[i]-1)//2
elif P==2:
    ans=0
    for i in range(N):
        if int(S[i])%2==0:
            ans+=i+1
elif P==5:
    ans=0
    for i in range(N):
        if int(S[i])==0 or int(S[i])==5:
            ans+=i+1

print(ans)



Fixed submission:
N, P = map(int, input().split())
S = input()

a = [0] * (N + 1)

if P != 2 and P != 5:
    tenfactor=1
    for i in range(1,N+1):
        a[i] = ((int(S[N-i])*tenfactor+a[i-1])%P)
        tenfactor*=10
        tenfactor%=P

    ans = 0
    from collections import Counter
    C=Counter(a)
    for v,i in C.items():
        ans += (i * (i - 1) // 2)
elif P == 2:
    ans = 0
    for i in range(N):
        if int(S[i]) % 2 == 0:
            ans += i + 1
elif P == 5:
    ans = 0
    for i in range(N):
        if int(S[i]) == 0 or int(S[i]) == 5:
            ans += i + 1

print(ans)