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:
ans=0
s=input()
if len(s)<4:
  print(0)
  exit()
N=len(s)
L=list()
for i in range(N):
  L.append(int(s[i:])%2019)
L.append(0)
print(len(L)-len(set(L)))

Submission status: Time Limit Exceeded

Solution code:
ans=0
s=input()
if len(s)<4:
  print(0)
  exit()
N=len(s)
L=list()
for i in range(3,N):
  L.append(int(s[:i+1]))
for i in range(N-3):
  for j in range(4,i+5):
    if (L[i]%pow(10,j))%2019==0:
      ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
ans=0
s=input()
if len(s)<4:
  print(0)
  exit()
N=len(s)
for i in range(N-3):
  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:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from collections import deque
def main():
    s = input()
    n = len(s)

    amari = [0] * n
    ketaamari = 1
    for i1 in range(n):
        t = (ketaamari * int(s[-i1 -1])) % 2019
        amari[-i1-1] = t
        ketaamari = (ketaamari * 10) % 2019
    amaria = tuple(accumulate(amari))
    r = 0
    minus = 0
    for j1 in range(n):
        r += sum([(ae - minus) % 2019 == 0 for ae in amaria[j1:]])
        minus = amaria[j1]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
def main():
    s = input()
    n = len(s)

    amari = [0] * n
    ketaamari = 1
    for i1 in range(n):
        t = (ketaamari * int(s[-i1 -1])) % 2019
        amari[-i1-1] = t
        ketaamari = (ketaamari * 10) % 2019
    amaria = list(accumulate(amari))
    r = 0
    for j1 in range(n):
        r += sum([ae % 2019 == 0 for ae in amaria[j1:]])
        amaria = [ae - amaria[j1] for ae in amaria]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    amari = [0] * n
    for i1 in range(n):
        amari[i1] = int(s[i1:]) % 2019
    r = 0
    for j1 in range(n):
        r += amari[:-j1-1].count(0)
        amari = [(i - amari[-j1 - 1]) % 2019 for i in amari]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()

cnts=[0]*2019

cnts[0]=1

for i in range(len(s)):
  tmp=int(s[i:])%2019
  cnts[tmp]+=1

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

Submission status: Time Limit Exceeded

Solution code:
import copy

s = list(input())

s.reverse()
n = len(s)
MOD = 2019
m = [0] * n
msum = [0] * (n+1)
cnt = [0] * (MOD)
t = 1
for i in range(n):
    m[i] = int(s[i]) * t % MOD
    msum[i+1] = (msum[i] + m[i]) % MOD
    cnt[msum[i+1]] += 1
    t = t * 10 % MOD

