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# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

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

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {

 	
			var inputArray = retreiveStringArray();
			var inputCount = 1;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var S = inputArray[0];
			var inputList = new List<int>();
			var N = S.Length;

			for (int i = 0; i < N; i++) {
				inputList.Add(int.Parse(S[i].ToString()));
			}

			if (N < 4) {
				Console.WriteLine(0);
				return;
			}

			 _count = 0;
			for (int i = 0; i <= N-4; i++) {
				var first = inputList[i] * 1000 + inputList[i + 1] * 100 + inputList[i + 2] * 10 + inputList[i + 3];
				var mod = first % 2019;
				if (mod == 0) {
					_count++;
				}
				if(i+4 <= N - 1) {
					nextString(mod, i + 4, inputList, N);
				}
			}

			//答えの出力
			var ans = _count;
			Console.WriteLine(ans);

		}

		/// <summary>
		/// 右に文字を足していったとき、何回2019で割れるか
		/// </summary>
		/// <param name="mod"></param>
		/// <param name="currentPosition"></param>
		/// <param name="S"></param>
		/// <param name="N"></param>
		private static void nextString(int mod, int currentPosition,List<int> inputList, long N) {

			mod = mod * 10;
			var curretNum = inputList[currentPosition];
			var newMod = (mod + curretNum) % 2019;

			if (newMod == 0) {
				_count++;
			}

			if(currentPosition < N - 1) {
				nextString(newMod, currentPosition + 1, inputList, N);
			}
			return;
		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}

		private static long _count;
	}
}