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.

Solution in C# (Mono-mcs 6.8.0.105) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        // 入力
var S = Console.ReadLine();
var N = S.Length;
 
// 逆順の数列にしておく
var A = S.Select(c => c - '0').Reverse().ToArray();
// 10のk乗を計算しておく
var pow = new int[N];
pow[0] = 1;
for (int i = 1; i < N; i++)
{
    pow[i] = pow[i - 1] * 10;
    pow[i] %= 2019;
}
 
// 元の文字列の右からi桁の10進法での数を計算し2019の余りをとる
var X = new long[N + 1];
for (int i = 0; i < N; i++)
{
    X[i + 1] = X[i] + A[i] * pow[i];
    X[i + 1] %= 2019;
}
 
// 余りが等しいものから2つ選ぶ組み合わせの数を求める
var ans = 0L;
foreach (var g in X.GroupBy(x => x))
{
    // nC2 = N*(N-1)/2
    ans += (long)g.Count() * (g.Count() - 1) / 2;
}
Console.WriteLine(ans);

    }


}