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()
a = int(s)%2019
mod_N = [0]*2019
mod_N[a] += 1
r = 10**(len(s)-1)
for i in range(len(s)):
  a = (a-int(s[i])*10**r)%2019
  r = r//10
  mod_N[a] += 1
ans = 0
for i in mod_N:
  ans += i*(i-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x


mod = [0]*2019
mod[0] = 1
pre = 0
for i in reversed(range(n)):
    pre = (pre + int(s[i])*pow_k(10, n-i-1))%2019
    mod[pre] += 1
print(sum([m*(m-1)//2 for m in mod]))

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = int(s)
mod_N = {}
mod_N[a%2019] = 1
for i in range(len(s)):
  a = a-int(s[i])*10**(len(s)-i-1)
  mod = a%2019
  if mod in mod_N:
    mod_N[mod] +=1
  else:
    mod_N[mod] = 1

ans = 0
for i in mod_N:
  ans += mod_N[i]*(mod_N[i]-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = int(s)
mods = [a%2019]
for i in range(len(s)):
  a = a-int(s[i])*10**(len(s)-i-1)
  mods.append(a%2019)
mod_N = {}
smod = set(mods)
for i in smod:
  mod_N[i] = 0
for i in mods:
  mod_N[i] += 1
ans = 0
for i in mod_N:
  ans += mod_N[i]*(mod_N[i]-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

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

mod = [0]*2019
mod[0] = 1
pre = 0
for i in reversed(range(n)):
    pre = (pre + int(s[i])*10**(n-i-1))%2019
    mod[pre] += 1
print(sum([m*(m-1)//2 for m in mod]))

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1]
length = len(s)
a = [0] * length
d = [0] * length
ans = [0] * 2019

a[0] = int(s[0])
d[0] = a[0]
ans[d[0]] += 1

for i in range(1, length):
    a[i] = int(s[i]) * 10**i % 2019
    d[i] = (d[i-1] + a[i]) % 2019
    ans[d[i]] += 1

print(int(sum([ans[i] * (ans[i]-1) for i in range(2019)]) / 2) + ans[0])

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = int(s)
mods = [a%2019]
for i in range(len(s)):
  a = a-int(s[i])*10**(len(s)-i-1)
  mods.append(a%2019)
mod_N = {}
for i in mods:
  mod_N[i] = 0
for i in mods:
  mod_N[i] += 1
ans = 0
for i in mod_N:
  ans += mod_N[i]*(mod_N[i]-1)/2
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
#S = input()
reverse_S = input()[::-1]
n = len(reverse_S)
count = 0
mod = 0
mods = [0] * 2019
mods[0] = 1
x = 1
for s in reverse_S:
    mod = (mod + (int(s) * x)) % 2019
    mods[mod] = mods[mod] + 1
    x = x * 10

for v in mods:
    count = count + v*(v-1)//2

print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
s = input()
n = len(s)
m = []

for i in range(0, n+1):
  ss =  int(s[i:n+1]) if s[i:n+1].isdigit() else 0
  m.append(ss % 2019)

C = Counter(m)
ct = 0
for v in C.values():
  if v == 2:
    ct += 1
print(ct)

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = int(s)
mods = [a%2019]
for i in range(len(s)):
  a = a-int(s[i])*10**(len(s)-i-1)
  mods.append(a%2019)
mod_N = {}
for i in mods:
  mod_N[i] = 0
for i in mods:
  mod_N[i] += 1
ans = 0
for i in mod_N:
  ans += int(mod_N[i]*(mod_N[i]-1)/2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
S = int(S)
list = []
for i in range(N):
	S = S % (10 ** (N - i))
	list.append(S % 2019)
list.append(0)
list.sort()
ans = 0
x = 1
for j in range(N):
	if list[j] == list[j + 1]:
		x += 1
	else:
		ans += x * (x - 1) // 2
		x = 1
ans += x * (x - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = 0
mod = 0
mods = [0] * 2019
mods[0] = 1
x = 1
for s in reversed(S):
    mod = (mod + (int(s) * x)) % 2019
    mods[mod] = mods[mod] + 1
    x = x * 10

for v in mods:
    count = count + v*(v-1)//2

print(count)

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = 0
T = [0] * (n + 1)
mod = 0
x = 1
mod_dict = {i:0 for i in range(2019)}
mod_dict[0] = 1
for s in reversed(S):
    mod = (mod + (int(s) * x)) % 2019
    mod_dict[mod] = mod_dict[mod] + 1
    x = x * 10

count = 0
for k, v in mod_dict.items():
    count = count + v*(v-1)//2

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
m = []

for i in range(0, n+1):
  ss =  int(s[i:n+1]) if s[i:n+1].isdigit() else 0
  m.append(ss % 2019)

u = set()
ct = 0
for mm in m:
  if mm not in u:
    u.add(mm)
  else:
    ct += 1
    
print(ct)

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = 0
T = [0] * (n + 1)
mods = [0] * (n + 1)
mod_dict = {i:0 for i in range(2019)}
mod_dict[0] = 1
for i in reversed(range(n)):
    mods[i] = (mods[i+1] + (int(S[i]) * (10 ** (n - i -1)))) % 2019
    mod_dict[mods[i]] = mod_dict[mods[i]] + 1

count = 0
for k, v in mod_dict.items():
    count = count + int(v*(v-1)/2)

print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()
a = int(s)
mods = [a%2019]
for i in range(len(s)):
  a = a-int(s[i])*10**(len(s)-i-1)
  mods.append(a%2019)
mod_N = {}
for i in mods:
  if not i in mod_N:
    mod_N[i] = 1
  else:
    mod_N[i] += 1
ans = 0
for i in mod_N:
  ans += int(mod_N[i]*(mod_N[i]-1)/2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length, dtype=int)
d = np.zeros(length, dtype=int)
ans = np.zeros(2019, dtype=int)

for i in range(length):
    if i == 0:
        a[i] = int(s[i])
        d[i] = a[i]
        ans[d[i]] += 1
    else:
        a[i] = int(s[i]) * pow(10, i) % 2019
        d[i] = (d[i-1] + a[i]) % 2019
        ans[d[i]] += 1

print(int(sum([ans[i] * (ans[i]-1) for i in range(ans.shape[0])]) / 2) + ans[0])

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = 0
T = [0] * (n + 1)
mod_dict = {0:0}
count  = 0
for i in reversed(range(n)):
    #print(i)
    T[i] = T[i+1] + int(S[i]) * (10 ** (n - i - 1))
    # print("Num : " + str(T[i]))
    mod = T[i] % 2019
    # print("Mod : " + str(mod))
    if(mod in mod_dict.keys()):
        # mod_dict[mod] = mod_dict[mod] + 1
        count = count + 1
    else:
        mod_dict[mod] = 0
print(count)
#print(sum(mod_dict.values()))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
n = len(S)
count = 0
T = [0] * (n + 1)
mod_dict = {0:0}

for i in reversed(range(n)):
    #print(i)
    T[i] = T[i+1] + int(S[i]) * (10 ** (n - i - 1))
    # print("Num : " + str(T[i]))
    mod = T[i] % 2019
    # print("Mod : " + str(mod))
    if(mod in mod_dict.keys()):
        mod_dict[mod] = mod_dict[mod] + 1
    else:
        mod_dict[mod] = 0

print(sum(mod_dict.values()))

Submission status: Time Limit Exceeded

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

ct = 0
for i in range(n+1)[::-1]:
  for j in range(i+1,n+1)[::-1]:
    ss = int(s[i:j])
    # q = ss // 2019
    m = ss % 2019
    # print(f"{i}:{j}, {ss}, {q}, {m}")
    if m == 0:# and q > 0:
      ct += 1
      # print(f"{ss}, {q}, {m}")

print(ct)

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
n = len(S)
dp = [0]*2019
dp[0] = 1
X = 0
Y = 1
for s in S:
    Z=(int(s)*Y)%2019
    X = (X+Z)%2019
    dp[X] += 1
    Y *= 10
answer = 0
for i in range(2019):
    answer += dp[i]*(dp[i]-1)/2
print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
n = len(S)
dp = [0]*2019
dp[0] = 1
X = 0
Y = 1
for s in S:
    X = (X+int(s)*Y)%2019
    dp[X] += 1
    Y *= 10
answer = 0
for i in range(2019):
    answer += dp[i]*(dp[i]-1)/2
print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
# ABC164 D - Multiple of 2019
import numpy as np
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length, dtype=int)
d = np.zeros(length, dtype=int)
ans = np.zeros(2019+1)

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    ans[d[i+1]] += 1

x = ans[np.where(ans > 1)]

print(int(sum([x[i] * (x[i]-1) for i in range(x.shape[0])]) / 2) + d.shape[0] - np.count_nonzero(d))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length, dtype=int)
d = np.zeros(length, dtype=int)
ans = np.zeros(2019+1)

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    ans[d[i+1]] += 1

x = ans[np.where(ans > 1)]

print(int(sum([math.factorial(x[i]) for i in range(x.shape[0])]) / 2) + d.shape[0] - np.count_nonzero(d))

Submission status: Time Limit Exceeded

Solution code:
s=int(input())
n=len(str(s))
a=[0]*2019
a[0]=1
ans=0
for i in range(n):
  k=(s%10**(n-i))%2019
  a[k]+=1
for i in range(2019):
  ans+=a[i]*(a[i]-1)/2

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

S = input()
size = len(S)
L = [0] * 2019

tmp = int(S[size-1])  # 最後の文字
L[0] = 1
L[tmp] += 1

for i in reversed(range(size - 1)):  # 最後の文字の一つ手前からループを回す
    d = size - i  # 今の桁数(10の何乗になるのか)
    n = int(S[i])  # 対象の文字
    r = (n * (10 ** (d - 1))) % 2019  # 2019で割った余り
    tmp = (tmp + r) % 2019
    # print(f"{i}桁目:{tmp}")
    L[tmp] += 1

ans = 0
for i in range(2019):
    tmp = L[i]
    if (tmp > 1):
        ans += tmp * (tmp - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1]
mod = 2019
cnt = [0] * mod
x = 1
cnt[0] = 1
total = 0
for s in s:
    total += int(s) * x
    total %= mod
    cnt[total] += 1
    x *= 10


ans = sum(i*(i-1)//2 for i in cnt)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = int(input())
n = len(str(S))
dp = [0]*2019
dp[0] = 1
for i in range(1,n+1):
    X =S%10**i % 2019
    dp[X] += 1
answer = 0
for i in range(2019):
    answer += dp[i]*(dp[i]-1)/2
print(int(answer))

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1]
m = 2019
ans = 0
x = 1
tot = 0
cnt = [0] * m
for s in s:
    cnt[tot] += 1
    tot += int(s) * x % m
    tot %= m
    ans += cnt[tot % m]
    x *= 10 % m
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()[::-1]
m = 2019
ans = 0
x = 1
tot = 0
cnt = [0] * m
for s in s:
    cnt[tot] += 1
    tot += int(s) * x
    tot %= m
    ans += cnt[tot]
    x *= 10
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    s=input()
    count=0
    if len(s)>3:

        for i in range(len(s)-4):
            for j in range(len(s)-i-3):
                e=s[i:i+4+j]
                if int(e)%2019==0:
                    count=count+1
        print(count)
    else:
        print(0)
resolve()

Submission status: Time Limit Exceeded

Solution code:
s=list(map(int,list(input())))[::-1]

n=len(s)
D=[0]*(n+1)
c=[0]*(n+1)
ans = 0
f = 0
dp = [0]*(2020)
dp[0]+=1

for e,i in enumerate(s):
    D[e]= i*pow(10,e)%2019
    if e!=0:
        c[e] = (c[e-1] + D[e])%2019
    else:
        c[e] = D[e]
    ans += dp[c[e]]
    dp[c[e]]+=1
# print(c)

# for e, i in enumerate(dp):
#     if i!=0:
#         print(e,i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=list(map(int,list(input())))[::-1]

n=len(s)
D=[0]*(n+1)
c=[0]*(n+1)

# print(s)
for e,i in enumerate(s):
    D[e]= i*pow(10,e)%2019
# print(D)
c[0] = D[0]
for e,i in enumerate(s):
    if e==0:continue
    c[e] = (c[e-1] + D[e])%2019
# print(c)
ans = 0
f = 0
dp = [0]*(2020)
for e,i in enumerate(c):
    ans +=dp[i]
    dp[i]+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from pip._vendor.distlib.compat import raw_input

s = raw_input()
l = len(s)
pairs = 0
if len(s) < 4:
    print(pairs)
    exit()
for i in range(len(s) - 4):
    for j in range(i + 4, len(s) + 1):
        curr = int(s[i:j])
        if curr % 2019 == 0:
            pairs += 1
print(pairs)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
s = input()
num = 2019

s_lst = list(s)

cnt = 0
for i in range(0, len(s_lst)):
    # print(i)
    for j in range(i+3, len(s_lst)+1):

        num_ = int("".join(s_lst[i:j]))
        # print(num_)
        if num_ % num == 0:
            # print(num_)
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

Solution code:

S = input()[::-1]


def calculate(rs):
    # rs = reversed(s)

    sss = 0
    dict = [0] * 2019
    dict[0] = 1
    for index, c in enumerate(rs):
        sss = sss + int(c) * 10 ** index
        mod = sss % 2019
        dict[mod] = dict[mod] + 1

    res = 0
    for d in dict:
        if d > 1:
            res = res + d -1

    print(res)


calculate(S)

Submission status: Time Limit Exceeded

Solution code:

S = input()


def calculate(s):
    rs = reversed(s)

    sss = 0
    dict = [0] * 2019
    dict[0] = 1
    for index, c in enumerate(rs):
        sss = sss + int(c) * 10 ** index
        mod = sss % 2019
        dict[mod] = dict[mod] + 1

    res = 0
    for d in dict:
        if d > 1:
            res = res + d -1

    print(res)


calculate(S)

Submission status: Time Limit Exceeded

Solution code:
S = input()


def calculate(s):
    length = len(s)

    rs = reversed(s)

    sss = 0
    dict = {0: 1}
    for index, c in enumerate(rs):
        sss = sss + int(c) * 10 ** index
        mod = sss % 2019

        if dict.get(mod) == None:
            dict.__setitem__(mod, 1)
        else:
            dict.__setitem__(mod, dict.get(mod) + 1)


    res = 0
    for d in dict.values():
        if d > 1:
            res = res + d -1

    print(res)


calculate(S)

Submission status: Time Limit Exceeded

Solution code:

S = input()

def comn(n,m):
    originamN = n
    s1 = 1
    s2 = 1
    while n >= (originamN-m + 1):
        s1 = s1 * n
        n = n - 1

    while m >= 1:
        s2 = s2 * m
        m = m - 1

    return s1 // s2


def calculate(s):
    length = len(s)

    dict = {0:1}
    for i in range(len(s)):
        index = (int(s[i:length]) % 2019)
        if dict.get(index) != None:
            dict.__setitem__(index, dict.get(index) + 1)
        else:
            dict.__setitem__(index, 1)

    result = 0
    for val in dict.values():
        if val > 1:
            result = result + val - 1

    print(result)
calculate(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()

def comn(n,m):
    originamN = n
    s1 = 1
    s2 = 1
    while n >= (originamN-m + 1):
        s1 = s1 * n
        n = n - 1

    while m >= 1:
        s2 = s2 * m
        m = m - 1

    return s1 // s2



def calculate(s):
    length = len(s)

    dict = {0:1}
    for i in range(len(s)):
        index = (int(s[i:length]) % 2019)
        if dict.get(index) != None:
            dict.__setitem__(index, dict.get(index) + 1)
        else:
            dict.__setitem__(index, 1)

    result = 0
    for val in dict.values():
        if val > 1:
            result = result + comn(val,2)

    print(result)
calculate(S)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys,bisect,math
sys.setrecursionlimit(15000)

s = sys.stdin.readline().rstrip()
cum = [0]*(len(s)+1)#cumulative sum
import collections
d = collections.defaultdict(int)
d[0] = 1
ret = 0
dig = 1
for i in range(len(s))[::-1]:
    cum[i]=(int(s[i])*dig+cum[i+1])%2019
    dig *= 10
    #cum[i+1]=int(s[i:])%2019
    if cum[i] in d:
        ret += d[cum[i]]
    d[cum[i]] += 1
#print(d,cum,ret)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# n,m = map(int,sys.stdin.readline().split())
# a = list(map(int,sys.stdin.readline().split()))
# a = [sys.stdin.readline() for _ in range(n)]
# s = sys.stdin.readline().rstrip()
# n = int(sys.stdin.readline())
import sys,bisect,math
sys.setrecursionlimit(15000)

s = sys.stdin.readline().rstrip()
cum = [0]*(len(s)+1)#cumulative sum
import collections
d = collections.defaultdict(int)
d[0] = 1
ret = 0
for i in range(len(s))[::-1]:
    cum[i+1]=int(s[i:])%2019
    if cum[i+1] in d:
        ret += d[cum[i+1]]
    d[cum[i+1]] += 1
#print(d,cum,ret)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
A=str(input())
B = list(A)
L = len(B)
sum = 0
list =[]
for n in range(0,int(L)+1):
	for i in range(n,int(L)):
		list.append(str(B[i]))
		suuji = ''.join(list)
		if int(suuji) % 2019 == 0:
			sum += 1
	list = []
print(sum)

Submission status: Time Limit Exceeded

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

S=[0,]
cnt=[0]*2019
cnt[0]=1
for i in range(len(s)):
    S.append((int(S[i]) + int(s[i]) * 10**i) % 2019)
    cnt[S[i+1]]+=1

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

print(ans)

Submission status: Time Limit Exceeded

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

S=[0,]
cnt=[0]*2019
cnt[0]=1
for n,i in enumerate(s):
    # print(s[n], i, n)
    S.append((int(S[n]) + int(i) * 10**n) % 2019)
    cnt[S[n+1]]+=1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
b = len(s)
c = 0
f = 0
while True:
    if f == b:
        break
    elif b - f <= 2:
        f = 0
        b -= 1
    else:
        if int(s[f:b]) % 2019 == 0:
            c += 1
            f += 1
        else:
            f += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

s = input()
N = len(s)
T = [0] * (N + 1)
for i in range(N):
    x = int(s[i:])
    T[i] = x % 2019
cnt = Counter(T)
ans = 0
for c in cnt:
    n = cnt[c]
    if n < 2:
        continue
    ans += n * (n - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length, dtype=int)
d = np.zeros(length, dtype=int)

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    
x = np.unique(d, return_counts=True)[1]
x = x[np.where(x > 1)]

print(int(sum([math.factorial(x[i]) for i in range(x.shape[0])]) / 2) + d.shape[0] - np.count_nonzero(d))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

def main():
    S = input()
    # S = "1" * 200000
    S = S[::-1]

    x = 1
    tot = 0
    cnt = defaultdict(int)
    ans = 0

    for i in range(len(S)):
        # print("-----")
        # print(f"i: {i}")
        # print(f"tot: {tot}")
        # print(f"S[i]: {S[i]}")
        # print(f"x: {x}")
        # print(f"cnt: {cnt}")
        # print(f"cnt[tot]: {cnt[tot]}")
        # print(f"ans: {ans}")

        cnt[tot] += 1
        tot += int(S[i]) * x
        tot %= 2019
        ans += cnt[tot]
        x = x * 10
    
    # print("-----")
    # print(f"i: {i}")
    # print(f"tot: {tot}")
    # print(f"S[i]: {S[i]}")
    # print(f"x: {x}")
    # print(f"cnt: {cnt}")
    # print(f"cnt[tot]: {cnt[tot]}")
    # print(f"ans: {ans}")
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# 後で１の位から処理するためにひっくり返しておく
S = list(map(int, reversed(input())))
#print("S = ", S)
"""
「2019の倍数か」は「mod 2019 が 0 になるか」と同意。
しかし、普通に切り出して、１つずつチェックすると計算工数がかかるので、累積和を使う。
各桁の値を （桁の数値a_k）× 10^k と表現し、mod 2019 の値を予め計算し、累積和を作っておく。
ここで、10^k の部分は、2019と互いに疎（公約数がない）なので、
含めて計算しても mod 2019 の周期を乱さない。（勿論オフセットは変わる）
「(i, j) が2019で割り切るか」は、「i～j の累積和が同じ値になる（2019周期）か」と同意。
累積和が同じ値になる個数を数えて、組み合わせを計算すればよい。
"""
# a　：　各桁の mod 2019 値（桁の数値 a_k × 10^k mod 2019）
a = [0 for i in range(len(S))]
# sums ：　累積和
sums = [0 for i in range(len(S) + 1)]
# d : 10^k
d = 1
# cnt : 累積和 mod 2019 の出現度数表
cnt = [0 for i in range(2019)]
cnt[0] = 1  # sums[0] = 0 の分
for i in range(len(S)):
    a[i] = (S[i] * d) % 2019
    sums[i + 1] = (sums[i] + a[i]) % 2019
    d *= 10
    cnt[sums[i + 1]] += 1
#print("sums = ", sums)
#print("cnt = ", cnt)
ans = 0
for i in cnt:
    ans += i * (i - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length, dtype=int)
d = np.zeros(length, dtype=int)

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    
x = np.array([i for i in collections.Counter(d).values()])
x = x[np.where(x > 1)]

print(int(sum([math.factorial(x[i]) for i in range(x.shape[0])]) / 2) + d.shape[0] - np.count_nonzero(d))

Submission status: Time Limit Exceeded

Solution code:
import math
# 後で１の位から処理するためにひっくり返しておく
S = list(map(int, reversed(input())))
#print("S = ", S)
"""
「2019の倍数か」は「mod 2019 が 0 になるか」と同意。
しかし、普通に切り出して、１つずつチェックすると計算工数がかかるので、累積和を使う。
各桁の値を （桁の数値a_k）× 10^k と表現し、mod 2019 の値を予め計算し、累積和を作っておく。
ここで、10^k の部分は、2019と互いに疎（公約数がない）なので、
含めて計算しても mod 2019 の周期を乱さない。（勿論オフセットは変わる）
「(i, j) が2019で割り切るか」は、「i～j の累積和が同じ値になる（2019周期）か」と同意。
累積和が同じ値になる個数を数えて、組み合わせを計算すればよい。
"""
# a　：　各桁の mod 2019 値（桁の数値 a_k × 10^k mod 2019）
a = [0 for i in range(len(S))]
# sums ：　累積和
sums = [0 for i in range(len(S) + 1)]
for i in range(len(S)):
    a[i] = (S[i] * 10**i) % 2019
    sums[i + 1] = (sums[i] + a[i]) % 2019
#print("sums = ", sums)
sorts = sorted(sums)
cnt = 0
while len(sorts) > 0:
    picknum = len([i for i in sorts if i == sorts[0]])
    sorts = [i for i in sorts if i != sorts[0]]
    if picknum > 1:
        cnt += math.factorial(picknum) / 2
print(int(cnt))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
import math

s = input()[::-1]
length = len(s)
a = np.zeros(length)
d = np.zeros(length)

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    
x = np.array([i for i in collections.Counter(d).values()])
x = x[np.where(x > 1)]

print(int(sum([math.factorial(x[i]) for i in range(x.shape[0])]) / 2) + d.shape[0] - np.count_nonzero(d))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
import math

s = input()[::-1]
length = len(s)
a = [0] * length
d = [0] * length

for i in range(length):
    a[i] = int(s[i]) * pow(10, i) % 2019
d[0] = a[0]

for i in range(length-1):
    d[i+1] = (d[i] + a[i+1]) % 2019
    
x = np.array([i for i in collections.Counter(d).values()])
x = x[np.where(x > 1)]

print(int(sum([math.factorial(x[i]) for i in range(x.shape[0])]) / 2) + d.count(0))

Submission status: Time Limit Exceeded

Solution code:
string = str(input())         
n = 0
limit = len(string)-4
secondLimit = len(string)
size = len(string)-1
potencys = list()
potencys.append(1)
for i in range(size, 2, -1):
  intValue = int(string[i])
  potencyIndex = 0
  for j in range(i-1, -1, -1):
    potencyIndex += 1
    if potencyIndex >= len(potencys):
      potencys.append(potencys[-1]*10)
    intValue += int(string[j])* potencys[potencyIndex]
    if i-j > 2 and intValue % 673 == 0:
      n += 1
print(n)

Submission status: Time Limit Exceeded

Solution code:
string = str(input())
         
pairs = 0
limit = len(string)-4
secondLimit = len(string)
size = len(string)-1
for i in range(size, 2, -1):
  intValue = int(string[i])
  potency = 1
  for j in range(i-1, -1, -1):
    potency *= 10
    intValue += int(string[j])* potency
    if i-j > 2 and intValue % 673 == 0:
      pairs += 1
print(pairs)

Submission status: Time Limit Exceeded

Solution code:
string = str(input())
         
pairs = 0
limit = len(string)-4
secondLimit = len(string)
size = len(string)-1
for i in range(size, 2, -1):
  intValue = int(string[i])
  potency = 1
  for j in range(i-1, -1, -1):
    potency *= 10
    intValue += int(string[j])* potency
    if i-j >= 3 and intValue % 673 == 0:
      pairs += 1
print(pairs)

Submission status: Time Limit Exceeded

Solution code:
#abc164-4
s = input()

counts = [0]*2019#modの値と個数を記録するリスト
d = 0
counts[0] += 1
for i in range(0,len(s)):
    d += int(s[-i-1])*(10**i)
    #print(d, d%2019)
    counts[d % 2019] += 1
#print(counts)
sumcount = 0
for m in counts:
    sumcount += m*(m-1)/2
print(int(sumcount))
#TLE

Submission status: Time Limit Exceeded

Solution code:
#abc164-4
s = input()

counts = [0 for _ in range(2019)]#modの値と個数を記録するリスト
d = 0
counts[0] += 1
for i in range(0,len(s)):
    d += int(s[-i-1])*(10**i)
    #print(d, d%2019)
    counts[d % 2019] += 1
#print(counts)
sumcount = 0
for m in counts:
    sumcount += m*(m-1)/2
print(int(sumcount))
#TLE

Submission status: Time Limit Exceeded

Solution code:
#abc164-4
import collections
s = input()

counts = []#modの値と個数を記録するリスト

for i in range(len(s)):
    counts.append(int(s[i:]) % 2019)
counts.append(0)
c = collections.Counter(counts)
mlist = list(c.values())
sumcount = 0
for m in mlist:
    sumcount += m*(m-1)/2
print(int(sumcount))

Submission status: Time Limit Exceeded

Solution code:
s = input()
table = [0 for i in range(2019)]
table[0] = 1
num = 0
d = 1
for c in s[::-1]:
    num += (int(c) * d)
    num %= 2019
    d *= 10
    table[num] += 1

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


def count_multi(s, d):
  a = np.array([int(c) for c in s], dtype=np.uint16)
  # assume d >= 10
  m = a
  # s not include 0
  count = 0
  while len(m) > 1:
    a = a[1:]
    m = (m[:-1]*10 + a) % d
    count += np.count_nonzero(m == 0)
  return count

print(count_multi(input().strip(), 2019))

Submission status: Time Limit Exceeded

Solution code:
s = input()
table = [0 for i in range(len(s)+1)]
i = 1
for c in s[::-1]:
    table[i] = ((10 ** (i-1)) * int(c) + table[i-1]) % 2019
    i += 1

ans =  len(s) + 1 - len(set(table))
print(ans)

Submission status: Time Limit Exceeded

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

ct = 0
for i in range(n+1):
  for j in range(i+1,n+1):
    ss = int(s[i:j])
    q = ss // 2019
    m = ss % 2019

    if m == 0 and q > 0:
      ct += 1
     # print(f"{ss}, {q}, {m}")

print(ct)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

import sys
import math
import collections
import itertools
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

S = input()
N = len(S)
P = 2019

A = [0] * (N+1)
for i in range(N):
    A[i+1] = (A[i] + int(S[N-1 - i]) * (10**i)) % P

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

print(ans)

Submission status: Time Limit Exceeded

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

cnts = [1] + [0] * (2019 - 1)
tmp = 0
d = 1

for i in s:
    tmp += int(i) * d
    cnts[tmp % 2019] += 1
    d *= 10

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
cnt = 0
for ri in reversed(range(len(S))):
    for li in reversed(range(ri + 1)):
        s = S[li : ri + 1]
        #print("li = {}, ri = {}, s = {}".format(li, ri, s))
        if int(s) % 2019 == 0:
            #print("count ++ ")
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def naive(s):
	cnt = 0
	for i in range(len(s)):
		for j in range(i + 1, len(s) + 1):
			k = int(s[i:j])
			if k % 2019 == 0 and k != 0:
				#print(i, j)
				cnt += 1
	return cnt

def main(s):
	nums = [0]
	cur = 0
	for i in range(len(s)):
		cur = (cur + int(s[len(s) - 1 - i]) * 10 ** i) % 2019
		nums.append(cur)
	d = dict(collections.Counter(nums))
	a = [val for key, val in d.items() if val >= 2]
	return sum([comb(x, 2) for x in a])	

s = input()
print(main(s))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import collections
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def naive(s):
	cnt = 0
	for i in range(len(s)):
		for j in range(i + 1, len(s) + 1):
			k = int(s[i:j])
			if k % 2019 == 0 and k != 0:
				#print(i, j)
				cnt += 1
	return cnt

def main(s):
	nums = [(int(s[len(s) - i - 1]) * (10 ** i)) % 2019 for i in range(len(s))]
	nums = [0] + [x % 2019 for x in np.cumsum(nums)]
	d = dict(collections.Counter(nums))
	a = [val for key, val in d.items() if val >= 2]
	return sum([comb(x, 2) for x in a])

s = input()
print(main(s))