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 C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <stdlib.h>

/* --- ring */

typedef struct ring {
    int val;
    struct ring *next, *prev;
} *ring_t;

ring_t ring_unshift (int val, ring_t ring) {
    ring_t cell = (ring_t)malloc(sizeof(struct ring));
    cell->val = val;

    if (!ring) {
        cell->next = cell;
        cell->prev = cell;
    } else {
        cell->next = ring;
        cell->prev = ring->prev;
        ring->prev = cell;
        cell->prev->next = cell;
    }

    return cell;
}

ring_t ring_pop (ring_t ring) {
    if (ring->next != ring) {
        ring->prev = ring->prev->prev;
        ring->prev->next = ring;
        return ring;
    } else {
        return NULL;
    }
}

/* --- skiplist */

typedef struct list {
    int id, count;
    ring_t ring;
    struct list* next[4];
} *list_t;

list_t res_buffer[4];

void list_search_prev_node (int id, list_t list) {
    int level = 3;

    if (!list) {
        res_buffer[3] = res_buffer[2] = res_buffer[1] = res_buffer[0] = NULL;
        return;
    }

    while (1) {
        if (list->next[level] && list->next[level]->id > id) list = list->next[level];
        else if (level) res_buffer[level--] = list;
        else { res_buffer[0] = list; break; };
    }
}

list_t list_add (int id, list_t list) {
    list_t cell = (list_t)calloc(1, sizeof(struct list));
    cell->id = id;

    if (!list) return cell;

    int level;
    switch (rand() % 8) {
      case 0: case 1: case 2: case 3:
        level = 0;
        break;
      case 4: case 5:
        level = 1;
        break;
      case 6:
        level = 2;
        break;
      default:
        level = 3;
        break;
    }

    list_search_prev_node(id, list); /* needs sentinel */
    for (int i = 0; i <= level; i++) {
        cell->next[i] = res_buffer[i]->next[i];
        res_buffer[i]->next[i] = cell;
    }

    return list;
}

list_t list_find_cell (int id, list_t list) {
    list_search_prev_node(id, list);
    if (res_buffer[0]->next[0] && res_buffer[0]->next[0]->id == id) return res_buffer[0]->next[0];
    return NULL;
}

void unshift_to_cell (int index, list_t cell) {
    cell->count++;
    cell->ring = ring_unshift(index, cell->ring);
}

void pop_from_cell_le (int index, list_t cell) {
    while (cell->ring && cell->ring->prev->val <= index) {
        cell->count--;
        cell->ring = ring_pop(cell->ring);
    }
}

void test () {
    list_t list = NULL, cell;

    list = list_add(-1, list);  /* sentinel */
    list = list_add(20, list);

    cell = list_find_cell(20, list);
    unshift_to_cell(-1, cell);
    unshift_to_cell(5, cell);
    unshift_to_cell(6, cell);
    unshift_to_cell(7, cell);
    printf("%d == 4\n", cell->count);

    pop_from_cell_le(6, cell);
    printf("%d == 1\n", cell->count);

    pop_from_cell_le(100, cell);
    printf("%d == 0\n", cell->count);

    unshift_to_cell(5, cell);
    unshift_to_cell(6, cell);
    unshift_to_cell(7, cell);
    printf("%d == 3\n", cell->count);

    list = list_add(0, list);
    list = list_add(100, list);

    cell = list_find_cell(0, list);
    unshift_to_cell(5, cell);
    printf("%d == 1\n", cell->count);

    cell = list_find_cell(100, list);
    unshift_to_cell(5, cell);
    printf("%d == 1\n", cell->count);

    cell = list_find_cell(20, list);
    printf("%d == 3\n", cell->count);
}

/* --- */

int n, k, a[100001], count;

int main (void) {
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);

    /*  sum */
    a[0] = (a[0] - 1) % k;
    if (a[0] < 0) a[0] += k;
    for (int i = 1; i < n; i++){
        a[i] = (a[i - 1] + a[i] - 1) % k;
        if (a[i] < 0) a[i] += k;
    }

    /* solve */

    list_t list = list_add(0, list_add(-1, NULL));
    /* unshift_to_cell(-1, list_find_cell(0, list)); */

    for (int i = 0; i < n; i++) {
        list_t cell = list_find_cell(a[i], list);
        if (!cell) {
            list_add(a[i], list);
            cell = list_find_cell(a[i], list);
        }
        /* pop_from_cell_le(i - k, cell); */
        /* if (cell->count) count += cell->count; */
        /* unshift_to_cell(i, cell); */
    }

    printf("%d\n", count);
}