prithivMLmods/Coder-Stat huggingface dataset:
Problem p02851 (C++)

Buggy submission (status: Runtime Error):

// E - Rem of Sum is Num

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll A[200000];
ll cumA[200001];
ll M[200001];

int main() {
  ll N, K;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 1; i <= N; i++) {
    cumA[i] = cumA[i - 1] + A[i - 1];
    M[i] = (cumA[i] - i) % K;
  }

  ll ans = 0;

  // **** debug : この方法でカウントするとTLE ****
  /*
  for (int i=0; i<N; i++) {
          for (int j=i+1; j<=N; j++) {
                  if ((j - i < K) && (M[i] % K == M[j] % K)) {
                          ans++;
                  }
          }
  }
  */

  map<ll, ll> M_cnt; // 0 < j - i < K の範囲でのMの度数分布
  // i = 0
  for (int j = 1; j < K; j++) {
    M_cnt[M[j]]++;
  }

  for (int i = 0; i < N; i++) {
    ans += M_cnt[M[i]];

    // 度数分布をスライド
    M_cnt[M[i + 1]]--;
    if (i + K <= N)
      M_cnt[M[i + K]]++;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:

// E - Rem of Sum is Num

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll A[200000];
ll cumA[200001];
ll M[200001];

int main() {
  ll N, K;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 1; i <= N; i++) {
    cumA[i] = cumA[i - 1] + A[i - 1];
    M[i] = (cumA[i] - i) % K;
  }

  ll ans = 0;

  // **** debug : この方法でカウントするとTLE ****
  /*
  for (int i=0; i<N; i++) {
          for (int j=i+1; j<=N; j++) {
                  if ((j - i < K) && (M[i] % K == M[j] % K)) {
                          ans++;
                  }
          }
  }
  */

  map<ll, ll> M_cnt; // 0 < j - i < K の範囲でのMの度数分布
  // i = 0
  for (int j = 1; j < K && j <= N; j++) {
    M_cnt[M[j]]++;
  }

  for (int i = 0; i < N; i++) {
    ans += M_cnt[M[i]];

    // 度数分布をスライド
    M_cnt[M[i + 1]]--;
    if (i + K <= N)
      M_cnt[M[i + K]]++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
map<ll, int> mp;
ll sum[N], x[N], m, n, ans, dp[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> x[i], dp[i] = dp[i - 1] + x[i];
  for (int i = 0; i <= n; i++) {
    if (i >= m)
      mp[dp[i - m]]--;
    dp[i] = (dp[i] + m - i % m) % m;
    ans += mp[dp[i]];
    mp[dp[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
map<ll, int> mp;
ll sum[N], x[N], m, n, ans, dp[N];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> x[i], dp[i] = dp[i - 1] + x[i];
  for (int i = 0; i <= n; i++) {
    if (i >= m)
      mp[dp[i - m]]--;
    dp[i] = (dp[i] + m - i % m) % m;
    ans += mp[dp[i]];
    mp[dp[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
// #define cccgift
// #define
// getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
template <typename T> inline void read(T &x) {
  static char ch;
  bool f = 1;
  for (x = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 0;
  for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar())
    ;
  x = f ? x : -x;
}
template <typename T> void print(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char ap) {
  print(x);
  if (ap)
    putchar(ap);
}
template <typename T> inline void chkmax(T &x, const T &y) {
  x = x < y ? y : x;
}
template <typename T> inline void chkmin(T &x, const T &y) {
  x = x < y ? x : y;
}
map<int, int> mp;
int n, k;
ll a[100001], tot;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (res i = 1; i <= n; ++i)
    cin >> a[i], a[i] += a[i - 1];
  mp[0] = 1;
  for (res i = 1; i <= n; ++i) {
    if (i >= k)
      --mp[(a[i - k] % k - i % k + k) % k];
    tot += mp[(a[i] % k - i % k + k) % k], ++mp[(a[i] % k - i % k + k) % k];
  }
  cout << tot << '\n';
  cout << flush;
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?), set tle
 * do something instead of nothing and stay organized
 */

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
// #define cccgift
// #define
// getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
template <typename T> inline void read(T &x) {
  static char ch;
  bool f = 1;
  for (x = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 0;
  for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar())
    ;
  x = f ? x : -x;
}
template <typename T> void print(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char ap) {
  print(x);
  if (ap)
    putchar(ap);
}
template <typename T> inline void chkmax(T &x, const T &y) {
  x = x < y ? y : x;
}
template <typename T> inline void chkmin(T &x, const T &y) {
  x = x < y ? x : y;
}
map<int, int> mp;
int n, k;
ll a[200001], tot;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (res i = 1; i <= n; ++i)
    cin >> a[i], a[i] += a[i - 1];
  mp[0] = 1;
  for (res i = 1; i <= n; ++i) {
    if (i >= k)
      --mp[(a[i - k] % k - i % k + k) % k];
    tot += mp[(a[i] % k - i % k + k) % k], ++mp[(a[i] % k - i % k + k) % k];
  }
  cout << tot << '\n';
  cout << flush;
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?), set tle
 * do something instead of nothing and stay organized
 */
Problem p02851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int x;
  vector<int> sum(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    x %= k;
    sum[i + 1] = (sum[i] + x) % k;
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    for (int l = 1; i + l <= n; l++) {
      if (l == (sum[i + l] - sum[i] + k) % k)
        res++;
    }
  }
  cout << res << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int x;
  vector<int> sum(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> x;
    x %= k;
    sum[i + 1] = (sum[i] + x) % k;
  }
  unordered_map<int, int> m;
  long long res = 0;
  for (int i = 0; i <= n; i++) {
    sum[i] = ((sum[i] - i) % k + k) % k;
    if (i >= k)
      m[sum[i - k]]--;
    res += m[sum[i]];
    m[sum[i]]++;
  }
  cout << res << '\n';
  return 0;
}
Problem p02851 (Python)

Buggy submission (status: Time Limit Exceeded):
n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    s[r] = s.get(r, []) + [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
r = 0
s = {0: [0]}
for i in range(n):
    r = (r + a[i] - 1) % k
    if r in s:
        s[r].append(i + 1)
    else:
        s[r] = [i + 1]
ret = 0
for key in s:
    a = s[key]
    en = 0
    for st in range(len(a)):
        while en < len(a) and a[en] - a[st] < k:
            en += 1
        ret += en - st - 1
print(ret)
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i) {
    --a[i];
    a[i] %= k;
  }
  vector<int> r(n + 1, 0);
  for (int i = 0; i < n; ++i)
    r[i + 1] = (r[i] + a[i]) % k;
  vector<int> v(k, 0);
  i64 ans = 0;
  for (int i = 0; i <= n; ++i) {
    ans += v[r[i]];
    v[r[i]]++;
    if (i - k + 1 >= 0)
      v[r[i - k + 1]]--;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

signed main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i) {
    --a[i];
    a[i] %= k;
  }
  vector<int> r(n + 1, 0);
  for (int i = 0; i < n; ++i)
    r[i + 1] = (r[i] + a[i]) % k;
  map<int, int> v;
  i64 ans = 0;
  for (int i = 0; i <= n; ++i) {
    ans += v[r[i]];
    v[r[i]]++;
    if (i - k + 1 >= 0)
      v[r[i - k + 1]]--;
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<ll, ll> m;
  vector<ll> sum(n + 1, 0);
  for (int i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + a[i];
  }
  for (int i = 0; i <= k - 1; i++) {
    ll cand = (sum[i] - i) % k;
    cand = (cand + k) % k;
    m[cand]++;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll cand = (sum[i] - i) % k;
    cand = (cand + k) % k;
    m[cand]--;
    ans += m[cand];
    if (k + i <= n) {
      ll add = (sum[k + i] - (k + i)) % k;
      add = (add + k) % k;
      m[add]++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<ll, ll> m;
  vector<ll> sum(n + 1, 0);
  for (int i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + a[i];
  }
  for (int i = 0; i <= min(n, k - 1); i++) {
    ll cand = (sum[i] - i) % k;
    cand = (cand + k) % k;
    m[cand]++;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll cand = (sum[i] - i) % k;
    cand = (cand + k) % k;
    m[cand]--;
    ans += m[cand];
    if (k + i <= n) {
      ll add = (sum[k + i] - (k + i)) % k;
      add = (add + k) % k;
      m[add]++;
    }
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &i : a)
    cin >> i, --i;
  vector<int> s(n + 1, 0);
  for (int i = 1; i <= n; ++i)
    (s[i] += s[i - 1] + a[i - 1]) %= k;
  map<int, int> m;
  int r = 0;
  for (int i = 1; i < k; ++i)
    ++m[s[i]];
  for (int i = 0; i <= n; ++i) {
    r += m[s[i]];
    if (i + k <= n)
      ++m[s[i + k]];
    if (i + 1 <= n)
      --m[s[i + 1]];
  }
  cout << r << endl;
  for (int i : s)
    cerr << i << " ";
  cerr << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int &i : a)
    cin >> i, --i;
  vector<int> s(n + 1, 0);
  for (int i = 1; i <= n; ++i)
    (s[i] += s[i - 1] + a[i - 1]) %= k;
  map<int, int> m;
  ll r = 0;
  for (int i = 1; i < min(k, n + 1); ++i)
    ++m[s[i]];
  for (int i = 0; i <= n; ++i) {
    r += m[s[i]];
    if (i + k <= n)
      ++m[s[i + k]];
    if (i + 1 <= n)
      --m[s[i + 1]];
  }
  cout << r << endl;
  for (int i : s)
    cerr << i << " ";
  cerr << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> sum(n + 1, 0);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    sum[i + 1] = (sum[i] + a[i]) % k;
  }

  vector<int> dp(k, 0);
  queue<int> q;
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    int s = sum[i];
    ans += dp[s];
    dp[s]++;
    q.push(s);
    if (q.size() == k) {
      dp[q.front()]--;
      q.pop();
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> sum(n + 1, 0);
  rep(i, n) {
    cin >> a[i];
    a[i]--;
    sum[i + 1] = (sum[i] + a[i]) % k;
  }

  map<int, int> dp;
  queue<int> q;
  ll ans = 0;
  for (int i = 0; i <= n; ++i) {
    int s = sum[i];
    ans += dp[s];
    dp[s]++;
    q.push(s);
    if (q.size() == k) {
      dp[q.front()]--;
      q.pop();
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;

  vector<long long int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= 1;
  }

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

  // solve
  long long int ans = 0;
  vector<int> rem(k);
  queue<int> q;
  for (int i = 0; i < n + 1; i++) {
    ans += rem[a[i]];
    rem[a[i]] += 1;
    q.push(a[i]);

    if (q.size() == k) {
      rem[q.front()] -= 1;
      q.pop();
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int n, k;
  cin >> n >> k;

  vector<long long int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] -= 1;
  }

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

  // solve
  long long int ans = 0;
  map<int, int> rem;
  queue<int> q;
  for (int i = 0; i < n + 1; i++) {
    ans += rem[a[i]];
    rem[a[i]] += 1;
    q.push(a[i]);

    if (q.size() == k) {
      rem[q.front()] -= 1;
      q.pop();
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  ll A[200005];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<ll> sum;
  sum.push_back(A[0]);
  for (int i = 1; i < N; i++) {
    sum.push_back(sum[sum.size() - 1] + A[i]);
  }

  ll ans = 0;
  ll tmp[200005];

  tmp[0] = 0;

  for (int i = 0; i < N; i++) {
    tmp[i + 1] = (sum[i] - (i + 1)) % K;
  }

  map<ll, ll> cnt;

  ll las = K - 1;
  for (int i = 0; i <= las; i++) {
    cnt[tmp[i]]++;
  }

  for (int i = 0; i <= N; i++) {
    ans += cnt[tmp[i]] - 1;
    // cout << cnt[tmp[i]] << endl;
    cnt[tmp[i]]--;
    las++;
    if (las <= N) {
      cnt[tmp[las]]++;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N, K;
  cin >> N >> K;

  ll A[200005];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  vector<ll> sum;
  sum.push_back(A[0]);
  for (int i = 1; i < N; i++) {
    sum.push_back(sum[sum.size() - 1] + A[i]);
  }

  ll ans = 0;
  ll tmp[200005];

  tmp[0] = 0;

  for (int i = 0; i < N; i++) {
    tmp[i + 1] = (sum[i] - (i + 1)) % K;
  }

  map<ll, ll> cnt;

  ll las = K - 1;
  for (int i = 0; i <= min(las, N); i++) {
    cnt[tmp[i]]++;
  }

  for (int i = 0; i <= N; i++) {
    ans += cnt[tmp[i]] - 1;
    // cout << cnt[tmp[i]] << endl;
    cnt[tmp[i]]--;
    las++;
    if (las <= N) {
      cnt[tmp[las]]++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> m;
const int N = 1e5;
ll sum[N + 100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  ll ans = 0;
  cin >> n >> k;
  m[0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    sum[i] = (sum[i - 1] + a) % k;
    ans += (m[(sum[i] - (i) % k + k) % k]);
    m[(sum[i] - (i) % k + k) % k]++;
    if (i - k + 1 >= 0) {
      m[(sum[i - k + 1] - (i - k + 1) % k + k) % k]--;
    }
  }
  if (k == 1) {
    cout << "0\n";
  } else
    cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> m;
const int N = 2e5;
ll sum[N + 100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  ll ans = 0;
  cin >> n >> k;
  m[0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    sum[i] = (sum[i - 1] + a) % k;
    ans += (m[(sum[i] - (i) % k + k) % k]);
    m[(sum[i] - (i) % k + k) % k]++;
    if (i - k + 1 >= 0) {
      m[(sum[i - k + 1] - (i - k + 1) % k + k) % k]--;
    }
  }
  if (k == 1) {
    cout << "0\n";
  } else
    cout << ans << "\n";
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, k, ans = 0, su = 0;
  cin >> n >> k;
  vector<ll> a(k, 0), b(n), r(n);
  rep(i, n) {
    r[i] = su;
    cin >> b[i];
    b[i] %= k;
    ll u = (b[i] - 1 + k) % k;
    if (i + 1 >= k)
      a[(k - r[i + 1 - k]) % k]--;
    if (u == 0)
      ans++;
    ans += a[abs(2 * k - u - su) % k];
    a[(k - su) % k]++;
    su = (su + u) % k;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, k, ans = 0, su = 0;
  cin >> n >> k;
  map<ll, ll> a;
  vector<ll> b(n), r(n);
  rep(i, n) {
    r[i] = su;
    cin >> b[i];
    b[i] %= k;
    ll u = (b[i] - 1 + k) % k;
    if (i + 1 >= k)
      a[(k - r[i + 1 - k]) % k]--;
    if (u == 0)
      ans++;
    ans += a[abs(2 * k - u - su) % k];
    a[(k - su) % k]++;
    su = (su + u) % k;
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map<int, int> mm;
int pre[200005];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
    pre[i] = (pre[i - 1] + a[i]) % k;
    // cout<<pre[i]<<" ";
  }

  int ans = 0;
  mm[0]++;
  for (int i = 1; i <= k - 1; i++) {
    ans += mm[pre[i]];
    mm[pre[i]]++;
  }
  for (int i = k; i <= n; i++) {
    mm[pre[i - k]]--;
    ans += mm[pre[i]];
    mm[pre[i]]++;
  }

  cout << ans;

  // -------------------------------------Code ends
  // here------------------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}

Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map<int, int> mm;
int pre[200005];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //  #ifndef ONLINE_JUDGE
  //     freopen ("INPUT.txt" , "r" , stdin);
  //     freopen ("OUTPUT.txt" , "w" , stdout);
  //  #endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------

  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
    pre[i] = (pre[i - 1] + a[i]) % k;
    // cout<<pre[i]<<" ";
  }

  int ans = 0;
  mm[0]++;
  for (int i = 1; i <= min(k - 1, n); i++) {
    ans += mm[pre[i]];
    mm[pre[i]]++;
  }
  for (int i = k; i <= n; i++) {
    mm[pre[i - k]]--;
    ans += mm[pre[i]];
    mm[pre[i]]++;
  }

  cout << ans;

  // -------------------------------------Code ends
  // here------------------------------------------------------------------
  clock_t clk;
  clk = clock();

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << "\n";
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int dp[100][2];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  map<int, int> m;
  m[0]++;

  int sum = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (i >= k) {
      m[v[i - k]]--;
    }
    sum += x;
    sum %= k;
    int t = sum - i;
    while (t < 0)
      t += k;
    t %= k;

    ans += m[t];
    v[i] = t;
    m[t]++;
  }

  cout << ans << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int dp[100][2];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  cin >> n >> k;
  vector<int> v(n + 1);
  map<int, int> m;
  m[0]++;

  int sum = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (i >= k) {
      m[v[i - k]]--;
    }
    sum += x;
    sum %= k;
    int t = sum - i;
    t += (i * k);

    t %= k;

    ans += m[t];
    v[i] = t;
    m[t]++;
  }

  cout << ans << "\n";
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define VI vector<int>
#define pb push_back
#define LL long long
#define mp make_pair
#define pii pair<int, int>
using namespace std;
map<int, int> s;
const int MAXN = 2e5;
int n, k;
int A[MAXN + 10];
LL S[MAXN + 10];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    A[i] = (A[i] - 1 + k) % k;
  }

  S[0] = A[0];
  for (int i = 1; i < n; i++) {
    S[i] = S[i - 1] + A[i];
    S[i] %= k;
  }
  for (int i = 0; i < k - 1; i++)
    s[S[i]]++;
  int req = 0;
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    ans += s[req];
    s[S[i]]--;
    if (i + k - 1 < n)
      s[S[i + k - 1]]++;
    req = (req + A[i]) % k;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define VI vector<int>
#define pb push_back
#define LL long long
#define mp make_pair
#define pii pair<int, int>
using namespace std;
map<int, int> s;
const int MAXN = 2e5;
int n, k;
int A[MAXN + 10];
LL S[MAXN + 10];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    A[i] = (A[i] - 1 + k) % k;
  }

  S[0] = A[0];
  for (int i = 1; i < n; i++) {
    S[i] = S[i - 1] + A[i];
    S[i] %= k;
  }
  for (int i = 0; i < min(n, k - 1); i++)
    s[S[i]]++;
  int req = 0;
  LL ans = 0;
  for (int i = 0; i < n; i++) {
    ans += s[req];
    s[S[i]]--;
    if (i + k - 1 < n)
      s[S[i + k - 1]]++;
    req = (req + A[i]) % k;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
using ll = long long;
using pi = pair<ll, ll>;
int s[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  unordered_map<int, deque<int>> a;
  a[0].emplace_back(0);
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    x %= k;
    s[i] = (s[i - 1] + x) % k;
    int t = s[i] - i;
    for (; t < 0; t += k)
      ;
    int pos = 0, sz = a[t].size(), sh;
    for (; pos < sz; ++pos)
      if (i - (sh = a[t][pos]) < k)
        break;
    ans += a[t].size() - pos;
    while (pos--)
      a[t].pop_front();
    a[t].emplace_back(i);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
using ll = long long;
using pi = pair<ll, ll>;
int s[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  unordered_map<int, deque<int>> a;
  a[0].emplace_back(0);
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    x %= k;
    s[i] = (s[i - 1] + x) % k;
    int t = (s[i] - i + k * 200000ll) % k;
    int pos = 0, sz = a[t].size(), sh;
    for (; pos < sz; ++pos)
      if (i - (sh = a[t][pos]) < k)
        break;
    ans += a[t].size() - pos;
    while (pos--)
      a[t].pop_front();
    a[t].emplace_back(i);
  }
  cout << ans;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 1000000007

vector<ll> num[100010];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    A[i]--;
    A[i] %= K;
  }

  for (int i = 1; i <= N; i++)
    A[i] += A[i - 1], A[i] %= K;

  map<ll, ll> Z;
  for (int i = 0; i <= N; i++) {
    if (Z.count(A[i])) {
      num[Z[A[i]]].push_back(i);
    } else {
      Z.insert({A[i], i});
      num[i].push_back(i);
    }
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    int js = num[i].size();
    for (ll j = 0; j < js; j++) {
      auto it = upper_bound(all(num[i]), num[i][j] - K);
      ll dis = it - num[i].begin();
      ans += j - dis;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 1000000007

vector<ll> num[200010];

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    A[i]--;
    A[i] %= K;
  }

  for (int i = 1; i <= N; i++)
    A[i] += A[i - 1], A[i] %= K;

  map<ll, ll> Z;
  for (int i = 0; i <= N; i++) {
    if (Z.count(A[i])) {
      num[Z[A[i]]].push_back(i);
    } else {
      Z.insert({A[i], i});
      num[i].push_back(i);
    }
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    int js = num[i].size();
    for (ll j = 0; j < js; j++) {
      auto it = upper_bound(all(num[i]), num[i][j] - K);
      ll dis = it - num[i].begin();
      ans += j - dis;
    }
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, s[100100], res;
map<int, int> mp;
signed main() {
  scanf("%lld%lld", &n, &m), mp[0]++;
  for (int i = 1; i <= n; i++) {
    if (i >= m)
      mp[s[i - m]]--;
    scanf("%lld", &s[i]), s[i]--, s[i] = (s[i] + s[i - 1]) % m, res += mp[s[i]],
                                  mp[s[i]]++;
  }
  printf("%lld\n", res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, s[200100], res;
map<int, int> mp;
signed main() {
  scanf("%lld%lld", &n, &m), mp[0]++;
  for (int i = 1; i <= n; i++) {
    if (i >= m)
      mp[s[i - m]]--;
    scanf("%lld", &s[i]), s[i]--, s[i] = (s[i] + s[i - 1]) % m, res += mp[s[i]],
                                  mp[s[i]]++;
  }
  printf("%lld\n", res);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> a(n), s(n + 1, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    s[i + 1] = s[i] + a[i];

  int ans = 0;
  vector<int> v(k + 1, 0);
  int l = 0;
  for (int r = 0; r < n + 1; r++) {
    if (r - l >= k) {
      --v[(s[l] - l) % k];
      ++l;
    }
    ans += v[(s[r] - r) % k];
    ++v[(s[r] - r) % k];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> a(n), s(n + 1, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    s[i + 1] = s[i] + a[i];

  long long ans = 0;
  map<int, int> v;
  int l = 0;
  for (int r = 0; r < n + 1; r++) {
    if (r - l >= k) {
      --v[(s[l] - l) % k];
      ++l;
    }
    ans += v[(s[r] - r) % k];
    ++v[(s[r] - r) % k];
  }
  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <map>
#include <stdio.h>
#define ll long long
#define MAXN 100005
#define MOD(x, m) (((x) % m + m) % m)
using namespace std;
ll N, K;
ll A[MAXN], psum[MAXN];
map<ll, ll> mp;

int main() {
  int i, j;
  scanf("%lld%lld", &N, &K);
  for (i = 1; i <= N; ++i) {
    scanf("%lld", &A[i]);
    A[i] %= K;
    psum[i] = (psum[i - 1] + A[i]) % K;
  }

  ll ans = 0ll;
  mp[psum[0]]++;
  for (i = 1; i <= N; ++i) {
    if (i - K >= 0) {
      mp[MOD(psum[i - K] - (i - K), K)]--;
    }
    ans += mp[MOD(psum[i] - i, K)];
    mp[MOD(psum[i] - i, K)]++;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <map>
#include <stdio.h>
#define ll long long
#define MAXN 200005
#define MOD(x, m) (((x) % m + m) % m)
using namespace std;
ll N, K;
ll A[MAXN], psum[MAXN];
map<ll, ll> mp;

int main() {
  int i, j;
  scanf("%lld%lld", &N, &K);
  for (i = 1; i <= N; ++i) {
    scanf("%lld", &A[i]);
    A[i] %= K;
    psum[i] = (psum[i - 1] + A[i]) % K;
  }

  ll ans = 0ll;
  mp[psum[0]]++;
  for (i = 1; i <= N; ++i) {
    if (i - K >= 0) {
      mp[MOD(psum[i - K] - (i - K), K)]--;
    }
    ans += mp[MOD(psum[i] - i, K)];
    mp[MOD(psum[i] - i, K)]++;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<ll> sum(n + 1);
  sum[0] = 0;
  rep(i, n) { sum[i + 1] = sum[i] + a[i]; }
  rep1(i, n) sum[i] = (sum[i] - (ll)i) % k;

  map<ll, ll> mp;
  rep(i, k) { mp[sum[i]]++; }

  ll res = 0;
  for (auto m : mp) {
    res += m.second * (m.second - (ll)1) / (ll)2;
  }

  rep(i, n - k + 1) {
    mp[sum[i]]--;
    res += mp[sum[i + k]];
    mp[sum[i + k]]++;
  }
  cout << res << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<ll> sum(n + 1);
  sum[0] = 0;
  rep(i, n) { sum[i + 1] = sum[i] + a[i]; }
  rep1(i, n) sum[i] = (sum[i] - (ll)i) % k;

  map<ll, ll> mp;
  rep(i, min(n + 1, k)) { mp[sum[i]]++; }

  ll res = 0;
  for (auto m : mp) {
    res += m.second * (m.second - (ll)1) / (ll)2;
  }

  rep(i, n - k + 1) {
    mp[sum[i]]--;
    res += mp[sum[i + k]];
    mp[sum[i + k]]++;
  }
  cout << res << "\n";

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
// Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!

/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define all(v) (v).begin(), (v).end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define pii pair<int, int>
#define maxn 100005
using namespace std;
ll psum[100005];
signed main() {
  fast;

  ll n, k, i, ans = 0;
  cin >> n >> k;
  ll a[n + 2];
  map<ll, ll> ma;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    psum[i] = (psum[i - 1] % k + a[i] % k) % k;
  }
  for (i = 1; i <= n; i++) {
    psum[i] = ((psum[i] - i) % k + k) % k;
  }
  for (i = 0; i <= n; i++) {

    if (i >= k) {
      ma[psum[i - k]]--;
    }
    ans += ma[psum[i]];
    ma[psum[i]]++;
  }
  cout << ans;
}

Fixed submission:
// Nihal Mittal - nihal_47
// Strike First , Strike Hard , No Mercy !!

/**⠀⠀⠀⠀⠀⠀⣀⣤⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣰⣿⣿⣿⣿⠿⠿⣿⣿⣿⣿⣿⣿⣿⣧⢀⠀⠀⠀⠀
⠀⠀⠀⣿⣿⣿⠋⠀⠀⠀⠀⠀⠙⠀⠙⣿⣿⣿⣷⢳⢀⠀⠀⠀
⠀⠀⣠⣿⣿⣿⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⢀
⠀⠀⣸⣿⣿⣿⠸⠀⠀⠀⠒⠒⠒⠐⠀⠀⢿⣿⣿⣿⣿⣿⠀⠀
⠀⣴⣿⣿⣿⡿⠀⠒⣋⣙⡒⢰⠀⠤⣖⠒⢾⣿⣿⣿⣿⣧⠀⠀
⢺⣿⣿⣿⣿⢀⠀⠀⠉⠉⠉⠸⠀⡇⠉⠉⠀⢿⣿⣿⣿⣄⠀⠀
⠀⠙⣿⣿⣧⢻⠀⠀⠀⠀⠀⠠⠀⠰⠀⠀⠀⣸⠸⣿⣿⠿⠰⠀
⠀⠀⠀⠹⣿⣿⣿⣷⠀⡠⠙⣲⣔⣅⢡⣰⣷⣿⣿⣿⣧⠀⠀⠀
⠀⠀⠀⣼⣿⣿⣿⣿⠀⡿⠭⠭⠭⠭⢿⠀⣿⢻⣿⣿⠃⠀⠀⠀
⠀⠀⠀⠙⠛⣿⢻⠹⣿⠐⠙⠛⠟⠉⢀⣴⡟⢿⣿⡏⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⡟⠀⠀⠻⣦⣤⣶⠾⠋⠀⠀⠁⡦⢄⢀⠀⠀⠀
⠀⠀⠀⠀⡠⠁⡇⠑⢄⠀⠀⠀⠀⠀⠀⠔⠀⠀⠁⠀⠀⠀⠉⠁
⠀⠔⠊⠁⠀⠀⣇⠀⠀⠀⠑⡤⠤⢎⠁⠀⠀⡘⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢢⠠⠀⡠⢆⠀⠀⡠⠙⢄⠀⡸⠀
**/
#include <bits/stdc++.h>
#define ll long long
#define l long
#define mk make_pair
#define pb push_back
#define in insert
#define se second
#define fi first
#define mod 1000000007
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define all(v) (v).begin(), (v).end()
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(0);
#define pii pair<int, int>
#define maxn 100005
using namespace std;
ll psum[200005];
signed main() {
  fast;

  ll n, k, i, ans = 0;
  cin >> n >> k;
  ll a[n + 2];
  map<ll, ll> ma;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    psum[i] = (psum[i - 1] % k + a[i] % k) % k;
  }
  for (i = 1; i <= n; i++) {
    psum[i] = ((psum[i] - i) % k + k) % k;
  }
  for (i = 0; i <= n; i++) {

    if (i >= k) {
      ma[psum[i - k]]--;
    }
    ans += ma[psum[i]];
    ma[psum[i]]++;
  }
  cout << ans;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int N, K;
  ll A[SIZE];

  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
    A[i + 1] += A[i];
    A[i + 1] %= K;
  }

  int counter[SIZE] = {};
  ll ans = 0;

  for (int i = 0; i <= N; i++) {
    if (i - K >= 0)
      counter[A[i - K]]--;

    debug(A[i]);
    A[i] = (A[i] + K - i % K) % K;
    debug(A[i]);
    ans += counter[A[i]];
    counter[A[i]]++;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int main() {
  int N, K;
  ll A[SIZE];

  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
    A[i + 1] += A[i];
    A[i + 1] %= K;
  }

  map<int, int> counter = {};
  ll ans = 0;

  for (int i = 0; i <= N; i++) {
    if (i - K >= 0)
      counter[A[i - K]]--;

    debug(A[i]);
    A[i] = (A[i] + K - i % K) % K;
    debug(A[i]);
    ans += counter[A[i]];
    counter[A[i]]++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265358979
#define EPS 1e-8
#define mod 1000000007
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl "\n"

const ll INF = (ll)2e18;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k;
  cin >> n >> k;

  vector<ll> v(n + 1);

  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
    v[i]--;
  }
  vector<ll> pref(n + 1, 0);

  for (ll i = 1; i <= n; i++) {
    pref[i] = pref[i - 1];
    pref[i] += v[i];
    pref[i] %= k;
  }

  map<ll, ll> rem;
  ll ans = 0;
  for (ll i = 0; i < k; i++) {
    ans += rem[pref[i]];
    rem[pref[i]]++;
  }

  for (ll i = k; i <= n; i++) {
    rem[pref[i - k]]--;
    ans += rem[pref[i]];
    rem[pref[i]]++;
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define PI 3.14159265358979
#define EPS 1e-8
#define mod 1000000007
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl "\n"

const ll INF = (ll)2e18;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k;
  cin >> n >> k;

  vector<ll> v(n + 1);

  for (ll i = 1; i <= n; i++) {
    cin >> v[i];
    v[i]--;
  }
  vector<ll> pref(n + 1, 0);

  for (ll i = 1; i <= n; i++) {
    pref[i] = pref[i - 1];
    pref[i] += v[i];
    pref[i] %= k;
  }

  map<ll, ll> rem;
  ll ans = 0;
  for (ll i = 0; i < min(k, n + 1); i++) {
    ans += rem[pref[i]];
    rem[pref[i]]++;
  }

  for (ll i = k; i <= n; i++) {
    rem[pref[i - k]]--;
    ans += rem[pref[i]];
    rem[pref[i]]++;
  }
  cout << ans;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define NMAX 500
#define zeros(x) (x ^ (x - 1)) & x

using namespace std;

typedef long long ll;
typedef long double ld;

ll n, k, x, V[200005], sp, tot, A[200005];
map<ll, ll> F;

int main() {
  freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    V[i] = x - 1;
    if (V[i] == -1)
      V[i] = k - 1;
  }

  if (k == 1) {
    cout << 0;
    return 0;
  }

  F[0] = 1;
  A[0] = 0;
  for (int i = 1; i <= n; i++) {
    sp += V[i];
    sp %= k;

    tot += F[sp];
    F[sp]++;
    A[i] = sp;

    if (i >= k - 1)
      F[A[i - (k - 1)]]--;
  }
  cout << tot;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define NMAX 500
#define zeros(x) (x ^ (x - 1)) & x

using namespace std;

typedef long long ll;
typedef long double ld;

ll n, k, x, V[200005], sp, tot, A[200005];
map<ll, ll> F;

int main() {
  // freopen("input.txt", "r", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    V[i] = x - 1;
    if (V[i] == -1)
      V[i] = k - 1;
  }

  if (k == 1) {
    cout << 0;
    return 0;
  }

  F[0] = 1;
  A[0] = 0;
  for (int i = 1; i <= n; i++) {
    sp += V[i];
    sp %= k;

    tot += F[sp];
    F[sp]++;
    A[i] = sp;

    if (i >= k - 1)
      F[A[i - (k - 1)]]--;
  }
  cout << tot;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
#define inf 1000000007
#define LINF 100000000000000007LL
#define ll long long
using namespace std;
int a[114514], sum[114514];
signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
  }
  map<int, int> mope;
  int ans = 0;
  mope[0] = 1;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
    if (i >= k)
      mope[sum[i - k] % k]--;
    ans += mope[sum[i] % k];
    // cout << mope[ sum[i] % k ] <<endl;
    mope[sum[i] % k]++;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
#define inf 1000000007
#define LINF 100000000000000007LL
#define ll long long
using namespace std;
int a[214514], sum[214514];
signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
  }
  map<int, int> mope;
  int ans = 0;
  mope[0] = 1;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + a[i];
    if (i >= k)
      mope[sum[i - k] % k]--;
    ans += mope[sum[i] % k];
    // cout << mope[ sum[i] % k ] <<endl;
    mope[sum[i] % k]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 7;
int n, k, sum[maxn], val[maxn], ans;
map<int, int> mp;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin);
  cin >> n >> k;
  mp[0] = 1;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    sum[i] = (sum[i - 1] + x) % k;
    val[i] = ((sum[i] - i) % k + k) % k;
    if (i - k >= 0) {
      mp[val[i - k]]--;
    }
    ans += mp[val[i]];
    mp[val[i]]++;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5 + 7;
int n, k, sum[maxn], val[maxn], ans;
map<int, int> mp;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  if (fopen("test.inp", "r"))
    freopen("test.inp", "r", stdin);
  cin >> n >> k;
  mp[0] = 1;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    sum[i] = (sum[i - 1] + x) % k;
    val[i] = ((sum[i] - i) % k + k) % k;
    if (i - k >= 0) {
      mp[val[i - k]]--;
    }
    ans += mp[val[i]];
    mp[val[i]]++;
  }
  cout << ans;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
// finish date: 2019/11/24

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define rep(i, n) FOR(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<vector<vector<double>>> vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, pair<int, int>> plii;
const int bigmod = 1000000007;
const int INF = 1000000007;
const long long INFll = 100000000000000000;

int main() {
  int N, K;
  cin >> N >> K;
  vi A(N);
  rep(i, N) {
    cin >> A[i];
    A[i] -= 1;
    A[i] %= K;
  }
  vi cum(N, 0);
  rep(i, N) {
    cum[i] = A[i] % K;
    if (i != 0)
      cum[i] = (cum[i] + cum[i - 1]) % K;
  }

  ll ans = 0;
  map<int, int> cnt;
  rep(i, K - 1) {
    //        cum[i] = (K - cum[i]) % K;
    cnt[cum[i]]++;
  }
  ans += cnt[0];

  //    int focus = 0;
  FOR(i, K - 1, N + K - 1) {
    cnt[cum[i - K + 1]]--;
    if (i < N)
      cnt[cum[i]]++;
    ans += cnt[(cum[i - K + 1]) % K];
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
// finish date: 2019/11/24

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int(i) = a; (i) < (b); (i)++)
#define rep(i, n) FOR(i, 0, n)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<vector<vector<double>>> vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, pair<int, int>> plii;
const int bigmod = 1000000007;
const int INF = 1000000007;
const long long INFll = 100000000000000000;

int main() {
  int N, K;
  cin >> N >> K;
  vi A(N);
  rep(i, N) {
    cin >> A[i];
    A[i] -= 1;
    A[i] %= K;
  }
  vi cum(N, 0);
  rep(i, N) {
    cum[i] = A[i] % K;
    if (i != 0)
      cum[i] = (cum[i] + cum[i - 1]) % K;
  }

  ll ans = 0;
  map<int, int> cnt;
  rep(i, min(K - 1, N)) {
    //        cum[i] = (K - cum[i]) % K;
    cnt[cum[i]]++;
  }
  ans += cnt[0];

  //    int focus = 0;
  FOR(i, K - 1, N + K - 1) {
    cnt[cum[i - K + 1]]--;
    if (i < N)
      cnt[cum[i]]++;
    ans += cnt[(cum[i - K + 1]) % K];
  }

  cout << ans << endl;

  return 0;
}