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: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define F(i,a,n) for(int i=a; i<n; i++)

int compare(const void *b1, const void *b2){ // 昇順
  if(*(int*)b1 > *(int*)b2) return 1;
  else if(*(int*)b1 < *(int*)b2) return -1;
  else return 0;
}

int main(){
  char cs[20010];
  scanf("%s",cs);
  int n=strlen(cs);
  int a[n];
  F(i,0,n){
    char ss[2];
    strncpy(ss,cs+i,1);
    a[i]=atoi(ss);
  }
  int tmod[n+1];
  int t10[n+1];
  tmod[n]=0;
  t10[n]=1;
  for(int i=n-1; i>=0; i--){
    if(i==n-1) t10[i]=1;
    else t10[i]=(t10[i+1]*10)%2019;
    tmod[i]=(tmod[i+1]+t10[i]*a[i])%2019;
  }
  int count[n+1];
  F(i,0,n+1){
    count[i]=0;
  }
  int iter=0;
  qsort(tmod,n+1,sizeof(int),compare);
  F(i,0,n){
    if(tmod[i]==tmod[i+1]){
      count[iter]++;
    }else{
      iter++;
    }
  }
  int ans=0;
  F(j,0,n){
    ans+=(count[j]*(count[j]+1))/2;
  }
  printf("%d\n",ans);
  return 0;
}