Buggy python submission (verdict: Runtime Error):
def main():
    n,p=map(int,input().split())
    s=input()
    cnt,ans=0,0
    m=[0]*p

    if p==2 or p==5:
        for i in s:
            if i%p==0:
                cnt+=1
        print(cnt*(cnt+1)//2)
        exit()

    for i in range(n):
        m[int(s[i:])%p]+=1
    
    for i in m:
        ans+=i*(i-1)//2
    ans+=m[0]

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,p=map(int,input().split())
    s=list(map(int,list(input())))
    cnt,ans=0,0
    m=[0]*p
    m[0]=1

    if p in [2, 5]:
        for i, x in enumerate(s):
            if x % p == 0:
                ans += i + 1
        print(ans)
        exit()

    sm=0
    d=1
    for i in range(n):
        sm=(sm+s[n-1-i]*d)%p
        m[sm]+=1
        d=(d*10)%p
    
    for i in m:
        ans+=i*(i-1)//2

    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    n,p=map(int,input().split())
    s=input()
    cnt,ans=0,0
    m=[0]*p

    if p==2 or p==5:
        for i in s:
            if i%p==0:
                cnt+=1
        print(cnt*(cnt+1)//2)
        exit()

    for i in range(n):
        m[int(s[i:])%p]+=1
    
    for i in m:
        ans+=i*(i-1)//2
    ans+=m[0]

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,p=map(int,input().split())
    s=list(map(int,list(input())))
    cnt,ans=0,0
    m=[0]*p
    m[0]=1

    if p in [2, 5]:
        for i, x in enumerate(s):
            if x % p == 0:
                ans += i + 1
        print(ans)
        exit()

    sm=0
    d=1
    for i in range(n):
        sm=(sm+s[n-1-i]*d)%p
        m[sm]+=1
        d=(d*10)%p
    
    for i in m:
        ans+=i*(i-1)//2

    print(ans)

if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,p=map(int,input().split())
    s=list(map(int,list(input())))
    cnt,ans=0,0
    m=[0]*p
    m[0]=1

    if p in [2, 5]:
        for i, x in enumerate(s):
            if x % p == 0:
                ans += i + 1
        print(ans)
        exit()

    sm=0
    d=1
    for i in range(n):
        sm=(sm+s[n-1-i]*d)%p
        m[sm]+=1
        d=(d*10)%p
    
    for i in m:
        ans+=i*(i-1)//2

    print(ans)

if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    n,p=map(int,input().split())
    s=list(map(int,list(input())))
    cnt,ans=0,0
    m=[0]*p
    m[0]=1

    if p in [2, 5]:
        for i, x in enumerate(s):
            if x % p == 0:
                ans += i + 1
        print(ans)
        exit()

    sm=0
    d=1
    for i in range(n):
        sm=(sm+s[n-1-i]*d)%p
        m[sm]+=1
        d=(d*10)%p
    
    for i in m:
        ans+=i*(i-1)//2

    print(ans)

if __name__ == '__main__':
    main()