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:
from collections import defaultdict

S = input()
N = len(S)

M = [0] * N
A = [0] * N
SUM = [0] * (N + 1)
MOD = 2019

x = 1
for i in range(N):
    M[i] = x % 2019
    x *= 10
for i, x in enumerate(S[::-1]):
    A[i] = M[i] * int(x) % MOD
for i in range(N):
    SUM[i + 1] = (SUM[i] + A[i]) % MOD

D = defaultdict(int)
ans = 0
for x in SUM:
    ans += D[x]
    D[x] += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


ans = 0
for i in range(l - 3):
  for j in range(4 + i, l+1):
    b = ''
    a = s[i:j]
    #print(a)
    if int(a) % 2019 == 0:
      ans = ans + 1
      #print('ue')

print(ans)

Submission status: Time Limit Exceeded

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


ans = 0
for i in range(l - 3):
  for j in range(4 + i, l+1):
    b = ''
    a = s[i:j]
    for k in range(len(a)):
      b = b + a[k]
    #print(b)
    if int(b) % 2019 == 0:
      ans = ans + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
l = len(s)
p = 0
for i in range(l):
    for j in range(l+1):
        if j - i >=4:
            a = int(s[i:j])
            if -10**(-3) < a % 673 <10**(-3):
                if a % 3 ==0:
                    p +=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
l = len(s)
p = 0
for i in range(l):
    for j in range(l+1):
        if j - i >=4:
            a = int(s[i:j])
            if -10**(-4) < a % 673 <10**(-4):
                if a % 3 ==0:
                    p +=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
l = len(s)
p = 0
for i in range(l):
    for j in range(l+1):
        if j - i >=4:
            a = int(s[i:j])
            if -10**(-6) < a % 673 <10**(-6):
                if a % 3 ==0:
                    p +=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
mod = 2019
s = input()
n = len(s)
w = 0
d = [0] * mod
p = 1
for i in reversed(range(n)):
	w += int(s[i]) * p
	p *= 10
	w %= mod
	d[w] += 1
d[0] += 1
o = 0
for x in d:
	o += x * (x - 1) // 2
print(o)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
def function(S):
    S_num = int(S)
    digit = len(S)
    multiple = 2019
    
    # make mod 2019 for each digit
    # 12114 -> 100
    # 2114 -> 2109
    # 114 -> 114
    digitAmari = []
    num = S_num
    while digit >= 0:
        digitAmari.append(num % multiple)
        num = num % pow(10, digit-1)
        digit -= 1
    
    # find same mod.
    # 余りが等しい場合、その区間は2019で割り切れるはず。
    kukans = []
    for i in range(0, len(digitAmari)):
        for j in range(i+1, len(digitAmari)):
            if digitAmari[i] == digitAmari[j]:
                kukans.append([i,j])
    return str(len(kukans))

if __name__ == '__main__':
    print(function(input()))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = input()
n = len(S)

dp = [0]*(n+1)

dp[n-1] = int(S[-1])

for i in range(n-2,-1,-1):
    dp[i] = (dp[i+1]+(10**(n-i-1))*int(S[i]))%2019

ans = Counter(dp)
cnt = 0
for i in ans.values():
    cnt += i*(i-1)//2
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = list(map(int,list(input())))
n = len(S)

dp = [0]*(n+1)

dp[n-1] = S[-1]

for i in range(n-2,-1,-1):
    dp[i] = (dp[i+1]+(10**(n-i-1))*(S[i]))%2019

ans = Counter(dp)
cnt = 0
for i in ans.values():
    cnt += i*(i-1)//2
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
l = [0]*2019
l[0] = 1
n = 0
out = 0
for i in range(N-1, -1, -1):
    n += int(S[i]) * (10**(N-1 - i))
    r = n % 2019
    l[r] += 1
for m in l:
    out += (m*(m-1))//2 
