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# (Mono 4.6.2.0) (judge verdict: Accepted)

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

using System.Runtime.CompilerServices;
using ll=System.Int64;

public class Contest
{
	static void Swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	static void Swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) Swap1(ref a, ref b); }  // b の方が小さければ交換
	static bool Chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	static bool Chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	static T[][] InitMat<T>(long len1, long len2, T val){ T[][] m = new T[len1][]; for (int i = 0; i < m.Length; i++) m[i] = Enumerable.Repeat(val, (int)len2).ToArray(); return m; }
	static T[][][] InitMat<T>(long len1, long len2, long len3, T val){ T[][][] m = new T[len1][][]; for (int i = 0; i < m.Length; i++) m[i] = InitMat(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;

	public static void Main() {
		Solve_E();
 	}

	public static void Solve_A() {
		string s;
		MyIO.ReadMulti(out s);


		long ans = 0;

		if(s=="SUN") ans = 7;
		if(s=="MON") ans = 6;
		if(s=="TUE") ans = 5;
		if(s=="WED") ans = 4;
		if(s=="THU") ans = 3;
		if(s=="FRI") ans = 2;
		if(s=="SAT") ans = 1;



		MyIO.WriteMulti(ans);
	}
	public static void Solve_B() {
		long  n;
		MyIO.ReadMulti(out n);
		string s;
		MyIO.ReadMulti(out s);

		StringBuilder ans = new StringBuilder();
		foreach (var e in s)
		{
			ans.Append((char)(((byte)e+(byte)n-65)%26+65));
		}

		MyIO.WriteMulti(ans.ToString());
	}
	public static void Solve_C() {
		long  a,b,x;
		MyIO.ReadMulti(out a, out b, out x);

		Func<ll,ll,bool> f = (i,l)=>{
			return (a*i+b*i.ToString().Length)<=l;
		};

        long left = 0; //「index = 0」が条件を満たすこともあるので、初期値は -1
        long right = 1000000001; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

        // どんな二分探索でもここの書き方を変えずにできる！
        while (right - left > 1) {
            long mid = left + (right - left) / 2;

            if (!f(mid,x)) right = mid;
            else left = mid;
        }




		MyIO.WriteMulti(left);
	}
	public static void Solve_D() {
		long  n;
		MyIO.ReadMulti(out n);
		VT<ll,ll>[] abl;
		MyIO.ReadLines(n-1,out abl);

		ll[] color = new ll[n-1];
		ll cmax = 0;
		ll root = -1;

		List<VT<ll,ll>>[] tree = new List<VT<ll, ll>>[n];
		for (int i = 0; i < n-1; i++)
		{
			if(tree[abl[i].Item1-1]==null)tree[abl[i].Item1-1]=new List<VT<ll, ll>>();
			if(tree[abl[i].Item2-1]==null)tree[abl[i].Item2-1]=new List<VT<ll, ll>>();
			tree[abl[i].Item1-1].Add(new VT<ll, ll>(abl[i].Item2-1,i));
			tree[abl[i].Item2-1].Add(new VT<ll, ll>(abl[i].Item1-1,i));
		}

		for (int i = 0; i < n; i++)
		{
			if(cmax<tree[i].Count){
				cmax = tree[i].Count;
				root = i;
			}
		}

		Func<ll,ll,ll,bool> dfs = null;
		dfs = (e,pi,pc)=>{
			if(e==pi) return false;
			ll cc = 1;
			foreach (var i in tree[e])
			{
				if(i.Item1==pi) continue;
				if(cc==pc) cc++;
				color[i.Item2] = cc;
				dfs(i.Item1,e,cc);

				cc++;
			}

			return true;
		};

		dfs(root,-1,-1);

		MyIO.WriteMulti(cmax);
		MyIO.WriteLines(color);


	}
	public static void Solve_E() {
		long  n, k;
		MyIO.ReadMulti(out n, out k);
		ll[] a;
		MyIO.ReadArray(out a);

		ll[] b = new ll[n+1];
		b[0] = 0;
		for (int i = 0; i < n; i++)
		{
			b[i+1] = (b[i] + a[i] - 1) % k ;
		}

		ll ans = 0;
		if(n<k) {
			Dictionary<ll,ll> d = new Dictionary<ll, ll>();
			for (int i = 0; i < n+1; i++)
			{
				if(!d.ContainsKey(b[i])) d[b[i]]=0;
				d[b[i]]++;
			}
			foreach (var kv in d)
			{
				ans += kv.Value*(kv.Value-1)/2;
			}
		}else{
			ll[] segcnt = new ll[k];
			for (int i = 0; i < n+1; i++)
			{
				if(k<=i) segcnt[b[i-k]]--;
				ans += segcnt[b[i]];
				segcnt[b[i]]++;
			}
		}


		MyIO.WriteMulti(ans);
	}
	public static void Solve_F() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;

		MyIO.WriteMulti(ans);
	}


















}




















#region MyIO

public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) =>
		eq<T, int>()    ? ct<T, int>(int.Parse(s))
	  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T, char>()   ? ct<T, char>(s[0])
						: ct<T, string>(s);
			
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(long n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(long n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(long n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(long n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(long n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}


}

#endregion