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()
count = 0
 
for i in range(len(S)-3):
    for j in range(len(S)-3-i):
        if int(S[i:j+i+4]) % 2019 == 0:
            count += 1
 
print(count)

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 = 0
m = len(mods)
modss = list(set(mods))
for s in modss:
    if s == 0:
        count += sum([s == i for i in mods])
        count += math.factorial(sum([s == i for i in mods])) -1
    else:
        count += math.factorial(sum([s == i for i in mods])) -1
print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input().strip()

dic = {}
for i in range(0, len(S)-2):
    s = int(S[i:])
    a = s%2019
    if a in dic.keys():
        dic[a].append(i)
    else:
        dic[a] = [i]

cnt = 0
for k, v in dic.items():
    m = len(v)
    if m >= 2:
        cnt += m * (m-1) / 2
    elif k ==0:
        cnt += m

print(int(cnt))

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)

main()

Submission status: Time Limit Exceeded

Solution code:
import math
from functools import reduce
from collections import deque,defaultdict
import sys
sys.setrecursionlimit(10**7)

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

s = input()
X = list(map(int, list(s)))
n = len(s)

ruiseki = [0] * (n+1)
for i in (range(n-1, -1, -1)):
    ruiseki[i] = (X[i] * 10**(n-i-1) + ruiseki[i+1]) % 2019

# log(ruiseki)

ans = 0
count = defaultdict(int)

for x in ruiseki:
    # log(count[x])
    ans += count[x]
    count[x] += 1
# log(count)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# if the remainders (mod 2019) are equal, the difference will be divisible
# x = Q*2019+5 y = R*2019+5 then x-y=(Q-R)*2019
# unless Q-R<=0
# where the substring itself is (Q-R)*2019/10**(something)
# 2019 = 3*673 in prime factorization
# so multiplying by 10 doesn't affect whether a number is divisible by it
from collections import Counter
s = input()[::-1]
partial = 0
remainders = list()
for i, character in enumerate(s):
  partial = (partial+int(character)*10**i)%2019
  remainders.append(partial)