print(out)

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod = 2019
cnt = [0]*mod
cnt[0] = 1
n = 0
t = 1
for i in map(int,S[::-1]):
    n += i*t
    cnt[n%mod] += 1
    t *= 10
print(sum(i*(i-1)//2 for i in cnt))

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod = 2019
cnt = [0]*mod
cnt[0] = 1
n = 0
t = 1
for i in reversed(S):
    n += int(i)*t
    cnt[n%mod] += 1
    t *= 10
print(sum(i*(i-1)//2 for i in cnt))

Submission status: Time Limit Exceeded

Solution code:
S = (input())
cnt = [0]*2019
cnt[0] = 1
n = 0
t = 1
for i in reversed(S):
    x = int(i)
    n += x*t
    cnt[n%2019] += 1
    t *= 10
print(sum(i*(i-1)//2 for i in cnt))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = (input())
cnt = deque([0]*2019)
cnt.appendleft(1)
n = 0
t = 1
for i in reversed(S):
    x = int(i)
    n += x*t
    cnt[n%2019] += 1
    t *= 10
cnt2 = [i*(i-1)//2 for i in cnt]
print(sum(cnt2))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
S = (input())
A = deque([0])
cnt = deque([0]*2019)
cnt.appendleft(1)
n = 0
t = 1
for i in reversed(S):
    x = int(i)
    n += x*t
    A.append(n%2019)
    cnt[n%2019] += 1
    t *= 10
cnt2 = [i*(i-1)//2 for i in cnt]
print(sum(cnt2))

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from collections import Counter
S = (input())
A = deque([0])
n = 0
t = 1
for i in reversed(S):
    x = int(i)
    n += x*t
    A.append(n%2019)
    t *= 10
C = Counter(A)
cnt= 0
for v in C.values():
    cnt += v*(v-1)//2
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from collections import Counter
S = list((input()))
A = deque([0])
B = deque([])
for i in range(len(S)):
    n = int(''.join(S[len(S)-2-i:]))
    A.append(n)
for j in A:
    B.append(j%2019)
C = Counter(B)
cnt= 0
for v in C.values():
    cnt += v*(v-1)//2
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
l = [0]*2019
n = 0
out = 0
for i in range(N-1, -1, -1):
    n += int(S[i]) * (10**(N-1 - i))
    r = n % 2019
    l[r] += 1
for i in range(2019):
    m = l[i]
    out += (m*(m-1))//2 
    if i == 0:
        out += m
print(out)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
l = list()
n = 0
out = 0
for i in range(N-1, -1, -1):
    n += int(S[i]) * (10**(N-1 - i))
    r = n % 2019
    l.append(r)
for i in range(2019):
    m = l.count(i)
    out += (m*(m-1))//2 
    if i == 0:
        out += m
print(out)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
s=input()
n=len(s)
def mod10(x):
    p=x//10000000
    q=x-10000000*p
    if p==0:
        return (10**q)%2019
    return ((10**q)*mod10(p))%2019

def T(i):
    if i==n-1:
        return int(s[n-1])
    return T(i+1)+10**(n-1-i)*int(s[i])
dict={}
def modi(i,dict):
    if i in dict.keys():
        return dict[i]
    else:
        if i==n-1:
            dict[i]=int(s[i])
            return int(s[i])
        else:
           # c=modi(i+1,dict)+(10**(n-1-i)*int(s[i]))
            c=modi(i+1,dict)+(mod10(n-1-i)*int(s[i]))
            c=c%2019
            dict[i]=c
            return c

modnum={}
for i in range(n):
    #print(T(i))
    if int(s[i])==0:
        continue
    x=modi(i,dict)
 #   print(T(i),x)
    if x not in modnum.keys():
        modnum[x]=1
    else:
        modnum[x]+=1
sum=0
for md in modnum.keys():
    a=modnum[md]
    if md==0:
        sum+=a
        sum+=-(a*(a-1)//2)
   # print(a*(a-1)//2)
    sum+=a*(a-1)//2
#print(modnum)
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000000)
s=input()
n=len(s)
def mod10(x):
    p=x//100
    q=x%100
    if p==0:
        return (10**q)%2019
    return (mod10(q)+mod10(p))%2019


def T(i):
    if i==n-1:
        return int(s[n-1])
    return T(i+1)+10**(n-1-i)*int(s[i])
dict={}
def modi(i,dict):
    if i in dict.keys():
        return dict[i]
    else:
        if i==n-1:
            dict[i]=int(s[i])
            return int(s[i])
        else:
            c=modi(i+1,dict)+(10**(n-1-i)*int(s[i]))
          #  c=modi(i+1,dict)+(mod10(n-1-i)*int(s[i]))
            c=c%2019
            dict[i]=c
            return c

modnum={}
for i in range(n):
    #print(T(i))
    if int(s[i])==0:
        continue
    x=modi(i,dict)
 #   print(T(i),x)
    if x not in modnum.keys():
        modnum[x]=1
    else:
        modnum[x]+=1
sum=0
for md in modnum.keys():
    a=modnum[md]
    if md==0:
        sum+=a
        sum+=-(a*(a-1)//2)
   # print(a*(a-1)//2)
    sum+=a*(a-1)//2
#print(modnum)
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = input()

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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

Submission status: Time Limit Exceeded

Solution code:
import collections
s = list(input())
s = list(map(int,s))
s_mod =[0]
mod = 0
for i in range(len(s)):
    a = s[len(s) - 1- i]* (10**i)
    mod += a
    mod = mod % 2019
    s_mod.append(mod)
s_values = list(collections.Counter(s_mod).values())
print(int(sum(s_values[i]*(s_values[i]-1)/2 for i in range(len(s_values)))))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = list(input())
S.reverse()
L = len(S)
dp = [0] * (L+1)
tmp = 0
for i in range(L):
    tmp += int(S[i]) * pow(10, i)
    tmp %= 2019
    dp[i] = tmp

c = Counter(dp)
ans = 0
for i, v in c.items():
    ans += v * (v-1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N_S = input()
N = int(N_S)
N_len = len(N_S)
MOD_N = [0]*N_len

for i in range(N_len):
  MOD_N[i] = int(N_S[i:N_len]) % 2019
  
c = collections.Counter(MOD_N)

Key = list(c.keys())
Num = list(c.values())
M = len(Key)
ans = 0

for i in range(M):
  if Key[i] == 0:
    ans += Num[i]
  else:
    ans += Num[i] * (Num[i] - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N_S = input()
N = int(N_S)
MOD_N = [0]*len(N_S)

for i in range(len(N_S)):
  MOD_N[i] = int(N_S[i:len(N_S)]) % 2019
  
c = collections.Counter(MOD_N)

Key = list(c.keys())
Num = list(c.values())
M = len(Key)
ans = 0

for i in range(M):
  if Key[i] == 0:
    ans += Num[i]
  else:
    ans += Num[i] * (Num[i] - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = input()
N_S = str(N)
N_len = len(N)
k=0

for A in list(itertools.combinations(range(0,N_len),2)):
  if A[1] - A[0] <= 3:
    continue
  if int(N_S[A[0]:A[1]+1]) % 2019 == 0:
    k += 1
print(k)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = input()
N_S = str(N)
N_len = len(N)
k=0

for A in list(itertools.combinations(range(0,N_len),2)):  
  if int(N_S[A[0]:A[1]+1]) % 2019 == 0:
    k += 1
print(k)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = input()
N_S = str(N)
N_len = len(N)
k=0

for A in list(itertools.combinations(range(0,N_len),2)):
  tar_S = N_S[A[0]]
  for i in range(A[0]+1,A[1]+1):
    tar_S = tar_S + N_S[i]
  if int(tar_S) % 2019 == 0:
    k += 1
print(k)

Submission status: Time Limit Exceeded

Solution code:
N=input()
List=list(N)
M=len(List)
ans=0
Mods=[0]*(M+1)
Sum=""
for i in range(M):
  Sum=List[M-i-1]+Sum
  Mods[M-i-1]=int(Sum)%2019
Mods.sort()

import collections
C=collections.Counter(Mods)
for j in range((Mods[M])):
  k=C[j]
  ans+=k*(k-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N=input()
List=list(N)
M=len(List)
ans=0
Mods=[0]*(M+1)
Mods[M]=0
Sum=""
for i in range(M):
  Sum=List[M-i-1]+Sum
  Mods[M-i-1]=int(Sum)%2019
Mods.sort()

import collections
C=collections.Counter(Mods)
for j in range((Mods[M])):
  if C[j]!=1:
    k=C[j]
    ans+=k*(k-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()

mods = []
cnt = 0
for i in range(len(S)):
    mod = int(S[i:]) % 2019
    if mod == 0:
        cnt += 1
    else:
        cnt += mods.count(mod)
        mods.append(mod)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()

mods = []
cnt = 0
for i in range(len(S)):
    mod = int(S[i:]) % 2019
    if mod == 0:
        cnt += 1
    else:
        mods.append(mod)

cnt += len(mods) - len(set(mods))
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()

mods = []
for i in range(len(S)):
    mod = int(S[i:]) % 2019
    mods.append(mod)

import collections
c = collections.Counter(mods)
cnt = 0
for k, v in c.items():
    if k == 0:
        cnt += v
    if v > 1:
        cnt += v * (v-1) / 2
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
s = input()
countRemainder = [0] * 2019
cnt = 0

for i in range(len(s)):
    countRemainder[int(s[i:]) % 2019] += 1

countRemainder[0] += 1

for i in range(2019):
    cnt += int(countRemainder[i] * (countRemainder[i] - 1) / 2)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

minLength = 4
maxLength = len(s)
cnt = 0

skipFlag = 0
skipFlag2 = 0

for length in range(minLength,maxLength+1):
    for position in range(maxLength - length + 1):
        if skipFlag > 0:
            skipFlag += 1
            if skipFlag == 4:
                skipFlag = 0
            continue
        if skipFlag2 > 0:
            skipFlag2 += 1
            if skipFlag2 == 2:
                skipFlag2 = 0
            continue
        num = int(''.join(s[position:position+length]))
        if num % 3 == 0:
            if num % 673 == 0:
                cnt += 1
                skipFlag = 1
            else:
                skipFlag2 = 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

minLength = 4
maxLength = len(s)
cnt = 0

for length in range(minLength,maxLength+1):
    for position in range(maxLength - length + 1):
        num = int(''.join(s[position:position+length]))
        if num % 3 == 0:
            if num % 673 == 0:
                cnt += 1
                position += 4

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

minLength = 4
maxLength = len(s)
cnt = 0

for position in range(maxLength):
    modCnt = 0
    for i in range(position, maxLength):
        modCnt = (modCnt + int(s[i])) % 3
        if modCnt == 0:
            num = int(''.join(s[position:i+1]))
            if num % 673 == 0:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

s_mod3 = []

for i in range(len(s)):
    s_mod3.append(int(s[i]) % 3)

minLength = 4
maxLength = len(s)
cnt = 0

for position in range(maxLength):
    modCnt = 0
    for i in range(position, maxLength):
        modCnt = (modCnt + s_mod3[i]) % 3
        if modCnt == 0:
            num = int(''.join(s[position:i+1]))
            if num % 673 == 0:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

minLength = 4
maxLength = len(s)

cnt = 0

for length in range(minLength,maxLength+1):
    for position in range(maxLength - length + 1):
        num = int(''.join(s[position:position+length]))
        if num % 3 == 0:
            if num % 673 == 0:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

minLength = 4
maxLength = len(s)

cnt = 0

for length in range(minLength,maxLength+1):
    for position in range(maxLength - length + 1):
        num = int(''.join(s[position:position+length]))
        if num % 2019 == 0:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
l = len(s)
p = 0
for i in range(l):
    for j in range(l+1):
        if j - i >=4:
            a = int(s[i:j])
            if a % 673 ==0:
                if a % 3 ==0:
                    p +=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
l = len(s)
p = 0
for i in range(l):
    for j in range(l+1):
        if j - i >=4:
            a = s[i:j]
            a = int(a)
            if a % 3 ==0:
                if a % 673 ==0:
                    p +=1
print(p)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import math

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()[::-1]
    ans = 0
    mods = [0] * 2019
    mods[0] = 1
    current = 0
    x = 1
    for s in S:
        current = (current + x * int(s)) % 2019
        ans += mods[current % 2019]
        mods[current % 2019] += 1
        x = x * 10 
        
    print(ans)
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

m=[0]*(N+1)

for i in range(1,N+1):
  m[i]=int(S[i-1:N]) % 2019

dic={}
for i in range(1,N+1):
  if m[i] in dic:
    dic[m[i]].append(i)
  else:
    dic[m[i]]=[i]

c=0
for k,v in dic.items():
  if k == 0:
    c+=len(v)
  else:
    c+=int(len(v)*(len(v)-1)/2)
print(c)

Submission status: Time Limit Exceeded

Solution code:
S=input()

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

sl = len(s)

cnt = 0

for i in range(sl):
  for j in range(i+4,sl+1):
    if int(s[i:j])%2019 == 0:
      # print("{} {} {}".format(i,j,int(s[i:j])))
      cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

S=input()
S_int = int(S)
mods = [int(S[i:])%2019 for i in range(len(S))]
mods.append(0)
mods_count = collections.Counter(mods)
count = 0
for mod in mods_count:
  c = mods_count[mod]
  count += c*(c-1)//2
#print(mods)
#print(mods_count)
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

S=input()
S_int = int(S)
interval = 2019
current = 2019
#A=[]
count = 0
for i in range(len(S)):
  for j in range(i+4,len(S)+1):
    if int(S[i:j])%2019 == 0:
      #A.append([i+1,j])
      count += 1
#print(len(A))
print(count)

Submission status: Time Limit Exceeded

Solution code:
import re

S=input()
S_int = int(S)
interval = 2019
current = 2019
A=[]
for i in range(len(S)):
  for j in range(i+4,len(S)+1):
    if int(S[i:j])%2019 == 0:
      A.append([i+1,j])
print(len(A))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
ans = 0
A = [0] * 2020
A[0] = 1
for i in range(len(s)):
    mod = int(s[-i:]) % 2019
    A[mod] += 1
for i in range(len(A)):
    ans += A[i] * (A[i] - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

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

print(c)

Submission status: Time Limit Exceeded

Solution code:
# Input => 1817181712114
# Output => 3 (1,5), (5,9), (9,13)

# 1817, 18171, 8171, ..., 181718172114

# 2019 4038 8076 16152

# |o|o|o|o|ooooo K桁の場合
# (|S| - K + 1) C 1 = |S| - K + 1

# \sum^{|S|}_{k=5} {|S| - k + 1}
# |S| = N
# \sum^{N}_{k=5} {N - k + 1}
# = (N-4)*(N+1) - (\sum^{N}_{k=1}k - \sum^{4}_{k=1}k)
# = N^2-3N-4 - N(N+1)/2 - 10
# O(N^2)

def resolve(S: str) -> int:
    count = 0
    N = len(S)
    for k in range(5, N+1):
        for i in range(N+1-k):
            if int(S[i:i+k]) % 2019 == 0:
                # print(S[i:i+k])
                count += 1

    return count

if __name__ == '__main__':
    S = input()
    res = resolve(S)
    print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = int(input())

d = defaultdict(int)

for i in range(len(str(S))+1):
    t = S % (10**i)
    d[t % 2019] += 1

ans = 0
for v in d.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = input()

d = defaultdict(int)
d[0] = 1

for i in range(1, len(S)+1):
    t = int(S[-i:])
    d[t % 2019] += 1

ans = 0
for v in d.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
count = 0
if (int(S) > 2018):
    for i in range(len(S)):
        i4 = i + 4
        if (i4 <= 200000):
            for j in range(i4, len(S) + 1):
                if (int(S[i:j]) % 2019 == 0):
                    count += 1
print (count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
S=input()

sequence=[]
for i in range(len(S)):
    sequence.append(int(S[i:]))
sequence.append(0)

sequence_np=np.array(sequence)
sequence_mod=np.mod(sequence_np,2019)

count=0

dict=collections.Counter(sequence_mod)
for v in dict.values():
    count+=v*(v-1)/2
           
print(int(count))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
S=input()

sequence=[]
for i in range(len(S)):
    sequence.append(int(S[i:]))
sequence.append(0)

sequence_np=np.array(sequence)
sequence_mod=np.mod(sequence_np,2019)
count=0
for i in range(len(sequence_mod)-1):
    for j in range(i+1,len(sequence_mod)):
        if sequence_mod[i]==sequence_mod[j]:
            count+=1
           
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())


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

print(count)

Submission status: Time Limit Exceeded

Solution code:
S=input()
src=[0]*2019
ans=0
pre=0
for s in range(len(S)):
  x=int(S[-(s+1)])*(10**s)
  a=(x+pre)%2019
  src[a]+=1
  pre+=x
  ans+=src[a]-1
print(ans+src[0])

Submission status: Time Limit Exceeded

Solution code:
S=input()
src=[0]*2019
ans=0
pre=0
for s in range(len(S)):
  a=(int(S[-(s+1)])*(10**s)+pre)%2019
  src[a]+=1
  pre+=int(S[-(s+1)])*(10**s)
  ans+=src[a]-1
print(ans+src[0])

Submission status: Time Limit Exceeded

Solution code:
S=input()
src={0:0}
ans=0
pre=0
for s in range(len(S)):
  a=(int(S[-(s+1)])*(10**s)+pre)%2019
  if a not in src.keys():
    src[a]=1
  else:
    ans+=src[a]
    src[a]+=1
  pre+=int(S[-(s+1)])*(10**s)
print(ans+src[0])

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
src=[0]
for s in range(len(S)):
  src.append((int(S[-(s+1)])*(10**s)+src[s])%2019)
ans=0
s=collections.Counter(src)
s[0]-=1
for n in s.values():
  ans+=n*(n-1)//2
print(ans+s[0])

Submission status: Time Limit Exceeded

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


def main():
    S = input()

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
src=[10**n*int(S[-(n+1)]) for n in range(len(S))]
sumS=[0]
for s in range(len(src)):
  sumS.append((sumS[s]+src[s])%2019)
ans=0
s=collections.Counter(sumS)
s[0]-=1
for n in s.values():
  ans+=n*(n-1)//2
print(ans+s[0])

Submission status: Time Limit Exceeded

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

M = 1
while 10 ** length > 2019 * M:
    M = M + 1


M2019 = []
for i in range(1,M+1):
    M2019.append(2019*i)


count = 0
for j in range(len(M2019)):
    if str(M2019[j]) in S:
        count = count + 1
    else:
        continue

print(count)

Submission status: Time Limit Exceeded

Solution code:
import collections
S=input()
src=[10**n*int(S[-(n+1)]) for n in range(len(S))]
sumS=[0]
for s in range(len(src)):
  sumS.append((sumS[s]+src[s])%2019)
sumS=sumS[1:]
ans=0
s=collections.Counter(sumS)

for n in s.values():
  ans+=n*(n-1)//2
print(ans+s[0])

Submission status: Time Limit Exceeded

Solution code:
s = input()

ans = 0

for i in range(len(s)):
    for j in range(i+3 , len(s)+1):
        if int(s[i:j]) % 2019 == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N=input()
a=0
for i in range(len(N)-4):
  for k in range(i+5,len(N)+1):
    if int(N[i:k])%2019==0:
      a+=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=len(S)
#「連続する区間」についての問題では、”累積和的なもの”をとるのが定石である。
#Sの連続する区間が、右からa文字分から、右からb文字分を取り除いたものであるとき
#その区間の整数が2019の倍数になるということは、s[a]とs[b]を2019で割ったあまりが等しい
#十分条件であることは分かったが、必要条件であるのか
#結論として必要条件でもある
#なぜなら2019と10は互いに素であるからである。

T=[]
T.append(0)
tmp=""
d=[0 for _ in range(2019)]
d[0]=1
r=0
t=1
for i in range(N):
    s=S[N-1-i]
    r+=int(s)*t
    r%=2019
    t*=10
    d[r]+=1

ans=0

print(sum(i*(i-1)//2 for i in d))

Submission status: Time Limit Exceeded

Solution code:
N=input()
a=0
for i in range(len(N)-3):
  for k in range(i+4,len(N)+1):
    if int(N[i:k])%2019==0:
      a+=1
print(a)

Submission status: Time Limit Exceeded

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


T=[]
T.append(0)
tmp=""
d=[0 for _ in range(2019)]
d[0]=1
r=0
t=1
for i in range(N):
    s=S[N-1-i]
    r+=int(s)*t
    r%=2019
    t*=10
    d[r]+=1

ans=0
for i in d:
    ans+=((i*(i-1))//2)

print(ans)

Submission status: Time Limit Exceeded

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


T=[]
T.append(0)
tmp=""
d=[0 for _ in range(2019)]
d[0]=1
for i in range(N):
    s=S[N-1-i]
    tmp=s+tmp
    val=int(tmp)%2019
    d[val]+=1

ans=0
for i in d:
    ans+=((i*(i-1))//2)


print(ans)

Submission status: Time Limit Exceeded

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


T=[]
T.append(0)
tmp=""
for i in range(N):
    s=S[N-1-i]
    tmp=s+tmp
    T.append(int(tmp))

x=dict()
for i in range(len(T)):
    val=T[i]%2019
    x[val]=0 
for i in range(len(T)):
    val=T[i]%2019
    x[val]+=1
ans=0
for idx in x:
    val=x[idx]
    ans+=((val*(val-1))//2)

print(ans)

Submission status: Time Limit Exceeded

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

T=[]
T.append(0)
tmp=""
for i in range(N):
    s=S[N-1-i]
    tmp=s+tmp
    T.append(int(tmp))

x=dict()
for i in range(len(T)):
    val=T[i]%2019
    if val in x:
        x[val]+=1
    else:
        x[val]=1
ans=0
for idx in x:
    val=x[idx]
    ans+=((val*(val-1))//2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
import sys

# input = lambda : sys.stdin.readline().rstrip()

S = input()
n = len(S)

M = []

for i in range(len(S)):
    m = int(S[i:n]) % 2019
    # print(S[i:n], m)
    M.append(m)

M.append(0)  # S[n:n] = ""が抜けるので、付け加える

c = Counter(M)

sum = 0
for i in c.values():
    sum += i * (i - 1) / 2

print(int(sum))

Submission status: Time Limit Exceeded

Solution code:
#ABC164d
s = str(input())
S = int(s)
s = list(s)

for i in range(len(s)):
    s[i] = int(s[i])

ans = 0
T = 0

for i in range(len(s)):
    for j in range(i+4,len(s)):
        t = s[i:j+1]
        for i2 in range(len(t)):
            T += t[i2] * 10**(len(t)-1-i2)
        if T%2019==0 and T != 0:
            ans += 1
        T = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = [int(c) for c in S()]
    t = [0] * 2019
    r = 0
    for c in s:
        u = [0] * 2019
        u[c] = 1
        for i in range(2019):
            u[(i*10+c) % 2019] += t[i]
        t = u
        r += t[0]

    return r

print(main())