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:
S = list(map(int, list(input())))

mods = [0 for _ in range(2019)]
mods[0] = 1

num = 0
for itr, s in enumerate(S[::-1]):
  num = s * 10 ** itr + num
  mo = num % 2019
  mods[mo] += 1

#  print(itr, s, num, mo)
  
ans = 0
for m in mods:
  ans += m * (m - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(int(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)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
amari=[0]*2019
amari[0]=1

for i in range(1,l+1):
    amari[int(s[l-i:])%2019]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import Counter 
from collections import defaultdict 
def main():
    s = input() 
    s = ''.join(reversed(s))
    n = len(s)
    T = [0]
    dd = defaultdict(int) 
    dd[0] += 1
    for i in range(n):
        T.append((int(s[i])*10**i+T[i])%2019)
        dd[(int(s[i])*10**i+T[i])%2019] += 1
    ans = 0
    for i in dd.values():
        ans += i*(i-1)//2
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
from scipy import special
S = input()
a = [int(S[i:]) % 2019 for i in range(len(S))]
a.append(0)
b = [kv[1]*(kv[1]-1)//2 for kv in Counter(a).items() if kv[1] > 1]
print(sum(b))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

print(len(flat))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

print(len(flat))

Submission status: Time Limit Exceeded

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

ans = 0
amari = [0] * 2019
follow = 0
POW = 1

for i in range(N):
  Amari_ = (int(S[N-i-1]) * POW + follow) % 2019
  amari[Amari_] += 1
  follow = Amari_
  POW *= 10
  
ans = sum(list( i * (i-1)//2 for i in amari ))
ans += amari[0]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

r_list = [0] * 2019

for i in range(len(s) - 1):
  x = int(s[i:])
  r_list[x % 2019] += 1
  
r_list[0] += 1
ct = 0
for n in r_list:
  ct += n * (n - 1) / 2
  
print(int(ct))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
mods = [0] * 2019
mods[0] = 1

for i in range(n-1, -1, -1):
    mod = int(s[i:]) % 2019
    mods[mod] += 1

ans = 0
for i in range(2019):
    ans += mods[i] * (mods[i] - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

s = input()
counter = 0

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

Submission status: Time Limit Exceeded

Solution code:
s=input()[::-1]

count=[0]*2019
count[0]+=1
gh=0
for i in range(0,len(s)):
    gh+=int(s[i])*(10**i)
    count[gh%2019]+=1

ans=0

for cnt in count:
    ans+=cnt*(cnt-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

s = input()
counter = 0

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

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
amari=[0]*(l+1)
for i in range(1,l+1):
    amari[i]=int(s[l-i:])%2019

from collections import Counter
myc = Counter(amari)
val=list(myc.values())

ans=0
for i in range(len(val)):
    ans+=val[i]*(val[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    n = len(s)
    dp = [0 for _ in range(2019)]
    dp[0] += 1
    x = 0
    for i in range(n):
        x = (x+int(s[n-1-i])*(10**i))%2019
        dp[x%2019] += 1
    ans = 0
    for i in range(2019):
        ans += (dp[i]*(dp[i]-1))//2
    print(ans)
 
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
S.reverse()
amari = []
A = 0
c = 0
for i in range(len(S)):
    a = (int(A) + int(S[i]) *(10 ** i)) % 2019
    amari.append(a)
    if a == 0:
        c += 1
    A = a
print(c + len(amari) - len(set(amari)))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
S.reverse()
amari = []
A = 0
for i in range(len(S)):
    a = (int(A) + int(S[i]) *(10 ** i)) % 2019
    amari.append(a)
    A = a
c = 0
if 0 in amari:
    c += 1
print(c + len(amari) - len(set(amari)))

Submission status: Time Limit Exceeded

Solution code:
N = str(input())
l=len(N)
count :int=0
ll=l
i :int=0
while i<int(ll)-3:
  span :int=4
  while span<int(l)+1:
    if int(N[i:i+span])%2019==0:
      count+=1
    span+=1
  i+=1
  l-=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def main():
    s = list(input())
    n = len(s)
    for i in range(n):
        s[i] = int(s[i])
    dp = [0 for _ in range(2019)]
    dp[0] += 1
    x = 0
    for i in range(n):
        x = (x+s[n-1-i]*(10**i))%2019
        dp[x%2019] += 1
    ans = 0
    for i in range(2019):
        ans += (dp[i]*(dp[i]-1))//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = list(map(int,list(input())))
N = len(S)
L = [0]
for i in range(N):
    L.append((L[-1] + S[-i-1] * pow(10, i)) % 2019)
count = Counter(L)
ans = 0
for value in count.values():
    ans += value * (value - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def main():
    s = list(input())
    n = len(s)
    for i in range(n):
        s[i] = int(s[i])
    dp = [0 for _ in range(2019)]
    dp[0] += 1
    x = 0
    for i in range(n):
        x = x+s[n-1-i]*(10**i)
        dp[x%2019] += 1
    ans = 0
    for i in range(2019):
        ans += (dp[i]*(dp[i]-1))//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import re

S = str(input())
ans=0

for x in range (1,100):
  x = x*2019
  x = str(x)
  cnt = sum(1 for tmp in range(len(S)) if S[tmp:].startswith(x))
  ans = ans+cnt
print (ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
from functools import reduce
import collections

S = list(map(int,input().rstrip()))
N=len(S)
mod_list = [0]
for i in range(N):
    T = reduce( lambda a,b:10*a+b, S[i:N+1])%2019
    mod_list.append(T)
    #print(T)
count = collections.Counter(mod_list)
ans =0
for j in range(2019):
    n = count[j]
    ans+= n*(n-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S=input()

N=len(S)
d={}
for i in range(2019):
    d[i]=0

d[0] = 1

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


ans = 0
for k in d.keys():
    v = d[k]
    if v > 1:
        ans += v*(v-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
def main():
    s = list(input())
    n = len(s)
    dp = [0 for _ in range(2019)]
    dp[0] += 1
    for i in range(n):
        dp[int(''.join(s[i:]))%2019] += 1
    ans = 0
    for i in range(2019):
        ans += (dp[i]*(dp[i]-1))//2
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
L = len(S)
ans = 0
now = 0
A = [0] * 2019
A[0] = 1
d = 0
for i in range(L):
    now = now + int(S[i]) * 10**d % 2019
    now %= 2019
    d += 1
    A[now] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
L = len(S)
ans = 0
now = 0
A = [0] * 2019
A[0] = 1
d = 0
for i in range(L):
    now = now + int(S[i]) * 10**d
    now %= 2019
    d += 1
    A[now] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
mod = 2019
l = [0]*mod
ans = 0

for i in range(len(s)):
    a = int(s[i:])%mod
    l[a] += 1
l[0] += 1

for i in range(2019):
    ans += l[i]*(l[i] - 1)//2

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N):
  tmp = 0
  for j in range(i, N):
    tmp *= 10
    tmp += ord(S[j]) - ord('0')
    tmp %= 2019
    if tmp == 0: ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# input
S = input()

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

s = [int(i) for i in STR()]
l = len(s)
arr = [-1 for _ in range(l)]
tmp = 0
for i in range(l):
    tmp += s[i] * (10 ** (l - i))
    arr[i] = tmp % 2019
arr.sort()
tmp = arr[0]
cnt = 1
ans = 0
for i in range(1, l):
    if tmp == arr[i]:
        cnt += 1
    else:
        ans += cnt * (cnt - 1) // 2
        if tmp == 0:
            ans += cnt
        cnt = 1
        tmp = arr[i]
ans += cnt * (cnt - 1) // 2
if tmp == 0:
    ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# input
S = input()

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

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

s = [int(i) for i in STR()]
l = len(s)
arr = [-1 for _ in range(l)]
tmp = 0
for i in range(l):
    tmp += s[i] * (10 ** (l - i))
    arr[i] = tmp % 2019
a = list(set(arr))
b = Counter(arr)
ans = 0
for i in a:
    tmp2 = b[i]
    if i == 0:
        ans += (tmp2 + 1) * tmp2 // 2
    else:
        ans += tmp2 * (tmp2 - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

if len(s) <= 3:
  print(0)
else:
  ct = 0
  for i in range(len(s) - 3):
    for j in range(i + 3, len(s)):
      x = int(s[i:j + 1])
      if x % 2019 == 0:
        ct += 1
  print(ct)

Submission status: Time Limit Exceeded

Solution code:
import collections
S = list(map(int, input()))[::-1]
mod_list = [0 for _ in range(len(S)+1)]
x = 0
d = 0
for i, s in enumerate(S):
    x += s * pow(10, d)
    d += 1
    mod_list[i] = x%2019
c = collections.Counter(mod_list)
ans = 0
for i in range(2020):
    ans += c[i] * (c[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
S = list(map(int, input()))[::-1]
mod_list = []
mod_list.append(0)
x = 0
d = 0
for s in S:
    x += s * pow(10, d)
    d += 1
    mod_list.append(x%2019)
c = collections.Counter(mod_list)
ans = 0
for i in range(2020):
    ans += c[i] * (c[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:

import collections
S = input()
mod_list = []
mod_list.append(0)
for i in range(len(S)):
    x = int(S[i:])
    mod_list.append(x%2019)
c = collections.Counter(mod_list)
ans = 0
for i in range(2020):
    ans += c[i] * (c[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
MOD = 2019
a =[0]*MOD
a[0] = 1
c_ = 0
ans = 0
t = 1
for i in reversed(s):
  c = (c_+int(i)*t)%MOD
  a[c] += 1
  t *= 10
#  t %= MOD
  c_ = c
for i in a:
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod = 2019
rem = [0] * mod
rem[0] = 1
now = 0
for k,v in enumerate(S[::-1]):
  now += int(v) * (10 ** k)
  rem[now % mod] += 1
ans = 0
for i in rem:
  ans += i * (i-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
l=[0]*2019
l[0]=1
s,k=0,1
for c in reversed(input()):k*=10;s+=int(c)*k;l[s%2019]+=1
print(sum(v*(v-1)//2for v in l))

Submission status: Time Limit Exceeded

Solution code:
target = 2019
S = input()
N = len(S)
l = [ int( S[ : i + 1 ] ) for i in range(len(S)) ]
r =[ 1 ] + [ 0 ] * ( target - 1 )

for i in l:
  r[ ( i * 10 ** ( N - len( str(i) ) )) % target ] += 1

cnt = 0
for i in r:
  cnt += i * ( i - 1 ) // 2
    
print( cnt )

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    MOD = 2019
    r = [0]*MOD
    r[0] = 1
    z = 0
    t = 1
    for i in reversed(s):
        z = int(i) * t%MOD + z
        z %= MOD
        t *= 10
        r[z] += 1
    print(sum(i*(i-1)//2 for i in r))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
b = {}
b[0] = 1
c_ = 0
s.reverse()
for num,i in enumerate(s):
  c = (c_+int(i)*10**num)%2019
  if c in b:
    b[c] += 1
  else:
    b[c] = 1
  c_ = c
ans = 0
for i in b.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
b = {}
b[0] = 1
c_ = 0
s.reverse()
for num,i in enumerate(s):
  c = (c_+int(i)*10**num)%2019
  if c in b:
    b[c] += 1
  else:
    b[c] = 1
  c_ = c
ans = 0
for i in b.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
 
    
S = [int(s) for s in input()]
N = len(S)
y = 2019
T = [S[-1]] * N
U = [0] * y
U[0] += 1

for i in range(N-2, -1, -1):
    t = (T[i+1] + S[i] * 10 ** (N-1-i)) % y
    T[i] = t
    U[t] += 1


cnt = 0
for u in U:
    cnt += u * (u - 1) // 2

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
mod_list = [0]*2020
mod_list[0] = 1

mod = 2019

for i in range(N):
    num = int(S[N-i-1:N])
    mod_list[num%mod] += 1
    
ans = 0

for i in mod_list:
    ans += i*(i-1)//2
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
mod_list = [0]*2020
mod_list[0] = 1

mod = 2019

for i in range(N):
    num = int(S[N-i-1:N])
    mod_list[num%mod] += 1
    
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()) )
b = {}
b[0] = 1
c_ = 0
for num,i in enumerate(reversed(s)):
  c = c_+int(i)*10**num
  if c%2019 in b: 
    b[c%2019] += 1
  else:
    b[c%2019] = 1
  c_ = c
ans = 0
for i in b.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()) )
a = [0]
b = {}
b[0] = 1
for num,i in enumerate(reversed(s)):
  a.append(a[num]+int(i)*10**num)
  if (a[num+1])%2019 in b: 
    b[(a[num+1])%2019] += 1
  else:
    b[(a[num+1])%2019] = 1
ans = 0
for i in b.values():
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
a = [0]
b = [0]*2019
b[0] += 1
for num,i in enumerate(reversed(s)):
  a.append(a[num]+int(i)*10**num)
  b[(a[num+1])%2019] += 1
ans = 0
for i in b:
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(str(input()))
a = [0]
b = [0]*2019
b[0] += 1
for num,i in enumerate(reversed(s)):
  a.append(a[num]+int(i)*10**num)
  b[(a[num+1])%2019] += 1
ans = 0
for i in b:
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = input()
mods = [0]
num, ten = 0, 1
for n in S[::-1]:
    num += int(n) * ten
    num %= 2019
    mods.append(num)
    ten *= 10

counter = Counter(mods)
ret = 0
for v in counter.values():
    ret += v * (v - 1) // 2
print(ret)

Submission status: Time Limit Exceeded

Solution code:
S = input()
counter = {0: 1}
num, ten = 0, 1
for n in S[::-1]:
    num += int(n) * ten
    mod = num % 2019
    if mod in counter:
        counter[mod] += 1
    else:
        counter[mod] = 1
    ten *= 10

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
ret = 0
counter = {0: 1}
for i in range(len(S)):
    mod = int(S[-i:]) % 2019
    if mod in counter:
        counter[mod] += 1
    else:
        counter[mod] = 1

for k, v in counter.items():
    ret += v * (v - 1) // 2

print(ret)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ret = 0
counter = {0: 1}
for i in range(len(S)):
    mod = int(S[-i:]) % 2019
    if mod in counter:
        counter[mod] += 1
    else:
        counter[mod] = 1
for k, v in counter.items():
    ret += v * (v - 1) // 2

print(ret)

Submission status: Time Limit Exceeded

Solution code:
s=input()
c=0
for i in range(len(s)):
  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:
s=input()
c=0
for i in range(len(s)):
  for j in range(i+2,len(s)+1):
    if int(s[i:j])%2019==0:
      c+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = list(input())
N = len(S)

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

ans = 0

tmp = 0
for i in range(N):
    s = S[N - 1 - i]
    tmp += int(s) * 10 ** i
    ans += dic[tmp % 2019]
    dic[tmp % 2019] += 1

print (ans)

# print (dic)

Submission status: Time Limit Exceeded

Solution code:
s = input()
data = []
ans = 0
l = len(s)
for i in range(l):
  data.append(int(s[i:l]) % 2019)
data.append(0)

for x in set(data):
  if data.count(x)>1:
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(map(int, list(input())))
    m = [0]*2019
    num = 0
    ans = 0
    n = len(s)
    ten = 1
    for i in range(n):
        num += s[n-i-1]*ten
        num %= 2019
        m[num] += 1 
        ten = (ten*10)
    for mi in m:
        if mi:
            ans += (mi*(mi-1))//2
    ans += m[0]
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()

n = len(s)
amari = [0] * 2019
amari[0] += 1
tmp = ""
for i in range(1, n + 1):
    tmp = s[-i] + tmp
    amari[int(tmp) % 2019] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

n = len(s)
amari = [0] * 2019
amari[0] += 1

for i in range(n):
    amari[int(s[n-i-1:]) % 2019] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(map(int,input()))
n=len(s)
dp=[[0]*2019,[0]*2019]
dp[0][s[0]]=1
ans=0
for i in range(1,n):
    for j in range(2019):
        nxt=(10*j+s[i])%2019
        dp[1][nxt]+=dp[0][j]
    ans+=dp[1][0]
    dp[1],dp[0]=[0]*2019,dp[1]
    dp[0][s[i]]+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# s = input()
 
# MOD = 2019
# d = [0]*MOD
# d[0] = 1
# r = 0
# t = 1
# # for i in reversed(s):
# #     r += int(i)*t
# #     r %= MOD
# #     t *= 10
# #     t %= MOD
# #     d[r] += 1
# #     #print (r)
    
# print(sum(i*(i-1)//2 for i in d))

S = input()
MOD = 2019
cnt = [0]*MOD
cnt[0] = 1
for i in range(len(S)):
    s = int(S[i:])
    cnt[s%MOD]+=1
    #print (s,s%MOD)

print(sum(c*(c-1)//2 for c in cnt))

Submission status: Time Limit Exceeded

Solution code:
s = input()
 
MOD = 2019
d = [0]*MOD
d[0] = 1
r = 0
t = 1
for i in reversed(s):
    r += int(i)*t
    r %= MOD
    t *= 10
    #t %= MOD
    d[r] += 1
    #print (r)
    
print(sum(i*(i-1)//2 for i in d))

Submission status: Time Limit Exceeded

Solution code:

def solve():
    s=input()
    n=len(s)
    ans=0
    for i in range(n):
        for j in range(i+4,n+1):
            x=int(s[i:j])

            if x%2019==0:
                ans+=1
            # print(x,ans)
    print(ans)
solve()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(map(int, list(input())))
    m = [0]*2019
    num = 0
    ans = 0
    n = len(s)
    ten = [(10**i)%2019 for i in range(n)]
    for i in range(n):
        num += s[n-i-1]*ten[i]
        num %= 2019
        m[num] += 1 
    for mi in m:
        if mi:
            ans += (mi*(mi-1))//2
    ans += m[0]
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
res = 0
pre = 0
dp = {int(S[0]): 1}
for i in range(1, n):
    d = int(S[i])
    tdp = {d: 1}
    for j in dp:
        if dp[j] > 0:
            rem = (10 * j + d) % 2019
            if rem == 0:
                res += dp[j]
            if rem not in tdp:
                tdp[rem] = 0
            tdp[rem] += dp[j]
    dp = tdp

print(res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = list(map(int, list(input())))
    m = [0]*2019
    num = 0
    ans = 0
    for i, si in enumerate(s[::-1]):
        num += si*10**i
        num %= 2019
        m[num] += 1 
    for mi in m:
        if mi:
            ans += (mi*(mi-1))//2
    ans += m[0]
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input()))
MOD = 2019
cnt = 0
M = len(N)

candi = []
for i in range(0,M-4):
    for j in range(i+4,M+1):
        if sum(N[i:j]) % 3==0:
            ans = N[i]
            for a in N[i+1:j]:
                ans = (ans * 10 + a)%673
            if ans == 0:
                cnt += 1
                

print(cnt)

Submission status: Time Limit Exceeded

Solution code:


from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

S=list(map(int,list(input())[:-1]))[::-1]
#print(S)

smod=[0]*(len(S))
bef=0
b=1
for i in range(len(S)):
    bef+=(S[i]*(b))%2019
    smod[i]=bef%2019
    #smod.append(bef%2019)
    b*=10%2019
    
cnt=[0]*2020
for sm in smod:
    cnt[sm]+=1

cnt[0]+=1

res=0
for sc in cnt:
    res+=sc*(sc-1)/2
    
    
print(int(res))

#print(smod)

#print(cnt[:10])

Submission status: Time Limit Exceeded

Solution code:
S = [int(a) for a in input()[::-1]]
m = 2019
N = len(S)
s = 0
p = 1
X = [0] * 2019
X[0] = 1
ans = 0


for a in S:
    s = (s + a * p) % m
    ans += X[s]
    X[s] += 1
    p = p * 10
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = list(map(int, input()))
MOD = 2019
cnt = 0
M = len(N)
for i in range(0,M):
    n = N[i]
    for j in range(i+1,M):
        n = ((n * 10) + N[j])%MOD
        if n == 0:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:


from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

S=list(map(int,list(input())[:-1]))[::-1]
#print(S)

smod=[]
bef=0
for i in range(len(S)):
    bef+=(S[i]*(10**i))%2019
    smod.append(bef%2019)
    
cnt=[0]*2020
for sm in smod:
    cnt[sm]+=1

cnt[0]+=1

res=0
for sc in cnt:
    res+=sc*(sc-1)/2
    
    
print(int(res))

#print(smod)

#print(cnt[:10])

Submission status: Time Limit Exceeded

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline




def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())

directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

S=list(map(int,list(input())[:-1]))[::-1]
#print(S)

smod=[]
bef=0
for i in range(len(S)):
    bef+=(S[i]*(10**i))%2019
    smod.append(bef%2019)
    
cnt=[0]*2020
for sm in smod:
    cnt[sm]+=1

cnt[0]+=1

res=0
for sc in cnt:
    res+=sc*(sc-1)/2
    
    
print(int(res))

#print(smod)

#print(cnt[:10])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

def main():
  s = input().strip()
  x = remain = 0
  remains = [0] * 2019
  for i, d in enumerate(map(int, list(s[::-1]))):
    x = d * (10**i) + remain
    remain = x % 2019
    remains[remain] += 1

  print(sum([(remain * (remain - 1)) // 2 for remain in remains]) + remains[0])
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

from collections import (
  Counter, 
  defaultdict )

def main():
  s = input().strip()
  x = remain = 0
  remains = defaultdict(int)
  for i, d in enumerate(map(int, list(s[::-1]))):
    x = d * (10**i) + x
    remain = x % 2019
    remains[remain] += 1

  cnt = 0
  for k, v in remains.items():
    cnt += (v * (v - 1)) // 2
    if k == 0:
      cnt += v
  print(cnt)
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s = input()
n = len(s)
# 累積和の準備
sum_list = [0]*(n+1)  # s[0]は０であってほしいから配列数ひとつおおい
_10 = 1
for i in range(1, n + 1):
    sum_list[i] += (sum_list[i-1] + int(s[n-i])*_10) % 2019  # ここでmodしておく
    _10 *= 10
"""
(s[j]-s[i]/10**i)が区間の整数
区間の整数が2019で割り切れればよい
つまり、(s[j]-s[i]/10**i)≡0 (mod=2019)
ここで10**iと2019(素数)は互いに素なので、
10**iをかけて上の式はs[j]-s[i]≡0 (mod=2019)と同値
s[j]≡s[i] (mod=2019)となる。
"""
c = Counter(sum_list)
ans = 0

for key in c.keys():
    ans += c[key]*(c[key]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s = input()
n = len(s)
# 累積和の準備
sum_list = [0]*(n+1)  # s[0]は０であってほしいから配列数ひとつおおい
for i in range(1, n + 1):
    sum_list[i] += (sum_list[i-1] + int(s[n-i])*10**(i-1)) % 2019  # ここでmodしておく
"""
(s[j]-s[i]/10**i)が区間の整数
区間の整数が2019で割り切れればよい
つまり、(s[j]-s[i]/10**i)≡0 (mod=2019)
ここで10**iと2019(素数)は互いに素なので、
10**iをかけて上の式はs[j]-s[i]≡0 (mod=2019)と同値
s[j]≡s[i] (mod=2019)となる。
"""
c = Counter(sum_list)
ans = 0

for key in c.keys():
    ans += c[key]*(c[key]-1)//2
print(ans)