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 numpy import*
d=zeros(2019,int)
r=arange(2019)
a=0
for i in map(int,input()):
  p=zeros(2019,int)
  p[(r*10+i)%2019]+=d[r]
  d=p
  d[i]+=1
  a+=d[0]
print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys, math
from pprint import pprint
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

S = rs()

mods = []
n = 0
e = 0
r = 0
for i in reversed(range(len(S))):
	n = int(S[i:])
	r = 2019
	m = n % r
	mods.append(m)
mods.append(0)
import collections
cnt = collections.Counter(mods)
ans = 0
for k, c in cnt.items():
	ans += c*(c-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

s = input()
n = len(s)
mod = 2019
DP = [0 for i in range(n+2)]
DP[n+1] = 0

for i in reversed(range(n)):
    DP[i] = (DP[i+1] + int(s[i])*(10**(n-i-1)%mod))%mod
DP.pop(-1)

arr = defaultdict(int)
for dp in DP:
    arr[dp]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import Counter
def resolve():
    S = sr()
    N = len(S)
    a = [0]*(N+1)
    t = ''
    for i in range(N):
        t = S[-1-i]+t
        a[i] = int(t)%2019
    A = Counter(a)
    ans = 0
    for n in A.values():
        ans += n*(n-1)//2
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
mod=2019
n=1
A=[1]
for k in range(1, 400):
    n=n*10%mod
    if n in A:
        break
    else:
        A.append(n)
S=input()
count=0
mod2=len(A)
B=[0]
B.append(int(S[-1]))
if len(S)>3:
    for k in range(2,len(S)+1):
        B.append((B[-1]+int(S[-k])*A[(k-1)%mod2])%mod)
    for k in range(len(B)-1):
        for j in range(k+1,len(B)):
            if B[k] == B[j]:
                count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


def get_result(data):
    S = data[0]
    int_S = int(S)
    mod_all_bit = [0] * 2019
    mod_all_bit[0] = 1
    for i in range(len(S)):
        _, mod = divmod(int_S % (10**(i+1)), 2019)
        mod_all_bit[mod] += 1
    ans = 0
    for val in mod_all_bit:
        if val >= 2:
            ans += val * (val-1) * 0.5
    return int(ans)


if __name__ == '__main__':
    data = list(map(str, stdin.readline().rstrip().split(' ')))
    result = get_result(data)
    print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = list(input())
ls = len(s)
T = [0] * (ls+1)
modL = [0]
cnt = 0

for i in range(0, ls):
    j = ls-i-1
    T[j] = T[j+1] + int(s[j]) * 10**(i)
    modL.append(T[j] % 2019)

ans = 0
modC = Counter(modL)
for c in modC.values():
    ans += c * (c - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

S = input()

D = defaultdict(int)
D[0] += 1
x = 0
d = 1
for s in reversed(S):
  x = (x + int(s) * d) % 2019
  D[x] += 1
  d *= 10

print(sum(x * (x-1) // 2 for x in D.values()))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
N = len(S)
 
 
l = [0] * 2019
index = 0
temp = 0
for i in range(N-1, -1, -1):
    temp += int(S[i]) * 10**index
    l[temp%2019] += 1
    index += 1
 
ans = l[0]
 
for i in range(1, 2019):
    ans += l[i] * (l[i]-1) // 2
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans = 0
s = input()
for i in range(len(s) + 1):
    if (i + 4) > len(s):
        continue
    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 = str(input())
s=s[::-1]
mod = 2019
li = [0]*(len(s)+1)
cnt=1
for i in range(len(s)):
    li[i+1]=(li[i] + int(s[i]) * cnt) % mod
    cnt*=10
import collections
A=collections.Counter(li[1:])
ans=0
for u,v in A.items():
    ans+=v*(v-1)//2
    if u==0:
        ans+=v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ret = [0 for _ in range(2019)]
ret[0] = 1

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

ans = 0
for r in ret:
    if r > 1:
        ans += r * (r-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

s = input()
array = [0]*(len(s)+1)
mod = 2019

for i in range(len(s)):
    array[i] = int(s[i:len(s)]) % mod
    
array[len(s)] = 0

array.sort()

cnt = 0
w = 1

for i in range(0, len(array)-1):
    if array[i] == array[i+1]:
        w += 1
    else:
        if w > 2:
            cnt += factorial(w) / (factorial(2)*factorial(w - 2))
            w = 1
        elif w == 2:
            cnt += 1
            w = 1
            
if w != 1:
    cnt += factorial(w) / (factorial(2)*factorial(w - 2))
    
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
s = input()
checker = [False]*2019
checker[0]=True
res = 0
for i in range(1,len(s)+1):
    a = int(s[-i:]) % 2019
    if checker[a] == True:
        res += 1
    checker[a] = True
if int(s)%2019==0:
  	res+=1
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
s=input()
total=0
for i in range(0,len(s)-2):
  for j in range(i+4,len(s)+1):
    d=int(s[i:j])
    if d%2019==0:
      total+=1
print(total)

Submission status: Time Limit Exceeded

Solution code:
S = str(input())
sum = [0]
cnt = 0
for Si,SUMi in zip(S,sum):
    sum.append(int(Si)+int(SUMi))

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
mod = 2019
def main():
    s = tuple([int(x) for x in input().rstrip()])
    n = len(s)
    ans = 0
    d = {}
    T = 0
    T1 = 0
    for i in range(n):
        T1 = s[n-1-i]*10**i+T
        T = T1
        t = T%mod
        if t in d:
            d[t] += 1
        else:
            d[t] = 1
    for k in d:
        if k != 0:
            ans += d[k]*(d[k]-1)//2
        else:
            ans += d[k]
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

for i in range(l):
    if len(s[i:]) > 4:
        m = int(s[i:])%2019
        for j in range(i + 2, l):
            if j == l - 1:
                if m == 0:
                    cnt += 1
            elif m == int(s[j + 1:])%2019:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


def main():
    S = input().rstrip()

    N = len(S)

    M = [0 for i in range(2019)]

    for i in range(N):
        T = S[i:]
        T = int(T)
        M[T%2019] += 1

    M[0] += 1

    res = 0

    for m in M:
        res += m * (m-1) // 2


    print(res)



main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from collections import Counter

def resolve():
    s = input()

    mod_table = np.zeros(shape=len(s), dtype=int)
    for i in reversed(range(len(s) - 1)):
        mod_table[i] = int(s[i:]) % 2019

    count = Counter(mod_table)

    ans = 0
    for num_dup in count.values():
        if num_dup >= 2:
            ans += (num_dup * (num_dup - 1)) / 2

    print(int(ans))
    
resolve()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
S = str(input())

ans = 0

def test(S, ans):
    len_str = len(S)
    for i in range(len_str):
        before_m = int(S[i:i+3])
        for j in range(i + 4, len_str + 1):
            before_m = (before_m * 10 + int(S[j - 1])) % 2019
            if before_m == 0:
                ans += 1
    return ans

print(test(S, ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
mod_bucket = [0] * 2019
mod_bucket[0] += 1

for i in range(len(S)):
    mod = int(S[i:]) % 2019
    mod_bucket[mod] += 1

ans = 0
for mod in mod_bucket:
    ans += int(mod * (mod-1) / 2)
print(ans)

Submission status: Time Limit Exceeded

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

dp = [0]*num
temp = [0]*2019
for i in range(num):
    temp[int(s[i:]) % 2019] += 1

ans = temp[0]
for t in temp:
    if t > 1:
        ans += t*(t-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import math
import collections
S = str(input())
n = len(S)
mods = [int(S[i:])%2019 for i in range(n)][::-1]
c = collections.Counter(mods)
count = 0
for s in c.values():
    count += math.factorial(s) - 1
count += c[0]
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
B = [0]*2019
B[0]+=1

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

ans=0

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

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = input()
l = len(n)
s = np.empty(0, dtype="i8")
for i in range(l):
  s = np.append(s, int(n[i:]))
s = s%2019
t = [np.count_nonzero(s == i) for i in set(s)]

ans = sum([i*(i-1)/2 for i in t])
if 0 in set(s):
  ans += 1

print(int(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a=input()
list1=[]
for i in range(len(a)) :
  list1.append(int(a[i:])%2019)
count=0

list1.append(0)
import collections
c=collections.Counter(list1)
list2=c.values()
count=0
for item in list2 :
    count+=(int(item*(item-1)/2))


print(count)

Submission status: Time Limit Exceeded

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

for i in range(len(S)):
    T.append(int(S[-1-i:])%2019)

ret = [0 for _ in range(2019)]
for t in T:
    ret[t] += 1
ans = 0
for r in ret:
    if r > 1:
        ans += r * (r-1) // 2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
s = stdin.readline().rstrip()
n = len(s)
dic = {}
dic[0] = 1
now = 0
mul = 0
for i in range(n-1,-1,-1):
    now = (now+(10**mul)*int(s[i]))%2019
    if now in dic:
        dic[now] += 1
    else:
        dic[now] = 1
    mul += 1
print(sum(dic[i]*(dic[i]-1)//2 for i in dic))

Submission status: Time Limit Exceeded

Solution code:
s=input()
kyonen=2019
modlist=[]
for i in range(len(s)):
    modlist.append(int(s[i:len(s)])%kyonen)
modlist.sort()
now=modlist[0]
nowc=1
ans=0
if modlist[0]==0:
    ans+=1
for i in range(1,len(modlist)):
    if modlist[i]==0:
        ans+=1
    if modlist[i]==now:
        nowc+=1
    else:
        if nowc>=2:
            ans+=int(nowc*(nowc-1)/2)
        nowc=1
        now=modlist[i]

if nowc>=2:
    ans+=int(nowc*(nowc-1)/2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

s = input()
n = len(s)
mod = 2019
DP = [0 for i in range(n+2)]
DP[n+1] = 0

for i in reversed(range(n)):
    DP[i] = (DP[i+1] + int(s[i])*(10**(n-i-1)))%mod
DP.pop(-1)

arr = defaultdict(int)
for dp in DP:
    arr[dp]+=1

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

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

s = input()
array = [0]*(len(s)+1)
mod = 2019

for i in range(len(s)):
    array[i] = int(s[i:len(s)]) % mod
    
array[len(s)] = 0

array.sort()

cnt = 0
w = 1

for i in range(0, len(array)-1):
    if array[i] == array[i+1]:
        w += 1
    else:
        if w != 1:
            cnt += factorial(w) / (factorial(2)*factorial(w - 2))
            w = 1
            
if w != 1:
    cnt += factorial(w) / (factorial(2)*factorial(w - 2))
    
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
S=input() # 文字入力
length = len(S)
count=0
for i in range(length-3):
    for j in range(i+3, length) :
        num = int(S[i: j+1])
        if num%2019 == 0:
            count+=1
            j+=3
print(count)

Submission status: Time Limit Exceeded

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

import sys


def input():
    return sys.stdin.readline()


def resolve():
    s = input().rstrip()

    count = 0
    if len(s) < 5:
        if int(s) % 2019 == 0:
            print("1")
            exit()
        else:
            print("0")
            exit()

    one_digit_list = ["2", "4", "8"]
    two_digit_list = ["02", "04", "08", "16", "32", "64"]
    three_digit_list = ["002", "004", "008", "016", "032", "064", "129", "258", "516"]
    for i in range(0, 5):
        for j in range(4, len(s)):

            one_num = s[i]
            if j == 4:
                if one_num  in one_digit_list:
                    continue
            if j == 5:
                two_num = s[i:i+1]
                if two_num in two_digit_list:
                    continue
            if j == 6:
                three_num = s[i:i+2]
                if three_num in three_digit_list:
                    continue


            for k in range(0, len(s), j):
                start = i + k
                end = start + j

                if end > len(s):
                    continue

                # print(start, end)
                int_s = int(s[start:end])

                if int_s % 2019 == 0:
                    count += 1
    print(count)



if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
S = str(input())

ans = 0

def test(S, ans):
    len_str = len(S)
    for i in range(len_str):
        before_m = int(S[i:i+3])
        for j in range(i + 4, len_str + 1):
            temp_ans = (before_m * 10 + int(S[j - 1])) % 2019
            before_m = temp_ans
            if temp_ans == 0:
                ans += 1
    return ans

print(test(S, ans))

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
s=s[::-1]
mod = 2019
li = [int(s[0])]
cnt=10
for i in range(len(s) - 1):
    li.append((li[i] + int(s[i+1]) * cnt) % mod)
    cnt*=10
import collections
A=collections.Counter(li)
ans=0
for u,v in A.items():
    ans+=v*(v-1)//2
    if u==0:
        ans+=v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
l = len(s)
ans = 0
for i in range(l):
    for j in range(i + 1, l + 1):
        n = int(s[i:j])
        if sum(list(map(int, str(n)))) % 3 == 0:
            if int(s[i:j]) % 673 == 0:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
mod = 2019
def main():
    s = input().rstrip()
    n = len(s)
    ans = 0
    d = {}
    T = [0]*(n+1)
    for i in range(n):
        T[i+1] = int(s[n-1-i])*10**i+T[i]
        t = T[i+1]%mod
        if t in d:
            d[t] += 1
        else:
            d[t] = 1
    for k in d:
        if k != 0:
            ans += d[k]*(d[k]-1)//2
        else:
            ans += d[k]
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
from collections import Counter
def rem2019(d):
    return d%2019
def resolve():
    S = [s for s in sr()]
    N = len(S)
    a = ['']
    for i in range(N):
        s = S.pop()
        a.append(int(s+str(a[i])))
    a[0] = 0
    a = list(map(rem2019, a))
    A = Counter(a)
    ans = 0
    for n in A.values():
        ans += n*(n-1)//2
    print(ans)
resolve()

Submission status: Time Limit Exceeded

Solution code:
S=input()
s=int(S)
count=0
for i in range(6,1+(s//2019)):
    a=str(2019*i)
    if "0" in a:
        pass
    else:
        count+=S.count(a)
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
table = [int(s[i:]) % 2019 for i in range(n)]+[0]
count = [0 for _ in range(2019)]
ans = 0
for t in table:
    if count[t] != 0:
        ans += count[t]
    count[t] += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
arr = [0 for i in range(2019)]
arr[0] = 1

s = input()
n = len(s)

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

ans = 0
for m in arr:
    if m==0: continue
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = str(input())
N = len(S)

smod = [int(S[i:])%2019 for i in range(N)]
smod.append(0)

cmod = Counter(smod)

ans = 0
for i in cmod.values():
    ans += i*(i-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
S = input()
A = []
B = [0]*2019
B[0]+=1

for i in range(len(S)):
    A.append(int(S[-i-1:])%2019)
    B[int(S[-i-1:])%2019]+=1

ans=0

for j in range(len(B)):
    ans+=(B[j]-1)*B[j]/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
a=input()
list1=[]
for i in range(len(a)) :
  list1.append(int(a[i:])%2019)
count=0

list1.append(0)
for i in range(len(list1)):
  for j in range(i+1,len(list1)) :
    if list1[i]==list1[j] :
      count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()
num = len(s)

dp = [0]*num
temp = [0]*2019
for i in range(num):
    temp[int(s[i:]) % 2019] += 1

ans = temp[0]
for t in temp:
    if t > 1:
        ans += t*(t-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
l = len(s)
ans = 0
for i in range(l):
    for j in range(i + 1, l + 1):
        if int(s[i:j]) % 2019 == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
s=s[::-1]
mod = 2019
li = [int(s[0])]
for i in range(len(s) - 1):
    li.append((li[i] + int(s[i+1]) * (10 ** (i + 1))) % mod)
import collections
A=collections.Counter(li)
ans=0
for u,v in A.items():
    ans+=v*(v-1)//2
    if u==0:
        ans+=v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=[0 for i in range(2019)]
l[0]+=1
r=0
for i in range(len(s)):
  a=int(s[i:])%2019
  l[a]+=1
for i in range(len(l)):
  r+=l[i]*(l[i]-1)/2
print(int(r))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = list(input())
ls = len(s)
T = [0] * (ls+1)
modL = [0]
cnt = 0

for i in reversed(range(0, ls)):
    T[i] = T[i+1] + int(s[i]) * 10**(ls-i-1)
    modL.append(T[i] % 2019)

ans = 0
for c in Counter(modL).values():
    ans += c * (c - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
    M = 2019
    dp = [0] * M
    s = input()
    x = 0
    for c in s:
        c = int(c)
        dp2 = [0] * M
        for j in range(M):
            index = (j*10 + c) % M
            dp2[index] += dp[j]
        dp = dp2
        dp[c] += 1
        x += dp[0]
    print(x)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    import time
    from collections import defaultdict
    dic = defaultdict(int)

    input = sys.stdin.readline

    s = str(input().rstrip())
    m = 0

    for i in range(1,len(s)+1):
        m_ad = (int(s[-i]) * (10 ** (i - 1))) % 2019
        m = (m_ad + m) % 2019
        dic[m] += 1

    ans1 = 0

    for x in dic.values():
        if x > 1:
            ans1 += x * (x - 1) // 2
    ans2 = dic[0]
    time.sleep(0.5)

    print(ans1 + ans2)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
mod = 2019
def main():
    s = input().rstrip()
    n = len(s)
    ans = 0
    d = {}
    for i in range(n):
        t = int(s[i:])%mod
        if t in d:
            d[t] += 1
        else:
            d[t] = 1
    for k in d:
        if k != 0:
            ans += d[k]*(d[k]-1)//2
        else:
            ans += d[k]
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = []
c = 0
for i in range(len(s)):
    b = int(s[- 1 - i:]) % 2019
    if b == 0:
        c += 1
    else:
        a.append(b)
b = set(a)
print(len(a) - len(b) + c)

Submission status: Time Limit Exceeded

Solution code:
S=input()
mem_mod = [0 for i in range(0,2019,1)]
N = len(S)
counter = 0
S_num_tmp = list(S)
S_num = list(map(int, S_num_tmp))
tmp=10
for i in range(1,N,1):
    S_num[N-i-1] = S_num[N-i] + (tmp)*S_num[N-i-1]
    tmp=tmp*10
for i in range(0,N,1):
    X = S_num[N-i-1] % 2019
    if X==0:
        counter+=1
    counter += mem_mod[X]
    mem_mod[X] += 1
print(counter)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
S = str(input())

ans = 0

def test(S, ans):
    len_str = len(S)
    for i in range(len_str):
        for j in range(i + 4, len_str + 1):
            if j == i + 4:
                temp_ans = int(S[i:j]) % 2019
            else:
                temp_ans = (before_m * 10 + int(S[j - 1])) % 2019
            before_m = temp_ans
            if temp_ans == 0:
                ans += 1
    return ans

print(test(S, ans))

Submission status: Time Limit Exceeded

Solution code:
S=input()
s=int(S)
count=0
for i in range(1,1+(s//2019)):
    a=str(2019*i)
    if "0" in a:
        pass
    else:
        count+=S.count(a)
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
S_len = len(S)  # <= 200000
ans = 0
dp = [0] * 2019
dp[0] = 1
val = ''
for s in S:
    val = s + val
    dp[int(val) % 2019] += 1
for i in dp:
    ans += i * (i-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s=input()
l=len(s)
if l<=4:
    print(0)
else:
    a=[]
    for i in range(l):
        a.append(int(s[i:l])%2019)
    count=0
    acount=Counter(a)
    p=acount[0]
    count+=p
    for j in acount.values():
        count+=1/2*j*(j-1)
    print(int(count))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from collections import defaultdict
    dic = defaultdict(int)

    input = sys.stdin.readline

    s = str(input().rstrip())
    m = 0

    for i in range(1,len(s)+1):
        m_ad = (int(s[-i]) * (10 ** (i - 1))) % 2019
        m = (m_ad + m) % 2019
        dic[m] += 1

    ans1 = 0

    for x in dic.values():
        if x > 1:
            ans1 += x * (x - 1) // 2
    ans2 = dic[0]

    print(ans1 + ans2)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
a=0
for i in range(len(S)-1):
    for j in range(i+1,len(S)):
        s=''.join(S[i:j+1])
        num=int(s)
        if num%2019==0:
            a+=1
print(a)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
ans=0
p=0
a={}
b=-1
c=-1
d=-1
for j in range(l)[::-1]:
    p=(p+int(s[j])*10**(l-j-1))%2019
    if b>0:
        if a.get(b)!=None:
            a[b]+=1
        else:
            a[b]=1
    b=c
    c=d
    d=p
    if p==0:
        ans+=1
    if a.get(p)!=None:
        ans+=a[p]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
arr = [0 for i in range(2019)]
arr[0] = 1

s = input()
n = len(s)

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

ans = 0
for i,m in enumerate(arr):
    if m==0: continue
    ans += m*(m-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = list(input())
ls = len(s)
T = [0] * (ls+1)
modL = [0]
cnt = 0

for i in range(0, ls):
    T[i] = int("".join(s[i:ls]))
    modL.append(T[i] % 2019)

ans = 0
for c in Counter(modL).values():
    ans += c * (c - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
S = str(input())
n = len(S)
mods = [int(S[i:])%2019 for i in range(n)][::-1]
count = sum([0 == i for i in mods])
count += math.factorial(sum([0 == i for i in mods])) -1
modss = list(set(mods) - set([0]))
for s in modss:
    count += math.factorial(sum([s == i for i in mods])) -1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
S_len = len(S)  # <= 200000
ans = 0
dp = [0] * 2019
dp[0] = 1
for i in range(1, S_len+1):
    dp[int(S[:i][::-1]) % 2019] += 1
for i in dp:
    ans += i * (i-1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
count =0
mark =[]
ct=0
st =0
end =1
while st < len(S)-1:
    while end < len(S)+1:
        T=S[st:end]
    #print(T)
    #print(''.join(T))
        tmp =''.join(T)
        n = int(tmp)
        if n % 2019 == 0:
            count +=1
            end +=1
        else:
            end +=1
    st +=1
    end = st+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

lst = [0 for i in range(2019)]
lst[0] = 1

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main(S=None):
    if not S:
        S = input()
    r = [0]*2019
    r[0] = 1
    tmp = 0
    base = 1
    for s in S[::-1]:
        s = int(s) * base
        base *= 10
        tmp += s
        tmp %= 2019
        r[tmp] += 1
    count = 0
    for v in r:
        count += v*(v-1)//2
    print(count)
    return count


if __name__ == "__main__":
    # assert main("1817181712114") == 3
    # assert main("14282668646") == 2
    # assert main("2119") == 0
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
mod = 2019
arr = {0:1}

num = 0
for i in reversed(range(n)):
    num = (num + int(s[i])*(10**(n-i-1)))%mod
    # print(num)
    if num in arr:
        arr[num]+=1
    else:
        arr[num]=1
# print(arr)
ans = 0
for i in arr.values():
    ans += i*(i-1)//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

s = input()
count = 0

lst = []
for i in range(len(s)):
    lst.append(int(s[(-i- 1):])%2019)
count += lst.count(0)
a = [x for x in set(lst) if lst.count(x) > 1]
for j in a:
    b = lst.count(j)
    c = math.factorial(b)/math.factorial(2)/math.factorial(b-2)
    count += c

print(int(count))

Submission status: Time Limit Exceeded

Solution code:
s=list(input())
s=[int(i) for i in s]
last_mod=0
from collections import defaultdict
c=defaultdict(int)
c[0]+=1
for i in range(len(s)):
    now=(s[-1-i]*(10**i))%2019
    c[(last_mod+now)%2019]+=1
    last_mod=(last_mod+now)%2019
ans=0
for k in c.keys():
    ans+=c[k]*(c[k]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def abc():

 s=input()
 sum=0
 for i in range(len(s)):
   s1=""
   for j in range(i+1,len(s)+1):
    s1=s[i:j]
    if(int(s1)%2019==0):
        sum+=1
 print(sum)
abc()

Submission status: Time Limit Exceeded

Solution code:
import collections
S = input()

a = []
s = int(S[-1])
for i in range(2,len(S)+1):
    s += int(S[-i])*(10**(i-1))
    #print(s)
    a.append(s%2019)

A = collections.Counter(a)
#print(A)

ans = 0
for i in A.values():
    if i>=2:
        ans += int(i*(i-1)/2)

ans += A[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:

def main():
    M = 2019
    s = input()

    x = 0
    dp = [0] * M
    for c in s:
        c = int(c)
        dp2 = [0] * M
        for j in range(M):
            dp2[(j*10 + c) % M] += dp[j]
        dp2[c] += 1
        dp = dp2
        x += dp[0]
    print(x)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = []
c = 0
for i in range(len(s)):
    b = int(s[- 1 - i:]) % 2019
    if b == 0 or b in a:
        c += 1
    else:
        a.append(b)
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = str(input())
N = len(S)
 
smod = [int(S[i:])%2019 for i in range(N)]
cmod = Counter(smod)
 
ans = 0
for x,y in cmod.items():
    if x == 0:
        y += 1
        ans += y*(y-1)/2
    else:
        ans += y*(y-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

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

l = [0] * 2019
for i in range(N):
    l[int(S[i:N])%2019] += 1

ans = l[0]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main(S=None):
    if not S:
        S = input()
    r = {k: 0 for k in range(0, 2019)}
    r[0] = 1
    tmp = 0
    base = 1
    for s in S[::-1]:
        s = int(s) * base
        base *= 10
        tmp += s
        tmp %= 2019
        r[tmp] += 1
    count = 0
    for k, v in r.items():
        # if v > 1:
        #     print(k, v)
        count += v*(v-1)//2
    print(count)
    return count


if __name__ == "__main__":
    # assert main("1817181712114") == 3
    # assert main("14282668646") == 2
    # assert main("2119") == 0
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = len(S)
noCount = 0
fourDigi = {'2019', '4038', '6057', '8076'}
fiveDigi = {'10095', '12114', '14133', '16152', '18171', '20190', '22209', '24228', '26247', '28266', '30285',
              '32304', '34323', '36342','38361', '40380', '42399', '44418', '46437', '48456', '50475', '52494',
              '54513', '56532', '58551', '60570', '62589', '64608', '66627', '68646', '70665', '72684', '74703',
              '76722', '78741', '80760', '82779', '84798', '86817', '88836', '90855', '92874', '94893', '96912', '98931'}
fiveD2nd = {'0','2','4','6','8'}
sixDigi = {'100950', '102969', '104988', '107007', '109026', '111045', '113064', '115083', '117102', '119121',
             '121140', '123159', '125178', '127197', '129216', '131235', '133254', '135273', '137292', '139311',
             '141330', '143349', '145368', '147387', '149406', '151425', '153444', '155463', '157482', '159501',
             '161520', '163539', '165558', '167577', '169596', '171615', '173634', '175653', '177672', '179691',
             '181710', '183729', '185748', '187767', '189786', '191805', '193824', '195843', '197862', '199881'}
for j in range(1,l):
    for k in range(l - j + 1):
        segment = int(S[k:k + j])
        if segment % 2019 == 0:
            noCount += 1
 
print(noCount)