histo = Counter(remainders)
non_single_count = int(sum(count*(count-1)/2 for value, count in histo.items() if value!=0))
single_count = int(sum(count for value, count in histo.items() if value==0))
print(non_single_count+single_count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from numba import jit
s = input()
n = len(s)
dic = {}
@jit
def main():
    global dic
    dic[0] = 1
    for i in range(n-1,-1,-1):
        p = int(s[i:])%2019
        if p in dic:
            dic[p] += 1
        else:
            dic[p] = 1
    return dic

main()
print(sum(dic[i]*(dic[i]-1)//2 for i in dic))

Submission status: Time Limit Exceeded

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

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

#mod = 10**9+7

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

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

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

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

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

    #print(l[i])
#print(l)

# #優先度付きqueue

kl=[0]*2019
kl[0]=1
# a = [0]
# heapq.heapify(a)  # aを優先度付きキューに

tmp=0
t2=1
count=0
for s in reversed(ss):
    tmp=(tmp+int(s)*10**count)%2019#ここを単純にint(s[i:sl-1])%2019とか、%10**i%2019すると、TLEになる！！！DPで求めなければならない
    kl[tmp]+=1
    #t2=t2*10%2019
    count+=1
    #print(tmp)

#print(a)




ans=0
for k in kl:
    ans+=k*(k-1)//2
print(ans)

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

# print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
s=s[::-1]
mod = 2019
li = [int(s[0])]
for i in range(len(s) - 1):
    li.append((li[-1] + 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=list(input())
s=[int(i) for i in s]
mods=[0]
for i in range(len(s)):
    now=(s[-1-i]*(10**i))%2019
    mods.append((mods[-1]+now)%2019)
mods=mods[::-1]
from collections import Counter
c=dict(Counter(mods))
ans=0
for k in c.keys():
    ans+=c[k]*(c[k]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():
    S = input()
    L = len(S)-1
    NUM = [0]*(L+1)
    CNT = 0
    for i in range(1,len(NUM)):
        NUM[i] = NUM[i-1] + int(S[i-1])
    for i in range(L-3):
        for j in range(i+4,L+1):
            if (NUM[j] - NUM[i])%3 == 0:
                # print(NUM[j] - NUM[i])
                # print(int(S[i:j]))
                if int(S[i:j])%2019 == 0:
                    CNT += 1
    # print(NUM)
    print(CNT)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()

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

for i in range(len(s)):
    amari = int(s[i:]) % 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:
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:
import functools
def solve():
    s = int(input())
    t = [int(x) for x in list(str(s))]

    l = []
    for i in range(1, 100):
        num = [int(x) for x in list(str(i*2019))]
        if 0 not in num:
            l.append(num)

    cnt = 0
    n = len(t)
    for i in range(5, n):
        for j in range(n-i+1):
            if t[j:j+i] in l:
                cnt += 1

    return cnt
    
print(solve())

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = input()
n = len(s)

T = [0] * (n + 1)

for k in range(n):
	tmp = int(s[k:])
	T[k] = tmp % 2019
    
c = Counter(T)

cnt = 0
for v in c.values():
	if v > 1:
		cnt += v * (v - 1) // 2

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from sys import setrecursionlimit, exit
setrecursionlimit(1000000000)

def main():
    s = input()
    l = len(s)
    mod = [0] * 2019
    for i in range(l):
        mod[int(s[i:])%2019] += 1
    mod[0] += 1
    ans = 0
    for i in range(2019):
        ans += (mod[i] - 1) * mod[i] // 2
    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
mod = 2019
li = [int(s[-1])]
for i in range(len(s) - 1):
    li.append((li[-1] + int(s[-(2 + i)]) * (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:
def main():
    s = input()
    n = len(s)
    dic = {}
    dic[0] = 1
    for i in range(n-1,-1,-1):
        p = int(s[i:])%2019
        if p in dic:
            dic[p] += 1
        else:
            dic[p] = 1
    print(sum(dic[i]*(dic[i]-1)//2 for i in dic))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

S = input()
N = len(S)
l = []
for i in range(N):
    l.append(int(''.join(S[i:N]))%2019)

c = collections.Counter(l)

values, counts = zip(*c.most_common())
counts = list(counts)

try:
    ans = counts.pop(list(values).index(0))
except:
    ans = 0

for i in range(len(counts)):
    if counts[i] != 1:
        ans += counts[i] * (counts[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 i, s in enumerate(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:
import math
from functools import reduce
from collections import deque,defaultdict
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def hoge(n):
    count = 0
    i = 0
    while count < n:
        if "0" not in str(2019*i):
            count += 1
            print(2019*i)
        i += 1


s = input()
X = list(map(int, list(s)))
n = len(s)

ruiseki = [0] * (n+1)
for i in (range(n-1, -1, -1)):
    ruiseki[i] = (X[i] * 10**(n-i-1) + ruiseki[i+1]) % 2019

# log(ruiseki)

ans = 0
count = defaultdict(int)

for x in ruiseki:
    # log(count[x])
    ans += count[x]
    count[x] += 1
# log(count)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
p = 2019
ans = 0

num_rem = [0] * p
num_rem[0] = 1
for i in range(n):
    # print(int(s[i:]))
    mod = int(s[i:])%p
    num_rem[mod] = num_rem[mod] + 1
for m in num_rem:
    ans += (m*(m-1))//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
list1 = []
for i in range(n):
    temp = ""
    for j in range(i,n):
        temp+=s[j]
        if(int(temp) not in list1):
            if(int(temp)%2019==0):
                list1.append(temp)
                temp=""

print(len(list1))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
cs = [0] * (n + 1)
for i, x in enumerate(s[::-1]):
    cs[i + 1] = cs[i] + int(x) * 10 ** i % 2019
    cs[i + 1] %= 2019
dp = [0] * 2019
for i, x in enumerate(cs):
    dp[x] += 1
ans = sum(x * (x - 1) // 2 for x in dp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import functools
def solve():
    s = int(input())

    t = [int(x) for x in list(str(s))]

    cnt = 0
    n = len(t)
    for i in range(5, n):
        for j in range(n-i+1):
            new = int(functools.reduce(lambda x, y: x + y, [str(x) for x in t[j:j+i]]))
            if new%2019 == 0:
                cnt += 1

    return cnt
    
print(solve())

Submission status: Time Limit Exceeded

Solution code:
#いけそう
#atcoder 
import itertools
s = input();
ans = 0;

list_ = range(len(s));
a = list(itertools.combinations(list_, 2))

for c,d in a:
    if int(s[c:d + 1]) % 2019 == 0 :
        ans += 1;

print(ans)

Submission status: Time Limit Exceeded

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

    ans = 0
    for x in set(l):
        y = l.count(x)
        ans += y * (y - 1) // 2
        if x == 0:
            ans += y
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
n = len(S)

dp = [0] * 2019
dp[0] = 1
tmp = 0
for i in range(n):
    tmp += int(S[n-1-i]) * (10**i)
    dp[tmp%2019] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
length = len(S)
a = []
output = 0

for i in range(length-3):
    n = 0
    for j in range(i):
        s = int(S[n:length-i+n+1])
        if s % 2019 == 0:
            a.append(s)
            output += 1
        n += 1

print(output)

Submission status: Time Limit Exceeded

Solution code:
S = input()

S_i = [0 for _ in range(2019)]

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

ans = 0
for k in range(2019):
    if k==0:
        ans += S_i[k]
    elif S_i[k]>1:
        ans += S_i[k]*(S_i[k]-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

S = list(input())
n = len(S)

dp = [0] * 2019
dp[0] = 1
tmp = 0
for i in range(n):
    tmp += int(S[n-1-i]) * (10**i)
    dp[tmp%2019] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

i = 0
count = 0
j = 2
j1 = 0
#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
            j1 = j 
        j += 1    
    if j1 != 0:
        i = j1-1
        j1 = 0
    else:
        i +=1    

print(count)

Submission status: Time Limit Exceeded

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

S = input()
n = len(S)

dp = [0] * 2019
dp[0] = 1
tmp = 0
for i in range(n):
    tmp += int(S[n-1-i]) * (10**i)
    dp[tmp%2019] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import groupby
s = input()
n = len(s)
li = [0]
for i in range(n-1,-1,-1):
    p = s[i:]
    li.append(int(p)%2019)
li.sort()
lin = []
for key, value in groupby(li):
    lin.append(len(list(value)))
point = 0
for i in lin:
    point += i*(i-1)//2
print(point)

Submission status: Time Limit Exceeded

Solution code:
s = input()
if(len(s) < 5):
    print(0)
else:
    t = [0]*2019
    t[0] = 1
    p = 0
    for i in range(1,len(s)+1):
        t[int(s[-i:])%2019] += 1
    for i in range(2019):
        p += (t[i]*(t[i]-1))//2
    print(p)

Submission status: Time Limit Exceeded

Solution code:
s = input()

l = [int(s[i:]) % 2019 for i in range(len(s))]
d = {}

for i in l:
  d[i] = d.get(i, 0) + 1

count = 0  
for i in d:
  count += d[i]*(d[i]-1)//2

count += l.count(0)

print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=len(s)
ans=0
ruiseki=[int(s[-1]) for i in range(t)]
for i in range(1,t):
    ruiseki[i]=(ruiseki[i-1]+int(s[-1-i])*pow(10,i,2019))%2019

ans+=ruiseki.count(0)
for i in range(2019):
    o=ruiseki.count(i)
    ans+=o*(o-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:


def main():
  s = input()
  len_s = len(s)

  res = 0
  res_list = [0] * 2019
  res_list[0] = 1

  _10 = 1
  use_num = 0
  for i in s[::-1]:
    use_num += int(i) * _10
    _10 *= 10
    m = use_num % 2019
    res += res_list[m]
    res_list[m] += 1

  print(res)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline

    s = int(input())
    li = []
    li_append = li.append

    cm = {}
    for i in range(1,2*(10 ** 5)):
        cm[i] = i * (i - 1) // 2
    cm[0] = 0



    for i in range(1, len(str(s))+1):
        num = s % (10 ** i)
        li_append(int(num) % 2019)

    co = Counter(li)
    co_1 = Counter(co.values())
    ans_1 = 0

    for x in co_1.keys():
        if x != 1:
            ans_1 += cm[x]* co_1[x]
    ans2 = 0
    if 0 in co:
        ans2 = co[0]
    print(ans_1 + ans2)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

lst = [None for i in range(n+1)]
arr = {0:1}

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
N = len(S)
arr = []
ans = [0 for i in range(2020)]
cnt0 = 0
for i in range(N):
  a = int(S[N-i-1:N])%2019
  ans[a] += 1
  arr.append(a)
  if a == 0:
    cnt0 += 1

cnt = 0
for i in range(1,2020):
  if ans[i] > 1:
    cnt = int(cnt + (ans[i]-1)*ans[i]/2)

cnt = int(cnt + (cnt0+1)*cnt0/2)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
LIN=lambda : list(IN())
mod=1000000007

#+++++

def main():
	#a = int(input())
	#b , c = IN()
	c=0
	s = input()
	
	ll=len(s)
	for i in range(ll):
		for j in range(i+1, ll+1):
			#print(s[i:j])
			if int(s[i:j]) % 2019 == 0:
				#print(i,j,s[i:j])
				c+=1
	print(c)
				
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Time Limit Exceeded

Solution code:
s = input()

i = 0
count = 0
j = 2
j1 = 0
#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
            j1 = j
        j += 1    
    if j1 != 0:
        i = j1-1
        j1 = 0
    else:
        i +=1    

print(count)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
count = 0

l = [int(s[i:]) % 2019 for i in range(len(s))]
d = {}

for i in l:
  d[i] = d.get(i, 0) + 1
  
for i in d:
  count += d[i]*(d[i]-1)//2

count += l.count(0)

print(count)

Submission status: Time Limit Exceeded

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

S = input()
n = len(S)

dp = [0] * 2019
dp[0] = 1
for i in range(n):
    dp[(int(S[i:])%2019)] += 1
# print(dp)

ans = 0
for i in dp:
    if i >= 2:
        ans += i*(i-1)//2
        # print(i, dp[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
l = []
n = len(s)
mod = 2019
for i in range(n-1, -1, -1):
    l.append(int(s[i:])%mod)
cnt = [0 for i in range(2019)]
cnt[0] += 1
for i in range(len(l)):
    cnt[l[i]] += 1
ans = 0
for i in range(len(cnt)):
    ans += cnt[i]*(cnt[i]-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

len_str = len(S)

ans = 0
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
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
u=int(s[-1])
s=s[:-1]
l=[0]*2019
l[0]=1
for i in s[::-1]:
  u=int(i)*10**len(str(u))+u
  l[u%2019]+=1
ans=0
for i in l:
  ans+=i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

counter = 0
for n in range(4, l):
    for i in range(l-3):
        try:
            if (i+n) < l:
                if int(S[i:i+n+1]) % 2019 == 0:
                    counter += 1
        except:
            pass

print(counter)

Submission status: Time Limit Exceeded

Solution code:
inf = 10**15
mod = 2019
s = input()
n = len(s)

#jが来てあまりがiとなった時に元々のあまりはいくつか
dic = [[0 for j in range(10)] for i in range(2019)]

for i in range(2019):
    for j in range(1,9):
        tmp = (i*10+j)%mod
        dic[tmp][j] = i

# i桁までみたとき、i桁目から左に連続しているもので、あまりがjの物の個数
dp_sub = [[0 for j in range(2019)]for i in range(n+1)]
dp_sub[1][int(s[0])] = 1

# i桁までみたとき、あまりがjの物の個数
dp = [[0 for j in range(2019)]for i in range(n+1)]
dp[1][int(s[0])] = 1
for i in range(1,n):
    c = int(s[i])
    dp_sub[i+1][c] = 1
    for j in range(2019):
        dp[i+1][j] = dp[i][j]
        if dic[j][c] != 0:
            dp_sub[i+1][j] += dp_sub[i][dic[j][c]]
            dp[i+1][j] += dp_sub[i][dic[j][c]]
print(dp[n][0])

Submission status: Time Limit Exceeded

Solution code:

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

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

main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = [0]*2019
count[0] = 1
for i in range(n):
    count[int(S[i:])%2019] += 1
   
ans = 0
for i in count:
    ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = str(input())
count = 0
for j in range(len(n) - 3):
  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:
from collections import defaultdict, deque

S = input()

D = defaultdict(set)
for x in range(2019, 10**8, 2019):
  y = str(x)
  D[y[0:4]].add(y)

L = deque()
D1 = defaultdict(set)
D2 = {}
for i in range(len(S)-3):
  ii = i
  R = ''
  while ii < len(S):
    y = R + S[ii:ii+4-len(R)]
    for d in D[y]:
      if R + S[ii:ii+len(d)-len(R)] == d:
        L.appendleft(i)
        D1[i].add(ii+len(d)-len(R))
        D2[ii+len(d)-len(R)] = i
        break
    else:
      X = int(R + S[ii:ii+8-len(R)])
      ii += 8-len(R)
      R = str(X % 2019)
      continue
    break

for x in L:
  if x in D2:
    D1[D2[x]] += D1[x]
print(sum(len(x) for x in D1.values()))

Submission status: Time Limit Exceeded

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

mod = 2019
dp = [0]*(L)
zero = 0
for l in range(L):
    t = int(S[-l:])%mod
    dp[l] = t
    if t==0:
        zero+=1

l = len(set(dp))
print(L-l+zero)

Submission status: Time Limit Exceeded

Solution code:
a = [1, 10, 100, 1000, 1924, 1069, 595, 1912, 949, 1414, 7, 70, 700, 943, 1354, 1426, 127, 1270, 586, 1822, 49, 490,
     862, 544, 1402, 1906, 889, 814, 64, 640, 343, 1411, 1996, 1789, 1738, 1228, 166, 1660, 448, 442, 382, 1801, 1858,
     409, 52, 520, 1162, 1525, 1117, 1075, 655, 493, 892, 844, 364, 1621, 58, 580, 1762, 1468, 547, 1432, 187, 1870,
     529, 1252, 406, 22, 220, 181, 1810, 1948, 1309, 976, 1684, 688, 823, 154, 1540, 1267, 556, 1522, 1087, 775, 1693,
     778, 1723, 1078, 685, 793, 1873, 559, 1552, 1387, 1756, 1408, 1966, 1489, 757, 1513, 997, 1894, 769, 1633, 178,
     1780, 1648, 328, 1261, 496, 922, 1144, 1345, 1336, 1246, 346, 1441, 277, 751, 1453, 397, 1951, 1339, 1276, 646,
     403, 2011, 1939, 1219, 76, 760, 1543, 1297, 856, 484, 802, 1963, 1459, 457, 532, 1282, 706, 1003, 1954, 1369, 1576,
     1627, 118, 1180, 1705, 898, 904, 964, 1564, 1507, 937, 1294, 826, 184, 1840, 229, 271, 691, 853, 454, 502, 982,
     1744, 1288, 766, 1603, 1897, 799, 1933, 1159, 1495, 817, 94, 940, 1324, 1126, 1165, 1555, 1417, 37, 370, 1681, 658,
     523, 1192, 1825, 79, 790, 1843, 259, 571, 1672, 568, 1642, 268, 661, 553, 1492, 787, 1813, 1978, 1609, 1957, 1399,
     1876, 589, 1852, 349, 1471, 577, 1732, 1168, 1585, 1717, 1018, 85, 850, 424, 202]

length = 224
mod = 2019
cont = [0 for _ in range(length)]
sums = [0 for _ in range(length)]
count = 0
s = list(map(int, input()))
n = len(s)
for i in range(n):
    m = s[n-1-i]
    for j in range(min(i + 1, length)):
        sums[j] += m * a[(i-j) % length]
        if i > j:
            sums[j] %= mod
            if i % length == j:
                if sums[j - 1] == 0:
                    cont[j] += 1
                else:
                    cont[j] = 0
            if sums[j] == 0:
                count += cont[j] + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
#input = sys.stdin.buffer.readline
from collections import Counter

def main():
    S = input()
    
    l = []
    base = 1
    for st in S[::-1]:
        l.append((int(st)*base)%2019)
        base *= 10
        
    cum = [0]
    
    for num in l:
        cum.append((cum[-1]+num)%2019)
    
    
    c = Counter(cum)
    
    ans = 0
    for it,num in c.items():
        ans += (num*(num-1))//2
    
    print(ans)    
    
    
    
    
    
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

c=0

def check2019(S):
    global c
    global d
    s = int(S)
    if s%2019 ==0:
        c +=1
    else:
        pass
    if s>10000:
            return check2019(S[1:])
    else:
        return 0
S = input()


for i in range(len(S)-3):
    check2019(S[:len(S)-i])
    if i == 0 and c >0:
        c-=1
else:
    pass

print(c)

Submission status: Time Limit Exceeded

Solution code:
S=input()
N=len(S)
nums=[0]*2019
zero=0
tmp=''
for i in range(N-1,-1,-1):
    tmp=S[i]+tmp
    div=int(tmp)%2019
    if div==0:
        zero+=1
    nums[div]+=1
    
ans = zero
for v in nums:
    if v<2:
        continue
    ans += v*(v-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

a = [1, 10, 100, 1000, 1924, 1069, 595, 1912, 949, 1414, 7, 70, 700, 943, 1354, 1426, 127, 1270, 586, 1822, 49, 490,
     862, 544, 1402, 1906, 889, 814, 64, 640, 343, 1411, 1996, 1789, 1738, 1228, 166, 1660, 448, 442, 382, 1801, 1858,
     409, 52, 520, 1162, 1525, 1117, 1075, 655, 493, 892, 844, 364, 1621, 58, 580, 1762, 1468, 547, 1432, 187, 1870,
     529, 1252, 406, 22, 220, 181, 1810, 1948, 1309, 976, 1684, 688, 823, 154, 1540, 1267, 556, 1522, 1087, 775, 1693,
     778, 1723, 1078, 685, 793, 1873, 559, 1552, 1387, 1756, 1408, 1966, 1489, 757, 1513, 997, 1894, 769, 1633, 178,
     1780, 1648, 328, 1261, 496, 922, 1144, 1345, 1336, 1246, 346, 1441, 277, 751, 1453, 397, 1951, 1339, 1276, 646,
     403, 2011, 1939, 1219, 76, 760, 1543, 1297, 856, 484, 802, 1963, 1459, 457, 532, 1282, 706, 1003, 1954, 1369, 1576,
     1627, 118, 1180, 1705, 898, 904, 964, 1564, 1507, 937, 1294, 826, 184, 1840, 229, 271, 691, 853, 454, 502, 982,
     1744, 1288, 766, 1603, 1897, 799, 1933, 1159, 1495, 817, 94, 940, 1324, 1126, 1165, 1555, 1417, 37, 370, 1681, 658,
     523, 1192, 1825, 79, 790, 1843, 259, 571, 1672, 568, 1642, 268, 661, 553, 1492, 787, 1813, 1978, 1609, 1957, 1399,
     1876, 589, 1852, 349, 1471, 577, 1732, 1168, 1585, 1717, 1018, 85, 850, 424, 202]

length = 224
mod = 2019
cont = [0 for _ in range(length)]
sums = [0 for _ in range(length)]
count = 0
s = list(map(int, input()))
s.reverse()
n = len(s)
for i in range(n):
    m = s[i]
    for j in range(min(i + 1, length)):
        sums[j] += m * a[(i-j) % length]
        if i > j:
            sums[j] %= mod
            if i % length == j:
                if sums[j - 1] == 0:
                    cont[j] += 1
                else:
                    cont[j] = 0
            if sums[j] == 0:
                count += cont[j] + 1
print(count)

Submission status: Time Limit Exceeded

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

len_str = len(S)

ans = 0
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])
        else:
            temp_ans = (before_m * 10 + int(S[j - 1]))
        temp_ans %= 2019
        before_m = temp_ans
        if temp_ans == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

res = 0
res_list = [0] * 2019
res_list[0] = 1

_10 = 1
use_num = 0
for i in range(len_s):
  use_i = len_s - i - 1
  use_num += int(s[use_i]) * _10
  _10 *= 10
  m = use_num % 2019
  res += res_list[m]
  res_list[m] += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
from math import ceil #切り上げ
import numpy as np

def inp(): return int(input()) #一個だけ読み取り
def inpm(): return map(int,input().split()) #mapオブジェクトで呼び出し
def inpl(): return list(map(int, input().split())) #数字リストをintで
def inpls(): return list(input().split()) #リストをstrで

def judge(n):
  return True if n%3==0 else False
  
S = input()
l = len(S)
ok_list = []
cnt = 0

if l <= 4:
  print("0")
else:
  for i in range(0,l):
    sum = []
    for j in range(0,l):
      if i==j:
        sum.append(int(S[j]))
      elif j<i:
       	sum.append(1)
      else:
       	sum.append(sum[j-1]+int(S[j]))
    sum_c = list(map(judge,sum))
    try:
      ok_ind = [k for k, x in enumerate(sum_c) if x == True]
      for x in ok_ind:
      	ok_list.append([i,x+1])
    except:
      pass
      
  for i,j in ok_list:
    ss = int(S[i:j])
    if ss%673 == 0:
      cnt = cnt +1
    else:
      pass

  print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

s = list(input())
s_l = t = len(s)

count=0
c=0

while(t>=0):
  kazu = []
  for j in range(c,s_l):
    kazu.append(s[j])
    k = ''.join(kazu)
    k = int(k)
    #print(k)
    if k%2019==0:
      count+=1
    else:
      count=count
  c+=1
  t-=1
print(count)