Problem: Given is a string S consisting of digits from `1` through `9`.  Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:  Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

Submission attempts (in order):
from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    S = input()
    cnt = 0
    start = 0
    LIST = []
    while start+4 < len(S):
        for i in range(start+4,len(S)+1):
            if S[i-1] == 0:
                start += 4
                continue
            if int(S[start:i]) % 2019 == 0:
                cnt += 1
                start += 4
        start += 1
    print(cnt)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    S = input()
    cnt = 0
    start = 0
    while start+4 < len(S):
        for i in range(start+4,len(S)+1):
            if S[i-1] == 0:
                continue
            if int(S[start:i]) % 2019 == 0:
                cnt += 1
                start += 3
        start += 1
    print(cnt)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    S = input()
    cnt = 0
    start = 0
    while start+4 < len(S):
        for i in range(start+4,len(S)+1):
            if S[i-1] == 0:
                break
            if int(S[start:i]) % 2019 == 0:
                cnt += 1
                break
        start += 2
    print(cnt)
if __name__ == '__main__':
  main()


from statistics import median
#import collections

from fractions import gcd
from itertools import combinations 
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]

#
#
#

#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    S = input()
    LIST = []
    for i in range(2000):
        LIST.append(2019*i)
    start = 0
    cnt = 0
    while start+4 < len(S):
        for i in range(start+4,len(S)+1):
            #print(S[start:i])
            if int(S[start:i]) in LIST:
                cnt += 1
        start += 1
    print(cnt)
if __name__ == '__main__':
  main()


S = input()
from collections import defaultdict
dic = defaultdict(int)
dic[0] += 1

S = S[::-1]
d = 1
now = 0
for i in range(len(S)):
    
    now += d*int(S[i]) % 2019
    dic[now%2019] += 1
    d *= 10
    d %= 2019
ans = 0
for k,v in dic.items():
    ans += v*(v-1)//2
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Time Limit Exceeded

Time Limit Exceeded

Accepted