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++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

char od[200020];
char buf[200020];

void keta(int idx, int adv)
{
  while(adv>0){
    int tmp = adv % 10;
    buf[idx] += tmp;
    adv /= 10;
    if( buf[idx] >= 10 ){
      adv += (buf[idx] / 10);
      buf[idx] = (buf[idx] % 10);
    }
    idx--;
  }
}

/*
void pketa(int idx)
{
  for(int i=0;i<idx;i++){
    cout << (int)(buf[i]);
  }
  cout<<endl;
}
*/

int main()
{
  int n;
  string s;
  
  cin >> s;
  n = 0;
  for(int i=0;i<s.length();i++){
    od[i] = s[i] - '0';
    if( '1'<=s[i] && s[i]<='9' ) n++;
  }
  
  /*
  for(int i=0;i<n;i++){
    cout << i << ":" << (int)(od[i]) << endl;
  }
  */
  
  ll rui[n+1];
  rui[0] = 0;
  for(int i=0;i<n;i++){
    rui[i+1] = rui[i] + od[i];
  }
  
  ll total = 0;
  for(int i=n-1;i>=3;i--){
    memset(buf,0,10);
    memcpy(buf+10,od,i+1);
    for(int j=i-3;j>=0;j--){
      ll tri = rui[i+1] - rui[j+1-1];
      int tmp = 1000 * (int)(buf[10+j]) + 100 * (int)(buf[11+j]) + 10 * (int)(buf[12+j]) + (int)buf[13+j];
      // cout << j << ":" << i << ":" << tri << ":" << tmp << endl;
      
      if( ( tri % 3 == 0 ) && ( tmp % 673 == 0 ) ){
        total ++;
      }
      tmp = 202 * buf[10+j+3];
      keta(10+j+2,tmp);
      // pketa(10+j+2);
    }
  }
  
  cout << total << endl;
  return 0;
}