Problem:


Score : 400 points

Problem Statement

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.

Constraints

1 ≤ |S| ≤ 200000

S is a string consisting of digits from 1 through 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

Sample Input 1

1817181712114

Sample Output 1

3

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.



Sample input:
1817181712114


Sample output:
3

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import Counter
import itertools
#from functools import reduce

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")

# ###########################################

S=SR()

cnt=0
for i in range(1, len(S)-3):
    for j in range(i+4, len(S)+1):
        if int(S[i-1:j])%2019==0:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

S = input()

Len = len(S)
N = 0
for Num1 in range(0, Len-2):
    #0-> 3
    #Len-4 -> Len-1
    Sum = int(S[Num1]) + int(S[Num1+1]) + int(S[Num1+2])
    for Num2 in range(Num1+4, Len+1):
        Sum += int(S[Num2-1])
        if(Sum%3 ==0):
            Num = int(S[Num1:Num2])
            if(Num%673 ==0):
                N+= 1
print(N)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

S = input()
n = len(S)
A = np.zeros([n,2019]) 


A[0][int(S[0])] = 1
for i in range(0,n-1):
    s = int(S[i+1])
    for j in range(2019):
        #pre = (j+2019-10-s)%2019
        fol = (10*j+s)%2019
        A[i+1][fol] += A[i][j]
    A[i+1][s]+=1


print(int(np.sum(A[:,0])))

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    S = input()
    slen = len(S)
    ans = 0
    for i in range(0, slen):
        start = i
        end = i + 4
        while end <= slen:
            if (int(S[start:end])) % 2019 == 0:
                start = end + 1
                end = start + 4
                ans += 1
            else:
                end += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
s_len=len(s)+1

l=list()
ans = 0
for i in range(0,s_len):
    size = s_len
    if len(str(s_len)) >= 19:
        size = 19
    for j in range(i+1,size):
        if int(s[i:j]) % 2019 == 0:
            ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

n = int(s)
j = len(s)
del s
ln = [n % 10**i for i in range(1, j+1)]
s_mod = set()
count = 0
for i in ln:
  amari = i % 2019
  if amari == 0:
    count += 1
  else:
    count += (amari in s_mod)
    s_mod.add(amari)
  
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input() #sys.stdin.readlineは最後が改行
n =len(s)

num=0
tmp=0

amari= [0]

for i in range(n):
    num = tmp + int(s[-1-i])*(10**(i))
    tmp = num%2019
    amari.append(tmp)

