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-csc 3.5.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;

namespace abc164
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = Console.ReadLine();
            var result = 0L;
            var pList = new Dictionary<int, List<long>>();
            for (var i = 0; i < s.Length - 3; ++i)
            {
                var n = int.Parse(s.Substring(i, 3));
                var j = i + 3;
                while (j < s.Length)
                {
                    n = (n * 10 + (s[j] - '0')) % 2019;
                    ++j;
                    if (n == 0)
                    {
                        add(pList, i, j);
                        break;
                        //++result;
                    }
                }
            }
            foreach (var rList in pList.Values)
            {
                result += expand(rList);
            }
            Console.WriteLine(result);
        }
        static void add(Dictionary<int, List<long>> pList, int i, int j)
        {
            //Console.WriteLine("call " + i + " " + j);
            var addList = new List<long>();
            if (pList.ContainsKey(i))
            {
                foreach (var p in pList[i])
                {
                    //Console.WriteLine(" add " + (p + 1));
                    addList.Add(p + 1);
                }
            }
            else
            {
                //Console.WriteLine(" add " + 1);
                addList.Add(1);
            }
            if (!pList.ContainsKey(j))
            {
                pList.Add(j, new List<long>());
            }
            pList[j].AddRange(addList);
            if (pList.ContainsKey(i))
            {
                pList[i] = new List<long>();
            }
        }
        static long expand(List<long> pList)
        {
            var result = 0L;
            if (pList != null)
            {
                foreach (var p in pList)
                {
                    result += p * (p + 1) / 2;
                }
            }
            return result;
        }
    }
}