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=input()
length = len(s)
count=0
for i in range(length):
  for j in range(i+4, length):
    num = int(s[i:j+1])
    if num%2019 == 0:
      count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from functools import reduce
l = list(input())
l_int = [int(s) for s in l]
index = len(l)
ln = []
answer_ = 0
for i in range(0, index, 1):
    ln.append(i)

for v in itertools.permutations(ln, 2):
    v_0 = int(v[0])
    v_1 = int(v[1])
    if v_0 <= v_1:
        number_list = l_int[v[0]:v[1]+1]
        n = reduce(lambda a, b: 10*a+b, number_list)  # リストの文字を連結する。
        if n % 2019 == 0:
            answer_ += 1
print(answer_)

Submission status: Time Limit Exceeded

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

print(count)

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
    T = [int(s[k:])%mod for k in range(n)]
    d = {}
    for t in T:
        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()

i = 0
count = 0
j = 2
#print(int(s[i:j]))
while i < len(s)-2:
    j = i + 2
    while  j <= len(s):
#        print(s[i:j])
        amari = int(s[i:j]) % 2019
        if  amari == 0:
            count += 1
        j += 1    
    i +=1    
#        j = j + 1 
print(count)

Submission status: Time Limit Exceeded

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

S = int(input())
StrS = str(S)
S_Surpus = S % 2019
LenS = len(str(S))
ListSurpus = []


for i in range(LenS):
    Surpus = (int(StrS[i]) * (10 ** (LenS - i - 1) )) % 2019
    ListSurpus.append(Surpus)

#print(ListSurpus)

ans = 0

for i in range(LenS):
    sum = ListSurpus[i]
    for j in range(i+1, LenS):
        sum += ListSurpus[j]
        if(sum % 2019 == 0):
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#D

#デバッグ
debug=0
def pront(*n):
    if debug==1:
        print(n)
        
#10**9+7を底とした処理

base=10**9+7

def add(a,b):#a+b
    return (a+b)%base

def sub(a,b):#a-b
    return (a-b)%base

def multi(a,b):#a*b
    return (a*b)%base

def exp(a,b):#a**b
    x=1
    c=a
    d=b
    while d!=0:
        if d%2==1:
            x=multi(x,c)
        c=(c**2)%base
        d=d//2
    return x

def div(a,b):#a/b
    return(multi(a,exp(b,base-2)))

#入力

S=input()

#処理
import math
L=len(S)
x=[0]
for i in range(L):
    x.append(((x[-1])+int(S[-i-1])*10**i)%2019)
xx={}
pront(x)
for i in x:
    if i in xx:
        xx[i]+=1
    else:
        xx[i]=1
y=0
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
for i,j in xx.items():
    if j>1:
        pront(i,j)
        y+=combinations_count(j, 2)
print(y)

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
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = len(s)
res = []

def dfs(s, i, j):
    if j - i <= 3:
        return 0
    if int(s[i:j]) % 2019 == 0 and (i, j) not in res:
        res.append((i, j))
        return 1
    return dfs(s, i + 1, j) + dfs(s, i, j - 1)

print(dfs(s, 0, len(s)))

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
n = len(S)
res = 0
cur = 0
count = [0] * 2020
count[0] = 1
for i, c in enumerate(map(int, S)):
    cur = cur * 10 + c
    temp = (cur * pow(10, (n - i), 2019)) % 2019
    count[temp] += 1