ans = 0
for i in range(1,n+1):
    ans += msum[:i].count(msum[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

s = list(input())

s.reverse()
n = len(s)
MOD = 2019
m = [0] * n
msum = [0] * n
cnt = [0] * (MOD)
t = 0
for i in range(n):
    m[i] = int(s[i]) * 10**i % MOD
    t =  (t + m[i]) % MOD
    msum[i] = copy.deepcopy(t)
    cnt[t] += 1

ans = copy.deepcopy(cnt[0])
for i in range(1,MOD):
    ans += cnt[i] * (cnt[i] - 1) // 2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

s = list(input())

s.reverse()
n = len(s)
MOD = 2019
m = [0] * n
msum = [0] * (n+1)
t = 0
for i in range(n):
    m[i] = int(s[i]) * 10**i % MOD
    t =  (t + m[i]) % MOD
    msum[i+1] = copy.deepcopy(t)

cnt = 0
for i in range(1,n+1):
    cnt += msum[:i].count(msum[i])
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

s.reverse()
n = len(s)
MOD = 2019
m = [0] * n

t = 1
for i in range(n):
    m[i] = int(s[i]) * t % MOD
    t = t * 10 % MOD

cnt = 0
for cr in range(n):
    cm = 0
    for cl in range(cr, n):
        cm = (cm + m[cl]) % MOD
        if cm == 0:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

s.reverse()
n = len(s)
MOD = 2019
m = [0] * n

t = 1
for i in range(n):
    m[i] = int(s[i]) * t % MOD
    t = t * 10 % MOD

cnt = 0
for cr in range(n):
    cm = 0
    for cl in range(cr, n):
        cm = (cm + m[cl]) % MOD
        if cm == 0:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

s = input()

n = len(s)
c = 0
for i in range(n):
    t=0
    j=0
    while 1:
        if i+j>=n:
            break
        t = t*10+int(s[i+j])
        if int(s[i+j])==0 :
            continue
        t %=2019
        if t==0 :
            c+=1
            j+=672
        j+=1
print(c)

Submission status: Time Limit Exceeded

Solution code:

s = input()

n = len(s)
c = 0
for i in range(n):
    t=0
    for j in range (n):
        if i+j>=n:
            break
        t = t*10+int(s[i+j])
        if int(s[i+j])==0 :
            continue
        t %=2019
        if t==0 :
            c+=1
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
mod = 2019

a = input()
n = len(a)
x = [0] * n
for i in range(n):
    x[n - i - 1] = int(a[i])

ans = 0
cnt = [0] * mod
cnt[0] = 1
s = [0] * (n + 1)
e = 1

for i in range(n):
    s[i + 1] = ((x[i] * e) % mod + s[i]) % mod
    # print("i = ", i, "s[i + 1] = ", s[i + 1])
    ans += cnt[s[i + 1]]
    cnt[s[i + 1]] += 1
    e *= 10

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
def main():
    S = input()
    A = [0] * ((n := len(S)) + 1)
    A[1] = (a := int(S[-1])) % 2019
    for i in range(2, n + 1):
        a += pow(10, i - 1) * int(S[-i])
        A[i] = a % 2019
    c = Counter(A)
    ans = 0
    for v in c.values():
        ans += v * (v - 1) // 2
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    S = input()
    n = len(S)
    i, j = 0, 4
    ans = 0
    while i <= n - 4:
        if eval(S[i:j] + ' % 2019 == 0'):
            ans += 1
        if j < n:
            j += 1
        else:
            i += 1
            j = i + 4
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s ="0"+ input()

nagasa=len(s)
ans=0

for i in range(1,nagasa):
    if nagasa-i<4:
        break
    for k in range(nagasa,1,-1):
        if k-i<4:
            break
        if int(s[i:k])%2019==0:
            ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s ="0"+ input()

nagasa=len(s)
ans=0

import itertools
for seq in itertools.combinations(range(1,nagasa+1),2):
    num=int(s[seq[0]:seq[1]])
    if num%2019==0:
        ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
s="0"+s
num="0"
ans=0

for i in range(1,len(s)):
    for j in range(i,len(s)):
        num="0"
        for k in range(i,j+1):
            num+=s[k]
        if int(num)%2019==0:
            ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()

l = len(s)
r_set = set()
num = 0
count = 0
for i in range(l - 1, -1, -1):
  num += int(s[i]) * 10 ** (l - i - 1)
  #print(num)
  r = num % 2019
  #print(r)
  num = r
  if r in r_set or r == 0:
    count += 1
    continue
  if r not in r_set:
    r_set.add(r)
print(count)

Submission status: Time Limit Exceeded

Solution code:
s = input()

l = len(s)
r_set = set()
num = 0
count = 0
for i in range(l - 1, -1, -1):
  num += int(s[i]) * 10 ** (l - i - 1)
  #print(num)
  r = num % 2019
  #print(r)
  if r in r_set or r == 0:
    count += 1
    continue
  if r not in r_set:
    r_set.add(r)
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#! python3
# coding:utf-8
 
def main():
    s = input()
    cnt = 0
    
    for i in range(1,len(s)):
        for j in range(i+3,len(s)+1):
            if int(s[i-1:j])%2019 == 0:
                cnt+=1
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
count = 0
DP = [[None for i in range(N)] for i in range(N)]
if N <= 3: print(0) 
else:
    DP[3][0] = int(S[0:4]) % 2019
    if DP[3][0] == 0:
        count += 1
    for i in range(4, N):
        for j in range(0, i-3):
            if DP[i-1][j] != None:
                DP[i][j] = (DP[i-1][j]*10 + int(S[i])) % 2019
                if DP[i][j] == 0:
                    count += 1
            else:
                DP[i][j] = int(S[i-3:i+1]) % 2019
                if DP[i][j] == 0:
                    count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
cnt = 0
base = 0
digits_base = 0
MOD = 2019
s = input().rstrip()

for d in s:
    digits_base += 1
    digits_tmp = digits_base
    base *= 10
    base += int(d)
    tmp = base
#     print(base)
    while tmp:
#         print(tmp)
        if tmp % MOD == 0:
            cnt += 1
        tmp %= 10 ** (digits_tmp-1)
        digits_tmp -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ls = len(s)
print(
    [
        sum(
            sum(int(s[j : j + i]) % 2019 == 0 for j in range(ls - i + 1))
            for i in range(5, ls)
        ),
        0,
    ][ls < 5]
)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

T=[0]
for i in range(N):
    p = int(S[-1-i])
    T.append( (T[-1]+(p*(10**i))%2019)%2019 )
    
from collections import Counter
ans=0
U=dict(Counter(T))
for num in U:
    ans += U[num]*(U[num]-1)//2
print(ans)

Submission status: Time Limit Exceeded

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

T=[0]
for i in range(N):
    p = int(S[-1-i])
    T.append( (T[-1]+(p*(10**i))%2019)%2019 )
    
from collections import Counter
ans=0
U=dict(Counter(T))
for num in U:
    ans += U[num]*(U[num]-1)//2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = list(map(int,input()))
t = len(s)
mod = 2019

import numpy as np
arr = [0] * t
arr = np.array(arr)
arr[0] = s[0]
for i in range(1,t):
    arr[i] = (arr[i-1] * 10 + s[i]) % mod

powarr = [1] * t
for i in range(1,t):
    powarr[i] = powarr[i-1]*10 % mod

ans = list(arr).count(0)
for i in range(1,t):
    tmp = (arr[i:] - powarr[i]*arr[:t-i]) % mod
    ans += list(tmp).count(0)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import pdb; pdb.set_trace()

s = list(map(int,input()))
t = len(s)
mod = 2019

import numpy as np
arr = [0] * t
arr = np.array(arr)
arr[0] = s[0]
for i in range(1,t):
    arr[i] = (arr[i-1] * 10 + s[i]) % mod

powarr = [pow(10,i,mod) for i in range(t)]

ans = list(arr).count(0)
for i in range(1,t):
    tmp = (arr[i:] - powarr[i]*arr[:t-i]) % mod
    ans += list(tmp).count(0)


print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


def main():
  # S = input()[-1:0:-1]
  S = input()[::-1]
  MOD = 2019

  # dp = [0]*(len(S)+1)
  # dp[0] = 0
  dic = [0]*2019
  dic[0] = 1

  ten = 1
  pre = 0
  for i, s in enumerate(S):
    # dp[i] = (dp[i-1] + ten*int(s)) % MOD
    pre = (pre + ten*int(s)) % MOD
    dic[pre] += 1
    ten *= 10

  sum_ = 0
  # for sup in dp:
  #   dic[sup] += 1

  for v in dic:
    sum_ += v*(v-1)//2

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

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


def main():
  # S = input()[-1:0:-1]
  S = input()[::-1]
  MOD = 2019

  dp = [0]*(len(S)+1)
  dp[0] = 0

  ten = 1
  for i, s in enumerate(S):
    dp[i] = (dp[i-1] + ten*int(s)) % MOD
    ten *= 10

  dic = [0]*2019
  sum_ = 0
  for sup in dp:
    dic[sup] += 1

  for v in dic:
    sum_ += v*(v-1)//2

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

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


def main():
  # S = input()[-1:0:-1]
  S = input()[::-1]
  MOD = 2019

  dp = [0]*(len(S)+1)
  dp[0] = 0

  ten = 1
  for i, s in enumerate(S):
    dp[i] = (dp[i-1] + ten*int(s)) % MOD
    ten *= 10

  dic = defaultdict(int)
  sum_ = 0
  for sup in dp:
    if sup in dic:
      sum_ += dic[sup]
    dic[sup] += 1

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from sys import stdin
input = stdin.readline


def main():
  S = input()[:-1]
  MOD = 2019

  dp = [0]*(len(S)+1)
  dp[0] = 0

  ten = 1
  for i in range(1, len(S)+1):
    dp[i] = (dp[i-1] + ten*int(S[-i])) % MOD
    ten *= 10

  dic = defaultdict(int)
  sum_ = 0
  for sup in dp:
    if sup in dic:
      sum_ += dic[sup]
    dic[sup] += 1

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from sys import stdin
input = stdin.readline


def main():
  S = input()[:-1]
  MOD = 2019

  dp = [0]*(len(S)+1)
  dp[0] = 0

  for i in range(1, len(S)+1):
    dp[i] = (dp[i-1] + (10**(i-1))*int(S[-i])) % MOD

  dic = defaultdict(int)
  sum_ = 0
  for sup in dp:
    if sup in dic:
      sum_ += dic[sup]
    dic[sup] += 1

  print(sum_)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = []
for s in S:
    l.append(int(s))

cnt = 0
for L in range(len(l)):
    sum = 0
    for R in range(L,len(l)):
        sum *= 10
        sum += l[R]
        if sum % 2019 == 0:
            #print(L+1,R)
            cnt += 1
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
S = input()
l = []
for s in S:
    l.append(int(s))

cnt = 0
for L in range(len(l)):
    for R in range(L+1,len(l)+1):
        sum = 0
        for i in range(L,R):
            sum *= 10
            sum += l[i]
        if sum % 2019 == 0:
            #print(L+1,R)
            cnt += 1
            break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = input()
s_keta = len(s)
dp = [0 for i in range(s_keta+1)]
for i in range(1,s_keta+1):
  dp[i] =  (dp[i-1] + (10 ** (i-1)) * int(s[s_keta - i])) % 2019

c = collections.Counter(dp)
result = 0
for v in c.values():
  result += v * (v-1) //2
print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = input()
N = len(S)

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

r = sum(m*(m-1)//2 for m in Counter(l).values())
print(r)

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cou = [0 for i in range(n+1)]
cou[1]= s[-1]
coum = [0 for i in range(2019)]
for i in range(1,n):
  cou[i+1] = s[n-i-1]+ str(cou[i])
  a = int(cou[i+1])
  coum[a%2019] += 1
coum[0] += 1
coum[int(s[-1])]+= 1
ans = 0
for i in coum:
    ans += i*(i-1)*0.5
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cou = [0 for i in range(n+1)]
cou[1]= s[-1]
coum = [0 for i in range(2019)]
for i in range(1,n):
  cou[i+1] = s[n-i-1]+ str(cou[i])
  a = int(cou[i+1])
  coum[a%2019] += 1
coum[0] += 1
coum[int(s[-1])]+= 1
ans = 0
for i in coum:
    ans += i*(i-1)*0.5
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cou = [0 for i in range(n+1)]
cou[1]= s[-1]
for i in range(1,n):
  cou[i+1] = s[n-i-1]+ str(cou[i])
mo = [0 for i in range(n+1)]
for i in range(n+1):
  a = int(cou[i])
 # print(a)
  mo[i] = a%2019
cou = [0 for i in range(2019)]
for i in range(n+1):
  cou[mo[i]] += 1
ans = 0
for i in cou:
    ans += i*(i-1)*0.5
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
s = input()
n = len(s)
cou = [0 for i in range(n+1)]
for i in range(n):
  cou[i+1] = s[n-i-1:]
mo = [0 for i in range(n+1)]
for i in range(n+1):
  a = int(cou[i])
  mo[i] = a%2019
cou = [0 for i in range(2019)]
for i in range(n+1):
  cou[mo[i]] += 1
ans = 0
for i in cou:
    ans += i*(i-1)*0.5
print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
def main():
    import collections

    S = input()

    mod_list = [0]+[False for i in range(len(S))]

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


    CTR_mod_list = collections.Counter(mod_list).most_common()
    cnt = 0

    for i in range(len(CTR_mod_list)):
        n = CTR_mod_list[i][1]

        if (n == 1):
            break
        
        cnt += n * (n - 1) // 2

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import collections

    S = input()

    mod_list = [0]+[False for i in range(len(S))]

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


    CTR_mod_list = collections.Counter(mod_list).most_common()
    cnt = 0

    for i in range(len(CTR_mod_list)):
        n = CTR_mod_list[i][1]
        cnt += n * (n - 1) // 2

    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections

S = input()

mod_list = [0]+[False for i in range(len(S))]

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


CTR_mod_list = collections.Counter(mod_list).most_common()
cnt = 0

for i in range(len(CTR_mod_list)):
    n = CTR_mod_list[i][1]

    if (n > 1):
        cnt += n * (n - 1) // 2

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

# ex. 1817181712114
S = input()

# 0桁目まで(=空文字列)のMODを0と考える
# 1桁目を含む数が2019の倍数の時に都合が良くなる
X = [0]

# 4,14,114,2114,12114,...と下からのmod2019を計算
for i in range(-1, -len(S) - 1, -1):
    temp = int(S[i:])
    X.append(temp % 2019)
# print(X)

C = Counter(X)
# print(C)

ans = 0
# v_C_2を計算
for v in C.values():
    ans += v * (v - 1) // 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  r += t*d
  r %= mod
  d *= 10%mod
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  r += t*d
  r %= mod
  d *= 10%mod
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in S[::-1]:
  t = int(s)%mod
  r += t*d
  r %= mod
  d *= 10%mod
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  t *= d
  r += t
  r %= mod
  d *= 10
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in S[::-1]:
  t = int(s)%mod
  t *= d
  r += t
  r %= mod
  d *= 10
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
for i,s in enumerate(S[::-1]):
  t = int(s)%mod
  t *= (10**i)%mod
  r += t
  r %= mod
  p[r] += 1

#p.append(0)
#ans = 0
#c = Counter(p)
#for k,n in c.most_common():
#  if n > 1:
#    ans += 1
#  else:break
  
print(sum([i*(i-1)//2 for i in p]))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
mod = 2019
p = [0] * len(S)
r = 0
for i,s in enumerate(S[::-1]):
  t = int(s)%mod
  t *= (10**i)%mod
  r += t
  p[i] = r%mod

p.append(0)
ans = 0
c = Counter(p)
for k,n in c.most_common():
  if n > 1:
    ans += 1
  else:break
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
mod = 2019
p = [0] * len(S)
r = 0
for i,s in enumerate(S[::-1]):
  t = int(s)%mod
  t *= (10**i)%mod
  r += t
  p[i] = r%mod

p.append(0)
ans = 0
c = Counter(p)
for k in c:
  if c[k] > 1:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in reversed(S):
  t = int(s)%mod
  r += t*d
  r %= mod
  d = d*10
  p[r] += 1

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
d = 1
for s in S[::-1]:
  t = int(s)%mod
  r += t*d
  r %= mod
  d = d*10
  p[r] += 1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
S = S + '0'
mod = 2019
p = [0] * mod
r = 0
for i,s in enumerate(S[::-1]):
  t = int(s)%mod
  d = (10**i)%mod
  r += t*d
  r %= mod
  p[r] += 1

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

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
mod = 2019
p = [0] * len(S)
r = 0
for i,s in enumerate(S[::-1]):
  t = int(s)%mod
  d = (10**i)%mod
  r += t*d
  p[i] = r%mod

p.append(0)
ans = 0
c = Counter(p)
for k,n in c.most_common():
  if n > 1:
    ans += 1
  else:break
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
mod = 2019
p = [0] * len(S)
r = 0
for i,s in enumerate(S[::-1]):
  r += (int(s) * (10**i))%mod
  p[i] = r%mod

p.append(0)
ans = 0
c = Counter(p)
for k,n in c.most_common():
  if n > 1:
    ans += 1
  else:break
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
S = input()
mod = 2019
p = [0] * len(S)
r = 0
for i,s in enumerate(S[::-1]):
  r += (int(s) * (10**i))%mod
  p[i] = r%mod

p.append(0)
ans = 0
c = Counter(p)
for k in c:
  if c[k] > 1:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
  k = len(s[i:])+1
  for j in range(i+1,n+1):
    if int(s[i:j])%2019 == 0:
      ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list -- START --
def summarizeList(l):
  sl=sorted(l)

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res
# Summarize count of factor within list --- END ---

def main():
  s=S()
  n=len(s)
  s=s[::-1]

  rl=[0]*(n+1)
  for i,x in enumerate(s):
    rl[i+1]=(int(x)*(10**i))%2019

  for i in range(n):
    rl[i+1]+=rl[i]
    rl[i+1]%=2019

  # print(rl[1:])

  sl=summarizeList(rl[1:])

  ans=0
  for x,c in sl:
    if x==0:
      ans+=c

    elif c>1:
      ans+=(c+c-1)//2

  return ans

# main()
print(main())

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

s = input()
S = [0]
n = len(s)
ans= 0
for n in range(n-1, -1, -1):
    S.append(int(s[n:])%2019)
for a, b in itertools.combinations(S, 2):
    if a == b:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def search(S):
    cnt = 0
    N = len(S)
    for i in range(N+1):
        for j in range(i+1, N+1):
            num = S[i:j]
            if int(num)%2019==0:
                cnt += 1
                i += 3
    return cnt 

if __name__ == "__main__":
    S = input()
    cnt = search(S)
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def search(S):
    cnt = 0
    N = len(S)
    for i in range(N+1):
        for j in range(i+1, N+1):
            num = S[i:j]
            if int(num)%2019==0:
                cnt += 1
            
    return cnt 

if __name__ == "__main__":
    S = input()
    cnt = search(S)
    print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
N = len(S)
cnt = 0
for i in range(0,N):
    for j in range(i+4,N+1):
        num = int(S[i:j])
        mod = num % 2019
        if mod == 0:
            cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

S = input()
n = len(S)

mod = 2019
d = defaultdict(int)
d[0] = 1
num = 0
for i in reversed(range(n)):
    num += int(S[i]) * 10 ** (n - i - 1)
    num %= mod
    d[num] += 1

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

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

S = input()
n = len(S)

mod = 2019
d = defaultdict(int)
d[0] = 1
num = 0
for i in reversed(range(n)):
    num += int(S[i]) * 10 ** (n - i - 1)
    d[num % mod] += 1

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
S = input()
remainder = [0 for _ in range(2019)]
for i in range(len(S)):
  s = int(S[-1*i:])
  remainder[s % 2019] += 1
ans = 0
for i in range(2019):
  if i == 0:
      ans += remainder[i]
  else:
    if remainder[i] > 1:
      # 組み合わせ
      ans += (math.factorial(remainder[i]) // (math.factorial(remainder[i] - 2) * math.factorial(2)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
S = input()
remainder = [0 for _ in range(2019)]
for i in range(len(S)):
  s = int(S[len(S)-1-i:])
  remainder[s % 2019] += 1
ans = 0
for i in range(2019):
  if i == 0:
      ans += remainder[i]
  else:
    if remainder[i] > 1:
      # 組み合わせ
      ans += (math.factorial(remainder[i]) // (math.factorial(remainder[i] - 2) * math.factorial(2)))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import itertools
S = input()
remainder = ["" for _ in range(2019)]
for i in range(len(S)):
  s = int(S[len(S)-1-i:])
  remainder[s % 2019] += "1"
ans = 0
for i in range(2019):
  if i == 0:
    ans += len(remainder[i])
  else:
    # 累積和
    ans += len(list(itertools.combinations(remainder[i], 2)))
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n-3):
    for j in range(i+3,n):
        m = int(''.join(s[i:j+1]))
        if m % 2019 == 0:
            p += 1

print(p)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(i,n):
        m = int(''.join(s[i:j+1]))
        if m % 2019 == 0:
            p += 1

print(p)

Submission status: Time Limit Exceeded

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

S = input().strip()
#-----
num = 0
cnt_mod = {}
mod = 2019

for i in range(len(S)):
    digit = int( S[len(S)-1-i] )
    
    num += digit * (10**i % mod)
    num %= mod
    
    remainder = num % mod
    
    cnt_mod.setdefault(remainder, 0)
    cnt_mod[remainder] += 1


ans = 0

for r,c in cnt_mod.items():
    # r: remainder
    # c: count
    if r == 0:
        ans += c
    elif c >= 2:
        ans += c*(c-1)//2

print(ans)

Submission status: Time Limit Exceeded

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

t = [0]*n
dp = [0]*2020
t[0] = int(s[-1])
dp[t[0]] += 1
for i in range(n-1):
    t[i+1] = t[i] + int(s[-2-i])*pow(10, i+1)
    t[i+1] %= mod
    dp[t[i+1]] += 1
ans = 0
for D in dp[1:]:
    ans += D*(D-1)//2
print(ans+(dp[0]+1)*(dp[0])//2)