Problem:
Score : 500 points

Problem Statement

Given are a sequence of N positive integers A_1, A_2, \ldots, A_N, and a positive integer K.

Find the number of non-empty contiguous subsequences in A such that the remainder when dividing the sum of its elements by K is equal to the number of its elements. We consider two subsequences different if they are taken from different positions, even if they are equal sequences.

Constraints

All values in input are integers.

1 \leq N \leq 2\times 10^5

1 \leq K \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print the number of subsequences that satisfy the condition.

Sample Input 1

5 4
1 4 2 3 5

Sample Output 1

4

Four sequences satisfy the condition: (1), (4,2), (1,4,2), and (5).

Sample Input 2

8 4
4 2 4 2 4 2 4 2

Sample Output 2

7

(4,2) is counted four times, and (2,4) is counted three times.

Sample Input 3

10 7
14 15 92 65 35 89 79 32 38 46

Sample Output 3

8

Solution in Go (1.6) (judge verdict: Runtime Error)

package main
 
import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)
 
var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt() int {
  n, _ := strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, "\n"))
}
func Output() {
  _ = writer.Flush()
}
 
func Max(x, y int) int {
  if x < y { return y }
  return x
}
func Mod(n, p int) int {
  if n < 0 {
    n += (-n / p + 1) * p
  }
  return n % p
}

func main() {
  NK := NextIntVec()
  _, K := NK[0], NK[1]
  A := NextIntVec()
  cnt := 0
  P := make([][]int, K)
  for i := range P {
    P[i] = make([]int, 0)
  }
  P[0] = append(P[0], 0)
  p := 0
  for i, a := range A {
    p = (p + a - 1) % K
    P[p] = append(P[p], i + 1)
  }
  for _, p := range P {
    s, t := 0, 0
    for t < len(p) {
      for p[t] - p[s] < K {
        t++
        if t == len(p) { break }
      }
      if t == len(p) {
        cnt += (t - s) * (t - s - 1) / 2
        break
      }
      for K <= p[t] - p[s] {
        cnt += t - s - 1
        s++
      }
    }
  }
  Write(cnt)
  Output()
}