print(sum(c * (c - 1) // 2 for c in count))

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):
    d[int(s[-i:])%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()
cnt = 0
for i in range(0,len(s)-3):
  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 numpy as np
s = input()
l = np.array([int(s[i:j]) for i in range(0,len(s)-3) for j in range(i+4,len(s)+1)])
print(np.count_nonzero(l % 2019==0))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
long = len(S)
ans = 0
modlist = [0] * (long + 1)
mod = 0
for i in reversed(range(long)):
    mod = (mod + pow(10,long - i - 1) * int(S[i])) % 2019
    modlist[i] = mod
c = Counter(modlist)
for i in c.values(): ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()

if len(S) < 5:
    print(0)
    exit()

check_str = deque()
first = 0
end = 4

for i_range in range(5, len(S)):
    start = 0
    while start+i_range <= len(S):
        check_str.append(int(S[start:start+i_range]))
        start += 1

ans = 0
while len(check_str) > 0:
    now_num = check_str.popleft()
    if now_num % 2019 == 0:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()
l = np.array([int(s[i:j]) for i in range(0,len(s)-3) for j in range(i+4,len(s)+1)])
p = l % 2019
print(np.count_nonzero(p==0))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

S = input()
size_S = len(S)

initial = np.arange(0, size_S-3, 1)
last = np.arange(2, size_S, 1)


count = 0
for i in range(len(initial)):
  last = np.delete(last, 0, 0)
  for j in range(len(last)):
    number = int(S[initial[i]:last[j]+1])
    if number % 2019 == 0:
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = int(input())
P = 2019

N = len(str(S))

li = [(S % pow(10, (N - i))) for i in range(N+1)]
li2 = [0 for i in range(P)]

for i in li:
  li2[i % P] += 1

t = 0
for i in li2:
  if i == 0 or i == 1:
    pass
  else:
    t += i * (i -1) // 2

print(t)

Submission status: Time Limit Exceeded

Solution code:
S = input()

n = len(S)

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

print(t)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
s = input()
l = np.array([int(s[i:j]) for i in range(0,len(s)-3) for j in range(i+4,len(s)+1)])
p = l % 2019
print(np.count_nonzero(p % 2019 ==0))

Submission status: Time Limit Exceeded

Solution code:
import itertools

S = input()
cnt = 0
ln = len(S)

ls = [i for i in range(len(S))]

com = list(itertools.combinations(ls, 2))

for i in com:
    #print(i[0])
    start = int(i[0])
    end = int(i[1])
    #print(start, end)
    if int(S[start:end+1]) % 2019 == 0:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s=input()
l=[]
l.insert(0,0)
for i in range(1,len(s)+1):
    l.append(int(s[-i:])%2019)
c=Counter(l)
ans=0
for v in c.values():
    ans += v*(v-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline
from functools import reduce

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
    S_int = II()
    S = str(S_int)
    #Slist = [int(x) for x in list(str(S))]
    #N = len(Slist)
    ans = 0
    max = S_int//2019
    a = [str(2019*xx) for xx in range(1,max)]
    ii = 0
    
    #print(a)
    #print(N)

    for i in a:
        if i in S:
            ans += 1
            S2 = S[S.find(i)+1:]
            #print("S2",S2)
            while True:
                if i in S2:
                    ans += 1
                    S2 = S2[S2.find(i)+1:]
                    #print("S3",S2)
                else:
                    break

    print(ans)



if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
lens=len(s)
count=0
if(lens<4):
    print(0)
    exit()
for i in range(lens+1):
    for k in range(i,lens+1):
        num=s[i:k]
        if(len(num)>=4):
            if(int(num)%2019==0):
                count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = tuple(input())
    m = 2019
    div_list = [0]*m
    num = 0
    for i, l in enumerate(reversed(S)):
        num += pow(10, i)*int(l)
        div_list[num%m] += 1

    div_list[0] += 1
    ans = 0
    for item in div_list:
        ans += int(item*(item-1)/2)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    result = 0
    L = len(S)
    for i in range(L-3):
        work = int(S[i:i+3])
        j = i + 3
        for _ in range(10**6):
            if j >= L:
                break
            work = work * 10 + int(S[j])
            if work % 2019 == 0:
                result += 1
            j += 1
    print(result)
main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
ans = []
for i in range(0, len(s) - 3):
    j = i
    num = ''
    while j < len(s):
        juhuku = 0
        num += s[j]
        if int(num) % 2019 == 0 and num != '0':
            #print(i, j)
            if j not in ans:
                cnt += 1
            else:
                cnt += 2
        j += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
long = len(S)
ans = 0
modlist = [0]
mod = 0
for i in reversed(range(long)):
    mod = (mod + pow(10,long - i - 1) * int(S[i])) % 2019
    modlist.append(mod)
c = Counter(modlist)
for i in c.values(): ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#


def main():
    s = input()
    length = len(s)
    count = 0
    # 愚直な方法ではTLE
    # for i in range(length - 3):
    #     for j in range(i + 4, length + 1):
    #         n = int(s[i:j])
    #         if n % 2019 == 0:
    #             count += 1
    for j in range(length, 4, -1):
        i = j - 4
        t = 3
        while i >= 0:
            n = int(s[i:j])
            if n % 2019 == 0:
                for k in range(i, -1, -4):
                    n = int(s[k:j])
                    if n % 2019 == 0:
                        count += 1
                break
            else:
                # ここ3回探索したら十分じゃね？
                i -= 1
                t -= 1
                if t < 0:
                    break
    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()

i = 0
j = 0
count = 0
while i < len(s):
    j = i + 1
    while j <= len(s):
        if int(s[i:j]) % 2019 == 0:
            count += 1
        j += 1
    i += 1

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 26 22:28:26 2020

@author: Kanaru Sato
"""

s = input()
n = len(s)
s = int(s)
dic = {}
mod = 2019
count = 0

for i in range(1,n+1):
    num = s%(10**i)
    num %= mod
    if num in dic:
        count += 1
    if num not in dic:
        dic[num] = 1

if 0 in dic:
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = input()
l = len(n)
s = np.empty(0)
for i in range(l-3):
  for j in range(i+4, l+1):
    s = np.append(s, int(n[i:j]))
print(np.count_nonzero(s%2019==0))

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
ans = []
for i in range(0, len(s) - 3):
    j = i
    num = ''
    while j < len(s):
        juhuku = 0
        num += s[j]
        if int(num) % 2019 == 0 and num != '0':
            #print(i, j)
            if j not in ans:
                ans.append(j)
            juhuku = ans.count(i - 1)
            cnt += 1 + juhuku
        j += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=list(map(int,input()))
n=len(s)
bf=[[0],[],[]]
now=[[],[],[]]
bf[0]=[0]
lst=[]
for i in range(n):
	if s[i]%3==0:
		now[0]=[j*10+s[i] for j in bf[0]]
		now[1]=[j*10+s[i] for j in bf[1]]
		now[2]=[j*10+s[i] for j in bf[2]]
	if s[i]%3==1:
		now[0]=[j*10+s[i] for j in bf[2]]
		now[1]=[j*10+s[i] for j in bf[0]]
		now[2]=[j*10+s[i] for j in bf[1]]
	if s[i]%3==2:
		now[0]=[j*10+s[i] for j in bf[1]]
		now[1]=[j*10+s[i] for j in bf[2]]
		now[2]=[j*10+s[i] for j in bf[0]]
	if i!=0:
		now[s[i]%3].append(s[i])
	lst.extend(now[0])
	bf=now.copy()
cnt=0
for i in lst:
	if i%2019==0:
		cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ls=len(s)
# 例外処理
if ls<4:
    print(0)
else:
    ans=0
    import numpy as np
    for i in range(ls-3):
        amari=np.array([int(s[i:j])%2019 for j in range(i+4,ls+1)])
        ans+=np.count_nonzero(amari==0)
    
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


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


def IF(): return float(input())


def LS(): return list(map(str, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    S, W = LI()
    if S>W:
        print('safe')
    else:
        print('unsafe')

    
    return

# B


def B():
    A,B,C,D=LI()
    while(1):
        C=C-B
        if C<=0:
            print('Yes')
            break
        A=A-D
        if A<=0:
            print('No')
            break



    return

# C


def C():
    N=II()
    S=['i']*N
    for i in range(N):
        S[i]=input().rstrip()
    print(len(set(S)))

    return


def D():
    S=input().rstrip()
    ans=0
    for i in range(len(S)-3):
        j=i+3
        while(1):
            if int(S[i:j+1])%2019 == 0:
                #print(i,j)
                ans+=1
                j+=3
            j+=1
            if j>len(S):
                break
    print(ans)
    return

# E


def E():
    N, K = LI()
    A = LI()
    F = LI()
    A.sort(reverse=True)
    F.sort()
    cost = []
    for i in range(len(N)):
        cost.append(A[i]*F[i])

    a = F[cost.index(max(cost))]

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    N = len(S)

    pass


# Solve
if __name__ == '__main__':

    D()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
s = input()
deq=deque()
deq.append(s)
count=0
keta=len(s)+1
while len(deq):
    a=deq.popleft()
    if keta>len(a) and len(a)>4:
        deq.append(a[0:-1])
        #print("del")
        keta-=1
    if len(a)>4:
        deq.append(a[1:-1]+a[-1])
    if int(a)%2019==0:
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
    
def abs(num):
    if num < 0:
        return -num
    else:
        return num

###
# main
if(__name__ == '__main__'):
    # input
    s = input()
    len_s = len(s)

    ans = 0
    if len_s >= 4:
        dic = {}
        for i in range(2019):
            dic[i] = 0

        seed = int(s[0:4]) % 2019
        dic[seed] = 1

        for s_i in range(len_s-3):
            ans += dic[0]

            if s_i == len_s-4:
                break

            tgt = int(s[s_i+4])

            new_dic = {}
            for i in range(2019):
                new_i = (i*10 + tgt) % 2019
                new_dic[new_i] = dic[i]
            
            seed = int(s[s_i+1:s_i+5]) % 2019
            new_dic[seed] += 1

            dic = new_dic

    print(ans)
    
# else:
    # do nothing

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 26 22:28:26 2020

@author: Kanaru Sato
"""

s = input()
n = len(s)
s = int(s)
dic = {}
mod = 2019
count = 0

for i in range(1,n+1):
    num = (s%(10**(i)))%mod
    if num in dic:
        count += 1
    if num not in dic:
        dic[num] = 1

if 0 in dic:
    count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = list(map(int, list(input())))
N = len(S)

dp = [[0]*N for i in range(N)]
if N < 4:
    print(0)
    exit(0)

D = [[0]*N for i in range(9)]
for i in range(9):
    D[i][0] = (i+1)
    for j in range(1, N):
        D[i][j] = D[i][j-1]*10 % 2019
ans = 0

dp[0] = S[0]
for i in range(1, N):
    dp[i] = (dp[i-1]*10 + S[i]) % 2019
    if dp[i] == 0:
        ans += 1

for i in range(1, N):
    for j in range(i, N):
        dp[j] -= D[S[i-1]-1][j - i + 1]
        dp[j] %= 2019
        if dp[j] == 0:
            ans += 1


# print(N)
# print(*dp, sep='\n')
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import re

def check(target):
#  print(target)
  return target > 0 and target % 2019 == 0

N = input()
L = len(N)
#print(N)
#print(L)
  
if L > 3:
  count = 0
  while len(N) > 3:
    for i in range(4, len(N)+1):
      if (check(int(N[:i]))):
        count += 1
    N = N[1:]
  
  print(count)
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
s = input()
cnt = 0
ans = []
for i in range(0, len(s) - 3):
    j = i
    num = ''
    while j < len(s):
        juhuku = 0
        num += s[j]
        if int(num) % 2019 == 0 and num != '0':
            #print(i, j)
            if j not in ans:
                ans.append(j)
            juhuku = ans.count(i - 1)
            cnt += 1 + juhuku
        j += 1
print(cnt)

Submission status: Time Limit Exceeded

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

def ModPow(N, R, M):
  if R == 0: return 1
  if R%2 == 0: ModPow(N, R//2, M)**2 % M
  return N * ModPow(N, R-1, M)

dp = []
dic = { 0: 1 }
ans = 0
for i in range(L):
  x = int(S[L-i-1])
  dp.append(x if i == 0 else ((x*ModPow(10, i, 2019))%2019 + dp[i-1])%2019)
  if dp[i] in dic:
    ans += dic[dp[i]]
    dic[dp[i]] += 1
  else: dic[dp[i]] = 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
n = len(S)
res = 0
cur = 0
count = [0] * 2020
for i, c in enumerate(map(int, S)):
    cur = cur * 10 + c
    if cur % 2019 == 0:
        res += 1
    temp = (cur * pow(10, (n - i), 2019)) % 2019
    res += count[temp]
    count[temp] += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
 
answer = 0
 
Li = len(S)
for i in range(Li):
  Lj = Li - i
  for j in range(Lj):
    Sij = S[i:i+j+1]
    Sij_int = int("".join(map(str,Sij)))
    if Sij_int % 2019 == 0:
      answer += 1
 
print(answer)

Submission status: Time Limit Exceeded

Solution code:
#d
N = str(input())
count=0
for i in range(3,len(N)+1):
    for x in range(len(N)-i+1):
        if int(N[x:x+i])%2019==0:
            count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
a = 0
for i in range(len(s)):
    last = None
    for j in range(i + 1, len(s) + 1):
        if last is not None:
            now = last + s[j - 1]
            if int(now) % 3 != 0: continue
        if int(s[i:j]) % 2019 == 0:
            a += 1
            last = s[i:j]

print(a)

Submission status: Time Limit Exceeded

Solution code:
from math import log10

def g(s):
    ans = 0
    n = int(log10(s)) + 1
    while s > 2019:
        if s % 2019 == 0:
            ans += 1
        n -= 1
        s %= (10**n)
    return ans

def f(s):
    ans = 0
    while s > 2019:
        ans += g(s)
        s //= 10
    return ans

s = int(input().strip())

print(f(s))

Submission status: Time Limit Exceeded

Solution code:
s=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:
#import numpy as np#not for pypy

def main():
    ##get int
    #first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    first_input = input().split(" ")
    #second_input = input().split(" ")

    S = first_input[0]

    mod_2019 = [0]*2019
    for i in range(len(S)+1):
        if i==len(S):
            mod_2019[0] = mod_2019[0]+1
        else:
            mod = int(S[i:])%2019
            mod_2019[mod] = mod_2019[mod]+1

    count = 0
    for i in range(2019):
        Cn = mod_2019[i]
        count = count+Cn*(Cn-1)/2

    print(int(count))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
if len(S) < 4:
    print(0)
    exit(0)
dp = [0]*2019
dp[0] = 1
s = int(S[-1])
dp[s] = 1
ans = 0
for k in range(len(S)-2,-1,-1):
    s = int(S[k])*(10**(len(S)-k-1)) + s
    s %= 2019
    if dp[s] > 0:
        ans += 1
    else:
        dp[s] = 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
S = input()
N = int(len(S))
cnt = 0
for i in range(N,-1,-1):
	for j in range(4,N+1,1):
		str = S[i-j:i]
		if(str != ""):
			num = int(str)
			if(num % 2019 == 0):
				#print(num)
				#print(i)
				#print(j)
				cnt = cnt + 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = len(s)
l = []
from collections import deque
l = deque(l)   # deque オブジェクトを取得

for _ in range(1, 20190000):
    if _%2019 == 0 and '0' not in str(_) :
        l.append(str(_))
l = set(l)
#print(l)

def aaa(s, num):
    x = 0
    for i in range(4,num+1):
        for j in range(num - i + 1):
            if s[j:j+i] in l:
                x += 1
    print(x)

aaa(s, num)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
	S = input()
	LEN = len(S)
	r = LEN
	l = LEN-1
	count = 0
	lst = []
	amari = 0
	while(l >= 0):
		amari = (amari + int(S[l])*10**(r-l-1)%2019)%2019
		
		if amari in lst:
			count += 1
		elif amari == 0:
			count += 1
			lst.append(count)
		else:
			lst.append(amari)
		l -= 1

	print(count)

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np#not for pypy

def main():
    ##get int
    #first_input = list(map(lambda x:int(x), input().split(" ")))
    #second_input = list(map(lambda x:int(x), input().split(" ")))

    ##get float
    #first_input = list(map(lambda x:float(x), input().split(" ")))
    #second_input = list(map(lambda x:float(x), input().split(" ")))

    ##get string
    first_input = input().split(" ")
    #second_input = input().split(" ")

    S = first_input[0]

    mod_2019 = [0]*2019
    for i in range(len(S)+1):
        if i==len(S):
            mod_2019[0] = mod_2019[0]+1
        else:
            mod_2019[int(S[i:])%2019] = mod_2019[int(S[i:])%2019]+1

    count = 0
    for i in range(2019):
        Cn = mod_2019[i]
        count = count+Cn*(Cn-1)/2

    print(int(count))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = str(input())


def solution(S):
    a = len(S)
    b = list(0 for j in range(4, a+1)
        for i in range(a-j+1)
            if int(S[i:i+j])%2019==0)
    print(len(b))
solution(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()
answer = 0
for i in range(int(S)//2019 + 1):
    f = 2019 * i
    if str(f) in S:
        answer += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

count = 0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()
ss=len(s)
res=0
target=2019

l=r=0
ff=False

for i in range(ss-4):
    l=i
    if ff : l =r-2
    if l > ss-4:break

    ff=False
    for j in range(l+4,ss+1):
        r=j
        # print('aaaa',l,r,s[l:r])

        tmp=int(s[l:r])
        # print(tmp)
        if ff == True and s[r] != '0':break


        if tmp % target ==0:
            # print('xxxx',l,r,tmp)
            # print('xxxx',tmp)
            ff=True
            res+=1

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
from functools import lru_cache
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))
def inpl_str():
    '''
    一行に複数の文字
    '''
    return list(input().split())

@lru_cache(maxsize=None)
def temp(n):
    return 10**i%2019

s = list(map(int,list(input())[:-1]))
n=len(s)
right = [0] * (n + 1)
for i in range(n):
    right[-i - 2] = (right[-i - 1] + s[-i-1] * temp(i)) % 2019
log = defaultdict(lambda: 0)
ans=0
for i in range(n + 1):
    log[right[-i - 1]] += 1
    value = log[right[-i - 1]]
    if value!=1:
        ans+=2*(value-1)-1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = int(s)
string = 1
i = 2
ans = 0

while l > string:
    string = 2019 * i 
    if s.find(str(string)) != -1:
        ans += 1
    i += 1
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
ans=0
def occurrences(string, sub):
    count = start = 0
    while True:
        start = string.find(sub, start) + 1
        if start > 0:
            count+=1
        else:
            return count

for i in range(199997):
    ans+= occurrences(S,str(2019*(i+1)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = input()
ll = len(s)
dat = list(map(int, list(s)))
dp = collections.defaultdict(int)
res = 0
for r in range(ll):
    v = dat[r]
    ndp = collections.defaultdict(int)
    for k in dp.keys():
        nt = (k * 10 + v) % 2019
        ndp[nt] += dp[k]
    res += dp[0]
    ndp[v] += 1
    dp = ndp
res += dp[0]
print(res)

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()

    ans_list = []
    """
    for i in range(len(s)):
        jump = 4
        start = 0
        for j in range(0, len(s)):

            start = start
            end = start + j

            if start >= end:
                continue

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

            if int_s % 2019 == 0:
                ans_list.append((start, end))

            jump += 1
    """

    count = 0
    for i in range(0, 5):
        for j in range(4, len(s)):
            if i // j >= 2:
                continue
            if j == 4 and s[i] == 1:
                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()