from collections import Counter
ctr = Counter(amari)
ans = 0
for val in ctr.values():
        ans += val*(val-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
ans = 0
for i in range(len(s[:-3])):
    for j in range(4,len(s)+1):
        if j > i and j-i > 4:
            num = int(s[i:j])
            if num % 2019 == 0:
                ans = ans + 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 2019 = 3 * 673 

def main():
     s = input()
     l = len(s)
     ans = 0

     for start in range(l):
          tgt = int(s[start:])
          while(tgt >= 2019):
               if check_2019(tgt):
                    ans += 1
                    tgt = tgt // 1000
               else:
                    tgt = tgt // 10
               
     print(ans)

def check_2019(n):
     if n % 2019 == 0:
          return True
     else:
          return False

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
from functools import reduce
import numpy as np
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def FLI(): return [int(i) for i in input().split()]
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

T = S()

def match(v):
    return int(v) % 2019 == 0

cnt = 0

M = {}
X = []
for i in range(0, len(T)):
    for j in range(i+1, len(T)+1):
        s = T[i:j]
        if s in M:
            cnt += 1
            continue

        if s in X:
            continue

        if match(s):
            M[s] = True
            cnt += 1
        X.append(s)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
ln = len(S)
T = []

for i in range(ln-3):
    for j in range(i+4,ln+1):
        #print(S[i:j])
        T.append(int(S[i:j]))
        """
        if int(S[i:j]) % 2019 == 0:
            cnt += 1
        """

T = list(map(lambda x: x % 2019, T))
print(T.count(0))

Submission status: Time Limit Exceeded

Solution code:

def Sol(st):
    n = len(st)
    count = 0
    for i in range(n):
        for j in range(i+3,n):
            if int(st[i:j+1])%2019 == 0:
                count += 1

    print(count)
    return

    

#for t in range(int(input())):
#n,k,c,d = map(int,input().split())
n = input()
#arr = []
#for i in range(n):
#    arr.append(input())
#arr = list(map(int,input().split()))
#k = input()
#n = int(input())
Sol(n)

Submission status: Time Limit Exceeded

Solution code:

from collections import Counter

def resolve():
  S_string = input()

  if len(S_string) < 4:
    print("0")
    return True

  count = 0
  d_start = 4
  d_end = len(S_string)

  # for i in range(0, d_end - 3):
  counter = Counter([int(S_string[i:i + d]) % 2019 for i in range(0, d_end - 3) for d in range(d_start, d_end + 1 - i)])
  count = counter[0]

  # for i in range(0, d_end - 3):
  #   for d in range(d_start, d_end + 1 - i ):
  #     # print(int(S_string[i: i + d]))
  #     if (int(S_string[i: i + d])%2019==0):
  #       count += 1

  print(count)

if __name__ == "__main__":
  resolve()

Submission status: Time Limit Exceeded

Solution code:
i = list(input())
pattern = int((len(i) * (len(i) + 1)) / 2)
count = 0
a, b = 0, 0
for j in range(pattern):
    if int("".join(map(str, i[a: b + 1]))) % 2019 == 0 and int("".join(map(str, i[a: b + 1]))) != 0:
        count += 1
    if b == len(i) - 1:
        a += 1
        b = a + 3  # 4桁未満は調べても意味ない
    else:
        b += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
#--Multiple of 2019

S = (input())

#print(S[0:1]) #-18
number = len(S)
k = 0
#integer = int(S[0]) + int(S[2])
for i in range(number):
  for j in range(number+1):
    if(i < j):
#      print(S[i:j])
      q, judge = divmod(int(S[i:j]), 2019)
#      print("judge", judge)
      if(judge == 0):
        k = k + 1
      else:
        pass

print(k)
#print(S)

Submission status: Time Limit Exceeded

Solution code:
import itertools

s = input()
A = list(int(x) for x in s)
S = [0] * (len(s) + 1)
S[1:] = itertools.accumulate(A)

ans = 0
for j in range(4, len(s)):
    for i in range(len(s) - j + 1):
        x = int(s[i:i + j])
        if (S[i + j] - S[i]) % 3 == 0:
            if int(s[i:i + j]) % 673 == 0:
                ans += 1
                i = i + j - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
S=input()

x=[i for i in range(1,10000)]
x=map(lambda i:str(i*2019),x)



ans=0

for i in x:
    t=len(re.findall("(?={})".format(i),S))
    ans+=t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

S = str(input())
Stack = []
for i in range(len(S)-3):
    for j in range(i+3, len(S)+1):
        Stack.append(int(S[i:j]))

print(np.count_nonzero(np.array(Stack) % 2019 == 0))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
N=[0]*2020
x=0
for i in reversed(range(n)):
    x+=int(s[i])*(10**(n-i-1))
    N[x%2019]+=1
ans=0
ans+=N[0]
for j in N:
    ans+=j*(j-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(map(int, list(input())))
n = len(S)
ans=0
for i in range(n-4):
    for j in range(i+3,n):
        NUM=0
        cnt=0
        for p in range(j,i-1,-1):
            NUM += S[p] * 10**cnt
            cnt += 1
        if NUM%2019==0 and NUM!=0:
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# ABC164d
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # 再帰関数を使わない限りPypyで出すこと

    s = input()
    n = len(s)-1
    c = 0

    def modinv(a, mod=10**9+7):
        return pow(a, mod - 2, mod)

    def combination(n, r, mod=10**9+7):
        r = min(r, n-r)
        res = 1
        for i in range(r):
            res = res * (n - i) * modinv(i+1, mod) % mod
        return res

    import math

    def c_count(n, r):  # 組み合わせ
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    counter = [0] * 2019
    number = 0
    for i in range(n):
        # print(s[n-i-1])
        number += int(s[n-i-1])*10**(i+1)
        counter[number % 2019] += 1
    counter[0] += 1

    for i in range(len(counter)):
        num = counter[i]
        if num >= 2:
            c += c_count(num, 2)

    print(c)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
f_can = list(itertools.combinations(range(len(str(N))+1),2))
s_can = [[int(f), int(b)] for f,b in f_can if int(b)-int(f)>3]
t_can = [str(N)[int(f): int(b)] for f,b in s_can if int(b)-int(f)>3]
print(len([i for i in t_can if int(i)%2019==0]))

Submission status: Time Limit Exceeded

Solution code:
s=input()
n=len(s)
c=s[:]
ans=0
for i in range(n-3):
    j=i+4
    while j<n+1:
        if int(s[i:j])%2019==0:
            ans+=1
            j+=3
        else:
            j+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
input_str = input()

output_cnt = 0
slice_size = 2019
to_i = len(input_str) - 3
to_j = len(input_str) + 1
for i in range(to_i):
  from_j = 3 + i
  for j in range(from_j, to_j):
    sliced_value = int(input_str[i : j])
    if sliced_value % slice_size == 0:
      output_cnt += 1

print(output_cnt)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
n = len(S)
count = 0
for i in range(0,n-3):
    for j in range(i+3,n):
        count += int(S[i:j+1])%2019 == 0
print(count)

Submission status: Time Limit Exceeded

Solution code:
#0がない
s = input()
ans = 0
size = len(s)
for i in range(size):
    for j in range(i+5, min(size+1, i+120)):
        if int(s[i:j])%2019==0:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
si = list(s)
count = len(s)
min = 3
ans =0
for i in range(count-min-1):
    for j in range(i+min, count):
        if int(''.join(si[i:j+1])) %2019 == 0:
                ans +=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()

deque_0, deque_1, deque_2 = deque(), deque(), deque()
lis = [0] * len(S)

tmp = 0
for i in range(len(S)):
    tmp = (tmp + int(S[i])) % 3
    lis[i] = tmp
    if tmp == 0:
        deque_0.append(i)
    elif tmp == 1:
        deque_1.append(i)
    elif tmp == 2:
        deque_2.append(i)

tmp = 0
num = 0
for i in range(len(S)-3):
    if tmp == 0:
        for j in range(len(deque_0)):
            var1 = deque_0[j]+1
            var = int(S[i:var1])
            if var % 2019 == 0:
                num += 1
    elif tmp == 1:
        for j in range(len(deque_1)):
            var1 = deque_1[j]+1
            var = int(S[i:var1])
            if var % 2019 == 0:
                num += 1
    elif tmp == 2:
        for j in range(len(deque_2)):
            var1 = deque_2[j]+1
            var = int(S[i:var1])
            if var % 2019 == 0:
                num += 1
    tmp = lis[i]
    if tmp == 0:
        deque_0.popleft()
    elif tmp == 1:
        deque_1.popleft()
    elif tmp == 2:
        deque_2.popleft()

print(num)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
ans = 0
for i in range(n):
    for j in range(i + 1, n + 1):
        t = int(s[i : j])
        ans += 1 if t % 2019 == 0 else 0

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#d
N = int(input())
s  = str(N)
def fun(a,b):
    ans=0
    for x in range(len(a)-b+1):
        if int(a[x:x+b])%2019==0:
            ans+=1
    return ans

if len(s)<4:
    print(0)
else:
    ans=0
    for x in range(3,len(s)):
        ans+=fun(s,x)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
s = input()
b=int(s)
a = [int(c) for c in s]
total=0
for i in range(0,len(a)-2):
  for j in range(i+4,len(a)+1):
    d=s[i:j]
    e=int(d)
    if e%2019==0:
      total+=1
print(total)

Submission status: Time Limit Exceeded

Solution code:
s=input()

k=0

for i in range(len(s)):

    for j in range(len(s)+1):

        if j<=i:

            pass

        else:

            if int(s[i:j])%2019 == 0:

                k+=1

            else:pass


print(k)

Submission status: Time Limit Exceeded

Solution code:
number = str(input())
a = [number[i:j] for i in range(0, len(number)-1) for j in range(1, len(number)+1) if number[i:j] != '']
print(len([x for x in a if int(x)%2019==0]))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

S = input()
lis_2019 = [2019 * i for i in range(1, 99)]
cnt = 0
S_len = len(S)
cnt_1 = 0
S_lis = [S]

for i in range(4, S_len):
    while cnt_1 != S_len - i + 1:
        #print(S[cnt_1:cnt_1+1+i])
        S_lis.append(S[cnt_1:cnt_1+i])
        cnt_1 += 1
    cnt_1 = 0

for i in S_lis:
    for j in lis_2019:
        if str(i) == str(j):
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
num=input()
len = len(num)
count=0
for i in range(len+1):
  for j in range(i+4,len+1):
    if int(num[i:j])%2019==0:
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
t = input()
s = 0
for i in range(len(t)):
  for j in range(i+1,len(t)+1):
    if int(t[i:j]) % 2019 == 0:
           s = s + 1
    else:
           s = s
           
print(s)

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
m = []
ans=0
for i in range(l-4):
    for j in range(i+4,l+1):
        m.append(int(S[i:j])%2019)
print(m.count(0))

Submission status: Time Limit Exceeded

Solution code:
def digitSum(n):
    # 数値を文字列に変換
    s = str(n)
    # １文字ずつ数値化し配列にする。
    array = list(map(int, s))
    # 合計値を返す
    return sum(array)
S = input()
Scount = 0
for i in range(len(S)-1):
    for j in range(1,len(S)):
        if (i+j)<=len(S):
            part_S = S[i:i+j]
            num = int(part_S)
            if num % 673 == 0:
                num = num//673
                sum3 = digitSum(num)
                while sum3 != digitSum(sum3):
                    sum3 = digitSum(sum3)
                if sum3 == 3 or sum3 == 6 or sum3 ==9:
                    Scount += 1
print(Scount)

Submission status: Time Limit Exceeded

Solution code:
S = input()
lenS = len(S)
S = int(S)
rui = [0] * 2019
for i in range(lenS + 1):
    rui[((S % (10 ** i)) % 2019)] += 1

ans = 0
for i in rui:
    if i >= 2:
        ans += (i * (i - 1) // 2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=len(S)

List = []
for i in range(224):
    List.append((10**i)%2019)
    
A = List

B = []
ans = 0
for i in range(N):
    B.append(int(S[N-i-1]))
    for j in range(len(B)-1):
        B[j] = (B[j] + int(S[N-1-i]) * A[(i-j)%224]) % 2019
        if B[j]== 0:
            ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
ans = 0
for i in range(4, n):
    ans += len([1 for j in range(n-i+1) if int(s[j:j+i])%2019 == 0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10

N = 2019
cnt = [0] * N
#print (cnt)

def dec(cnt,s):
    new = [0] * N
    for n in range(N):
        idx = (10 * n + s) % N
        new[idx] += cnt[n]
    return new


S = input()
ans = 0
for s in S:
    s = int(s)
    cnt = dec(cnt,s)
    cnt[s] += 1
    ans += cnt[0]
    # if cnt[0] != 0:
    #     print (cnt[0])
print (ans)

Submission status: Time Limit Exceeded

Solution code:

s = str(input())
n = len(s)

cnt = 0
for i in range(0,n-3):
    for j in range(i+3, n):
        num = int(s[i:j+1])
        if num%2019 == 0:
            cnt += 1
            if n-1-j < 4:
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
P = 0
for i in range(len(S)-3):
    for j in range(i+4,len(S)+1):
        M = ''.join(S[i:j])
        N = int(M)
        if N%2019 == 0:
            P += 1
print(P)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len( S )
l = [ str( 2019 * i )  for i in range( 1, 200000 ) if i % 10 != 0 ]
cnt = 0
for i in l:
    if len( i ) > N:
        break
    Sd = S
    add = Sd.find( i )
    while add != - 1:
        cnt += 1
        Sd = Sd[ add + 1: ]
        add = Sd.find( i )
print( cnt )

Submission status: Time Limit Exceeded

Solution code:
s=input()
p=set()
ans=0
for c in s:
    t=set()
    for i in p:
        i=(i*10+int(c))%2019
        if i==0:
            ans+=1
        else:
            t.add(i)
    t.add(int(c))
    p=t.copy()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

count = 0

used = {}

start = 0
end = len(s)

for i in range(4, len(s) + 1):
    for j in range(0,len(s) - i + 1):
        # print(s[j:j+i])
        if int(s[j:j+i]) in used:
            count += 1
            # print('count')
            continue
        if int(s[j:j+i]) % 2019 == 0:
            used[int(s[j:j+i])] = True
            # print('count')
            count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
from functools import lru_cache

s = input()
s_len = len(s)
n = 2019
n_len = len(str(n))
result = 0
a = 202


@lru_cache(maxsize=128)
def reduce_number(s):
    left = int(s[:-1])
    right = a * int(s[-1])
    result = str(left + right)
    return result


@lru_cache(maxsize=128)
def is_multiple(s):
    _s = s
    while True:
        _s = reduce_number(_s)
        _s_int = int(_s)
        if _s_int == n:
            return True
        elif _s_int < n:
            return False


for i in range(s_len - n_len):
    for j in range(i + n_len, s_len + 1):
        m = s[i:j]
        if is_multiple(m):
            result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
s=input()
sl=list(map(int,list(s)))
n=len(s)
ans=0
for i in range(n-4):
    for j in range(i+4,n):
        if sum(sl[i:j+1])%3==0:
            if int(s[i:j+1])%2019==0:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import re
S=input()

x=[i for i in range(1,100000)]
x=map(lambda i:str(i*2019),x)



ans=0

for i in x:
    t=len(re.findall("(?={})".format(i),S))
    ans+=t

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

strA = str(input())

i = len(strA)
x = 0
for start in range(i-3):

  j = start +3
  for end in range(j,i):
    z = int(strA[start:end]) 
    if z % 2019 == 0 and z > 0:
        x = x + 1
        end = end + 2

print (x)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import queue
import copy
import bisect#2分探索
from collections import deque #popleft(), appendleft(x)
##listでqueの代用をするとO(N)の計算量がかかってしまうので注意

def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s): return sorted(range(len(s)), key=lambda k: s[k])

#mod = 10**9+7

S = int(input())
#N, K = map(int, input().split())
#L = [int(input()) for i in range(N)]
#A = list(map(int, input().split()))
#S = [inputl() for i in range(H)]
#q = queue.Queue() #q.put(i) #q.get() 
#q = queue.LifoQueue() #q.put(i) #q.get() 

#a= [[0]*3 for i in range(5)] #2次元配列はこう準備、[[0]*3]*5だとだめ
#b=copy.deepcopy(a)  #2次元配列はこうコピーする
#w.sort(key=lambda x:x[1],reverse=True)  #二個目の要素で降順並び替え

#素数リスト
# n = 100
# primes = set(range(2, n+1))
# for i in range(2, int(n**0.5+1)):
#     primes.difference_update(range(i*2, n+1, i))
# primes=list(primes)

#bisect.bisect_left(a, 4)#aはソート済みである必要あり。aの中から4未満の位置を返す。rightだと以下
#bisect.insort_left(a, 4)#挿入

ss=str(S)
# #print(ss)
ls=len(ss)
# l=[0]*(ls+1)

    #print(l[i])
#print(l)
import heapq #優先度付きqueue


a = [0]
heapq.heapify(a)  # aを優先度付きキューに
for i in range(0,ls):
    heapq.heappush(a,int(ss[i:ls])%2019)
    #print(a)

cur=-1
count=1
ans=0

while len(a)>0:
    c=heapq.heappop(a)
    #print(c)
    if cur==-1:
        cur=c
    elif cur==c:count+=1
    else: 
        ans+=count*(count-1)//2
        count=1
        cur=c
print(ans)

# cur=-1
# ans=0
# for i, li in enumerate(l):
#     if cur==-1:
#         cur=li
#         count=1
#     elif li==cur:
#         count+=1
#     else:
#         ans+=count*(count-1)//2
#         cur=li
#         count=1

# print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
for i in range(len(S)):
    for j in range(i+1,len(S)):
        B = int(''.join(S[i:j+1]))
        if B % 673 == 0 and B % 3 == 0:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)

r, cnt = 1, 0
for l in range(n):
    if l == r:
        r += 4
    int_s = int(s[l:r])
    while r < n and int_s % 2019 != 0:
        r += 1
        int_s = int(s[l:r])
    if 2019 <= int_s and int_s % 2019 == 0:
        cnt += 1
    if r == n:
        r = l + 4

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = list(str(s))
n = len(l)

c = 0
for i in range(n):
    for j in range(i+4,n):
        t = ''.join(l[i:j+1])
        if int(t)%2019 == 0:
            c = c+1
            
print(c)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()

    ans = 0
    length = len(S)
    for i in range(length-3):
        sub_S = int(S[i:])
        for j in range(0, length-i-3):
            if (sub_S//10**j)%2019 == 0:
                ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0
num = []

for i in range(len(s)):
    for j in range(len(s)-i):
        num = []
        num = s[i:i+j+1]
        num = ''.join(num)
        if int(num)%2019 == 0:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
n = int(input())
#文字列にバラす
l = [int(x) for x in list(str(n))]
N = len(l)
#print(N)



#元の文字列をfに保存
f = l

#print(l)
#終点
j = 0
#始点
i = 0

#2019の倍数
con =0

#print(l[0:4])

for a in range(N):
    for b in range(a+4,N+1):
        l = f[a:b]
        new_n = int(reduce(lambda x, y: x + y, [str(x) for x in l]))
        #print(new_n)
        if new_n % 2019 == 0:
            con +=1
            #print("Yes")
    
print(con)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
    
def print_ans(S):
    """Test Case
    >>> print_ans('1817181712114')
    3
    >>> print_ans('14282668646')
    2
    >>> print_ans('2119')
    0
    """
    target_list = []
    itr = 2019
    for i in range(1, 9906):
        tmp = i * itr
        if tmp > int(S):
            break
        elif '0' in list([s for s in str(tmp)]):
            continue
        else:
            target_list.append(tmp)
    ans = 0
    if len(S) <= 3:
        print(0)
    else:
        for i in range(len(S)-2):
            for j in range(i + 2, len(S)):
                tmp = S[i:j+1]
                if len(tmp) > 8:
                    break
                if int(tmp) in target_list:
                    ans = ans + 1
        print(ans)
            
            

if __name__ == '__main__':
    S = input().rstrip()
    print_ans(S)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
cs = [0] * (n + 1)
for i, x in enumerate(map(int, list(s))):
    cs[i + 1] = cs[i] + x
ans = 0
for i in range(n):
    for j in range(i + 3, n + 1):
        if (cs[j] - cs[i]) % 3 != 0:
            continue
        k = int(s[i:j])
        if k % 2019 == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np

s = input()
# nums = np.array(s)

# print(len(nums))

count = 0
i_check = len(s)-3
j_check = len(s)
for i in range(i_check):
    for j in range(i+3, j_check):
        tmp = int(s[i:j+1])
        if tmp % 2019 == 0:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=2019
numMultiples = 0
seenRemainders = []
for i in range(len(s)):
  l={}
  seenRemainders.append(l)
for i in range(len(s)):
    remainder = 0
    prefixesFound = 0
    for j in range(i,len(s)):
        remainder = (10 * remainder + int(s[j])) % k
        if remainder not in seenRemainders[j]:  
            seenRemainders[j][remainder]=1
        else:
            break
        if remainder == 0:
            prefixesFound+=1
    numMultiples += prefixesFound * (prefixesFound + 1) // 2
print(numMultiples)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
ans = 0
for i in range(N - 3):
    j = i + 4
    while j <= N:
        if int(S[i: j]) % 2019 == 0:
            ans += 1
            j += 4
            continue
        j += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
digitnum = len(S)
ans = 0
ansmax = 200000 // 2019 + 1

for begindigit in range(0, digitnum - 4):
    for enddigit in range(begindigit + 4, digitnum + 1):
        judgeS = int(S[begindigit:enddigit])
        if int(S[begindigit:enddigit]) % 2019 == 0:
            ans = ans + 1
            if ans == ansmax:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=int(input())
n=len(str(s))
t=0
f=str(s)
for j in range(n):
    j+=1
    for i in range(j):
        k=int(f[i:j])
        if k%2019==0 and k!=0:
            t+=1
print(t)

Submission status: Time Limit Exceeded

Solution code:
a = input()
lists= [1,4,5,6,8,9]
count =0
for i in range(len(a)):
    if int(a[-1])  in [1,4,5,6,8,9]:

        for j in range(len(a)-3):
            if int(a[-(j+4):]) % 2019 == 0:
                count +=1

    a = a[:-1]

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = input()
lenS = len(S)
S = int(S)
rui = [0] * 2019
for i in range(lenS + 1):
    rui[((S % (10 ** i)) % 2019)] += 1

ans = 0
for i in rui:
    if i >= 2:
        ans += (i * (i - 1) // 2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()

sij = [int(S[i:j+1]) % 2019 == 0 for i in range(0, len(S)-3) for j in range(3, len(S)) if len(S[i:j+1]) > 3 and len(S[i:j+1]) <= 200000 and i <= j]

print(sij.count(True))

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = len(s)
su = 0
right = 0
if l<=3:
  print(0)
  exit()
for i in range(4,l+1):
  right = i
  left = 0
  while right<=l:
    if int(s[left:right])%2019==0:
      su += 1
      left = right-1
      right += i-1
    else:
      right += 1
      left += 1
  while left<l-i:
    if int(s[left:])%2019==0:
      su += 1
      left += 1
    else:
      left += 1
print(su)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 26 20:57:47 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

x = 2019
S = list(input())
l = len(S)

a, b = [],[0]
s = ''
S.reverse()
for i in range(l):
#    a.append(int(S[:i+1]) % x)
#    if S[i] != '0':
    s = S[i] + s
    b.append(int(s) % x)

ans = 0

xx = [0 for _ in range(2019)]
for i in range(len(b)):
    xx[b[i]] += 1

def a(n):
    return n*(n+1)//2 

for i in range(len(xx)):
    if xx[i] > 1:
        ans += a(xx[i] - 1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
 
cnt = 0

i = 0
up = 1
while i < len(s)-3:
  j = i+3
  up = 1
  while j < len(s):
    num = s[i:j+1]
    if int(num) % 2019 == 0:
      cnt += 1
      up = 4
    j += 1
  i+=up
      
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans=0
l = len(s)
for i in range(l+1):
    for j in range(i+1,l+1):
        sij= s[i:j]
        sij=int(sij)
        if sij%2019==0:
            ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = len(s)

x = 0
for i in range(4,num+1):
    for j in range(num - i + 1):
        if int(s[j:j+i]) % 2019 == 0:
            x += 1

print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys
## io
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))
MIIZ = lambda: list(map(lambda x: x-1, MII()))
## dp
INIT_VAL = 0
MD2 = lambda d1,d2: [[INIT_VAL]*d2 for _ in range(d1)]
MD3 = lambda d1,d2,d3: [MD2(d2,d3) for _ in range(d1)]
## math
DIVC = lambda x,y: -(-x//y)
DIVF = lambda x,y: x//y

def main():
    s = IS()
    n = len(s)
    cnt = 0
    for i in reversed(range(3,n)):
        if int(s[i-1])%2 == 0:
            if int(s[i])%2 == 0: pass
            else: continue
        for j in reversed(range(0,i-3)):
            if int(s[j:i+1])%2019 == 0:
                cnt += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    s_len1 = len(s)-3
    s_len2 = len(s)+1
    s_tuple = ((i,j) for i in range(s_len1) for j in range(i+4, s_len2) if int(s[i:j])%2019==0)
    return len(tuple(s_tuple))

if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
s=str(input())
n=len(s)
ans=0
for i in range(3,n):
  for j in range(i-2):
    if int(s[j:i+1])%2019==0:
      ans+=1
print(ans)