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

Buggy submission (status: Runtime Error):

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

typedef long long ll;

ll vs[100000 + 10];
ll cs[100000 + 10];
map<ll, queue<int>> table;

int main(void) {
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  cs[0] = 0;
  REP(i, n) {
    scanf("%lld", &vs[i]);
    vs[i] = (vs[i] + k - 1) % k;
    cs[i + 1] = (cs[i] + vs[i]) % k;
  }
  ll res = 0;
  // REP(i, n) {
  //   cerr << i << " : " << vs[i] << endl;
  // }
  // cerr << endl;
  // REP(i, n+1) {
  //   cerr << i << " : " << cs[i] << endl;
  // }
  for (int i = n; i >= 0; --i) {
    ll cur = cs[i];
    ll opp = cur;
    while (!table[opp].empty() && table[opp].front() >= i + k) {
      table[opp].pop();
    }
    // if(table[opp].size() > 0) {
    //   cerr << i << " " << table[opp].size() << endl;
    // }
    res += table[opp].size();
    table[cur].push(i);
  }
  cout << res << endl;

  return 0;
}


Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

typedef long long ll;

ll vs[200000 + 10];
ll cs[200000 + 10];
map<ll, queue<int>> table;

int main(void) {
  int n;
  ll k;
  scanf("%d%lld", &n, &k);
  cs[0] = 0;
  REP(i, n) {
    scanf("%lld", &vs[i]);
    vs[i] = (vs[i] + k - 1) % k;
    cs[i + 1] = (cs[i] + vs[i]) % k;
  }
  ll res = 0;
  // REP(i, n) {
  //   cerr << i << " : " << vs[i] << endl;
  // }
  // cerr << endl;
  // REP(i, n+1) {
  //   cerr << i << " : " << cs[i] << endl;
  // }
  for (int i = n; i >= 0; --i) {
    ll cur = cs[i];
    ll opp = cur;
    while (!table[opp].empty() && table[opp].front() >= i + k) {
      table[opp].pop();
    }
    // if(table[opp].size() > 0) {
    //   cerr << i << " " << table[opp].size() << endl;
    // }
    res += table[opp].size();
    table[cur].push(i);
  }
  cout << res << endl;

  return 0;
}
Problem p02851 (C++)

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

typedef long long LL;
typedef unsigned long long LONG;
const int N = 1e5 + 5;
using namespace std;
typedef pair<int, int> PII;
#define ft fisrt
#define sd second
#define eb emplace_back

template <class T> inline bool read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

LL a[N * 5], k, n;
LL ans = 0, sum[N] = {0};
map<unsigned int, LL> num;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  num[0] = 1;
  for (int i = 1; i <= n; ++i) {
    if (i - k >= 0) {
      num[(sum[i - k] - (i - k)) % k]--;
    }
    if (num.count((sum[i] - i) % k)) {
      ans += num[(sum[i] - i) % k];
    }
    num[(sum[i] - i) % k]++;
  }
  cout << ans;
  return 0;
}

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

typedef long long LL;
typedef unsigned long long LONG;
const int N = 1e5 + 5;
using namespace std;
typedef pair<int, int> PII;
#define ft fisrt
#define sd second
#define eb emplace_back

template <class T> inline bool read(T &ret) {
  char c;
  int sgn;
  if (static_cast<void>(c = getchar()), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (static_cast<void>(c = getchar()), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}

LL a[N * 5], k, n;
LL ans = 0, sum[N * 5] = {0};
map<unsigned int, LL> num;

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  num[0] = 1;
  for (int i = 1; i <= n; ++i) {
    if (i - k >= 0) {
      num[(sum[i - k] - (i - k)) % k]--;
    }
    if (num.count((sum[i] - i) % k)) {
      ans += num[(sum[i] - i) % k];
    }
    num[(sum[i] - i) % k]++;
  }
  cout << ans;
  return 0;
}
Problem p02851 (C++)

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n, k;
  cin >> n >> k;
  // vector<int32> a(n);
  // REP(i,n)cin >> a[i];
  // vector<int32> asum(n+1,0);
  // REP(i,n)asum[i+1] = (asum[i] + a[i] - 1 + k) % k;
  vector<int32> asum(n + 1, 0);
  REP(i, n) {
    int32 a;
    cin >> a;
    asum[i + 1] = (asum[i] + a - 1 + k) % k;
  }
  vector<int32> cnt(k, 0);
  int64 ans = 0;
  REP(i, n + 1) {
    ans += cnt[asum[i]];
    cnt[asum[i]]++;
    if (i - k + 1 >= 0)
      cnt[asum[i - k + 1]]--;
  }
  ANS(ans);
  return 0;
}

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n, k;
  cin >> n >> k;
  // vector<int32> a(n);
  // REP(i,n)cin >> a[i];
  // vector<int32> asum(n+1,0);
  // REP(i,n)asum[i+1] = (asum[i] + a[i] - 1 + k) % k;
  vector<int32> asum(n + 1, 0);
  REP(i, n) {
    int32 a;
    cin >> a;
    asum[i + 1] = (asum[i] + a - 1 + k) % k;
  }
  // vector<int32> cnt(k,0);
  map<int32, int32> cnt;
  int64 ans = 0;
  REP(i, n + 1) {
    ans += cnt[asum[i]];
    cnt[asum[i]]++;
    if (i - k + 1 >= 0)
      cnt[asum[i - k + 1]]--;
  }
  ANS(ans);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2e5 + 4;
int n, a[N], mod, sum[N];
map<int, int> mp;
ll ans;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
struct Node {
  int pos, val;
  friend bool operator<(const Node &a, const Node &b) { return a.pos > b.pos; }
};
priority_queue<Node> q;
int main() {
  n = read(), mod = read();
  for (register int i = 1; i <= n; ++i) {
    a[i] = read() - 1;
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  mp[0] = 1;
  q.push((Node){0, 0});
  for (register int i = 1; i <= n; ++i) {
    while (i - q.top().pos >= mod) {
      Node cur = q.top();
      --mp[cur.val];
      q.pop();
    }
    ans += mp[sum[i]];
    ++mp[sum[i]];
    q.push((Node){i, sum[i]});
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 2e5 + 4;
int n, a[N], mod, sum[N];
map<int, int> mp;
ll ans;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
struct Node {
  int pos, val;
  friend bool operator<(const Node &a, const Node &b) { return a.pos > b.pos; }
};
priority_queue<Node> q;
int main() {
  n = read(), mod = read();
  if (mod == 1) {
    puts("0");
    return 0;
  }
  for (register int i = 1; i <= n; ++i) {
    a[i] = read() - 1;
    sum[i] = (sum[i - 1] + a[i]) % mod;
  }
  mp[0] = 1;
  q.push((Node){0, 0});
  for (register int i = 1; i <= n; ++i) {
    while (i - q.top().pos >= mod) {
      Node cur = q.top();
      --mp[cur.val];
      q.pop();
    }
    ans += mp[sum[i]];
    ++mp[sum[i]];
    q.push((Node){i, sum[i]});
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
set<int> f[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  long long ans = 0, cur = 0;
  cin >> n >> k;
  f[0].insert(0);
  for (int i = 1, a; i <= n; i++) {
    cin >> a;
    cur = (cur + a - 1) % k;
    int er = i - k;
    while (f[cur].size() && *f[cur].begin() <= er) {
      f[cur].erase(f[cur].begin());
    }
    ans += f[cur].size();
    f[cur].insert(i);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
map<int, set<int>> f;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  long long ans = 0, cur = 0;
  cin >> n >> k;
  f[0].insert(0);
  for (int i = 1, a; i <= n; i++) {
    cin >> a;
    cur = (cur + a - 1) % k;
    int er = i - k;
    while (f[cur].size() && *f[cur].begin() <= er) {
      f[cur].erase(f[cur].begin());
    }
    ans += f[cur].size();
    f[cur].insert(i);
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 800000000
#define MAX 1000

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N), sum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    sum.at(i + 1) = sum.at(i) + A.at(i);
  }
  for (int i = 0; i <= N; i++) {
    sum.at(i) -= i;
    sum.at(i) %= K;
  }
  map<int, ll> m;
  for (int i = 0; i <= N; i++) {
    if (m.count(sum.at(i)) == 0) {
      m[sum.at(i)] = 0;
    }
    m[sum.at(i)]++;
  }
  map<int, ll> count;
  for (auto x : m) {
    count[x.first] = 0;
  }
  for (int i = 0; i < K - 1; i++) {
    count[sum.at(i)]++;
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    if (i + K - 1 <= N) {
      count[sum.at(i + K - 1)]++;
    }
    ans += count[sum.at(i)] - 1;
    count[sum.at(i)]--;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 800000000
#define MAX 1000

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N), sum(N + 1, 0);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    sum.at(i + 1) = sum.at(i) + A.at(i);
  }
  for (int i = 0; i <= N; i++) {
    sum.at(i) -= i;
    sum.at(i) %= K;
  }
  map<int, ll> m;
  for (int i = 0; i <= N; i++) {
    if (m.count(sum.at(i)) == 0) {
      m[sum.at(i)] = 0;
    }
    m[sum.at(i)]++;
  }
  map<int, ll> count;
  for (auto x : m) {
    count[x.first] = 0;
  }
  for (int i = 0; i <= min(K - 2, N); i++) {
    count[sum.at(i)]++;
  }

  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    if (i + K - 1 <= N) {
      count[sum.at(i + K - 1)]++;
    }
    ans += count[sum.at(i)] - 1;
    count[sum.at(i)]--;
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v;
  rep(i, N) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  vector<int> S;
  S.push_back(0);
  int b = 0;
  rep(i, N) {
    b += v[i];
    b %= K;
    S.push_back(b);
  }
  rep(i, S.size()) { S[i] = (S[i] - (i % K) + K) % K; }

  map<int, int> memo;
  rep(i, K) { memo[S[i]]++; }
  ll ret = 0;
  rep(i, N) {
    memo[S[i]]--;
    ret += memo[S[i]];
    // cout << i << " " << S[i] << " " << memo[S[i]] << endl;
    if (i + K < N + 1)
      memo[S[i + K]]++;
  }

  cout << ret << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define FOR(it, c)                                                             \
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> v;
  rep(i, N) {
    int a;
    cin >> a;
    v.push_back(a);
  }

  vector<int> S;
  S.push_back(0);
  int b = 0;
  rep(i, N) {
    b += v[i];
    b %= K;
    S.push_back(b);
  }
  rep(i, S.size()) { S[i] = (S[i] - (i % K) + K) % K; }

  map<int, int> memo;
  rep(i, min(K, (int)S.size())) { memo[S[i]]++; }
  ll ret = 0;
  rep(i, N) {
    memo[S[i]]--;
    ret += memo[S[i]];
    // cout << i << " " << S[i] << " " << memo[S[i]] << endl;
    if (i + K < N + 1)
      memo[S[i + K]]++;
  }

  cout << ret << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define fi first
#define se second
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 7;

int main() {
  Int n, k;
  cin >> n >> k;
  Int res = 0;
  vector<Int> a(n);
  for (Int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (Int i = 0; i < n - 1; i++) {
    a[i + 1] += a[i];
  }
  for (Int i = 0; i <= n; i++) {
    a[i] %= k;
    // cout << a[i] <<" ";
  }
  // cout << endl;
  map<Int, Int> mp;
  for (Int i = 0; i < min(n, k - 1); i++) {
    mp[a[i]]++;
  }
  mp[0]++;
  for (auto i : mp) {
    res += i.se * (i.se - 1) / 2;
  }
  mp[0]--;
  // dump(res);
  for (Int i = k - 1; i < n; i++) {
    if (k <= i) {
      mp[a[i - k]]--;
    }
    // cout << mp[a[i]] << " ";
    res += mp[a[i]];
    //  dump(res);
    mp[a[i]]++;
  }
  dump(res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define dump(x) cout << (x) << '\n';
#define all(x) x.begin(), x.end()
#define fi first
#define se second
typedef int64_t Int;
Int mod = 1e9 + 7;
Int INF = 1e9 + 7;

int main() {
  Int n, k;
  cin >> n >> k;
  Int res = 0;
  vector<Int> a(n);
  for (Int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (Int i = 0; i < n - 1; i++) {
    a[i + 1] += a[i];
  }
  for (Int i = 0; i < n; i++) {
    a[i] %= k;
    // cout << a[i] <<" ";
  }
  // cout << endl;
  map<Int, Int> mp;
  for (Int i = 0; i < min(n, k - 1); i++) {
    mp[a[i]]++;
  }
  mp[0]++;
  for (auto i : mp) {
    res += i.se * (i.se - 1) / 2;
  }
  mp[0]--;
  // dump(res);
  for (Int i = k - 1; i < n; i++) {
    if (k <= i) {
      mp[a[i - k]]--;
    }
    // cout << mp[a[i]] << " ";
    res += mp[a[i]];
    //  dump(res);
    mp[a[i]]++;
  }
  dump(res);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define LL long long
using namespace std;

LL k, a[200005];

int main() {
  // clock_t start = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    a[i] -= i;
    a[i] %= k;
  }
  LL ans = 0;
  map<LL, int> c;
  c[a[0]]++;
  for (int i = 1; i < k; i++) {
    ans += c[a[i]];
    c[a[i]]++;
  }
  for (int i = k; i <= n; i++) {
    c[a[i - k]]--;
    ans += c[a[i]];
    c[a[i]]++;
  }
  cout << ans << endl;
  // cerr << fixed << setprecision(3) << (clock()-start)*1./CLOCKS_PER_SEC <<
  // endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define pii pair<int, int>
#define LL long long
using namespace std;

LL k, a[200005];

int main() {
  // clock_t start = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    a[i] += a[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    a[i] -= i;
    a[i] %= k;
  }
  LL ans = 0;
  map<LL, int> c;
  c[a[0]]++;
  if (k > n)
    k = n + 1;
  for (int i = 1; i < k; i++) {
    ans += c[a[i]];
    c[a[i]]++;
  }
  for (int i = k; i <= n; i++) {
    c[a[i - k]]--;
    ans += c[a[i]];
    c[a[i]]++;
  }
  cout << ans << endl;
  // cerr << fixed << setprecision(3) << (clock()-start)*1./CLOCKS_PER_SEC <<
  // endl;
  return 0;
}
Problem p02851 (C++)

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

#define i64 long long int
#define ran 202202

int n, k;
int s[ran];
vector<int> v[ran];
int it[ran];

int main() {
  scanf("%d%d", &n, &k);

  i64 ans = 0LL;

  s[0] = 0;
  v[0].push_back(0);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    s[i] = (s[i - 1] + x - 1) % k;

    int cur = s[i];
    v[cur].push_back(i);
    while (v[cur][it[cur]] < i - (k - 1))
      it[cur]++;
    ans += (int)v[cur].size() - it[cur] - 1;
  }

  cout << ans << endl;

  return 0;
}

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

#define i64 long long int
#define ran 202202

int n, k;
int s[ran];
map<int, vector<int>> v;
map<int, int> it;

int main() {
  scanf("%d%d", &n, &k);

  i64 ans = 0LL;

  s[0] = 0;
  v[0].push_back(0);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    s[i] = (s[i - 1] + x - 1) % k;

    int cur = s[i];
    v[cur].push_back(i);
    while (v[cur][it[cur]] < i - (k - 1))
      it[cur]++;
    ans += (int)v[cur].size() - it[cur] - 1;
  }

  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

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

const int N = 1e5 + 7;
int arr[N], pref[N];
map<int, int> lol;
int n, k;

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

  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    arr[i] %= k;
    if (arr[i] == 0) {
      arr[i] = k - 1;
    } else {
      arr[i]--;
    }
  }
  for (int i = 1; i <= n; ++i) {
    pref[i] = pref[i - 1] + arr[i - 1];
    if (pref[i] >= k) {
      pref[i] -= k;
    }
  }
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    if (i >= k - 1) {
      lol[pref[i - k + 1]]--;
    }
    lol[pref[i]]++;
    res += lol[pref[i + 1]];
  }
  cout << res << "\n";

  return 0;
}


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

const int N = 2e5 + 7;
int arr[N], pref[N];
map<int, int> lol;
int n, k;

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

  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    arr[i] %= k;
    if (arr[i] == 0) {
      arr[i] = k - 1;
    } else {
      arr[i]--;
    }
  }
  for (int i = 1; i <= n; ++i) {
    pref[i] = pref[i - 1] + arr[i - 1];
    if (pref[i] >= k) {
      pref[i] -= k;
    }
  }
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    if (i >= k - 1) {
      lol[pref[i - k + 1]]--;
    }
    lol[pref[i]]++;
    res += lol[pref[i + 1]];
  }
  cout << res << "\n";

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define mod 1000000007
#define INF 90000000000000000
int solve(vector<int> V, int k) {
  int res = 0, l = 0, r = 0, s = V.size();
  while (true) {
    while (V[r + 1] - V[l] < k)
      r++;
    r = min(r, s - 1);
    res += r - l;
    // cout<<V[l]<<':'<<V[r]<<endl;
    l++;
    if (l == s)
      break;
  }
  return res;
}
signed main(void) {
  int n, k;
  cin >> n >> k;
  int V[200010];
  V[0] = 0;
  rep(i, n) {
    int a;
    cin >> a;
    V[i + 1] = V[i] + a;
  }
  rep(i, n + 1) V[i] = (V[i] - i + k * 100000) % k;
  // rep(i,n+1)cout<<V[i]<<' ';
  // cout<<endl;
  vector<P> G(n + 1);
  rep(i, n + 1) G[i] = make_pair(V[i], i);
  sort(all(G));
  int now = 0, ans = 0;
  while (now < n + 1) {
    vector<int> V0;
    while (now < n + 1) {
      V0.push_back(G[now].S);
      if (G[now].F != G[min(n, now + 1)].F)
        break;
      now++;
    }
    // for(auto x:V0)cout<<x<<' ';
    // cout<<endl;
    now++;
    ans += solve(V0, k);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define mod 1000000007
#define INF 90000000000000000
int solve(vector<int> V, int k) {
  int res = 0, l = 0, r = 0, s = V.size();
  while (true) {
    while (r < s - 1 && V[r + 1] - V[l] < k)
      r++;
    r = min(r, s - 1);
    res += r - l;
    // cout<<V[l]<<':'<<V[r]<<endl;
    l++;
    if (l == s)
      break;
  }
  return res;
}
signed main(void) {
  int n, k;
  cin >> n >> k;
  int V[200010];
  V[0] = 0;
  rep(i, n) {
    int a;
    cin >> a;
    V[i + 1] = V[i] + a;
  }
  rep(i, n + 1) V[i] = (V[i] - i + k * 100000) % k;
  // rep(i,n+1)cout<<V[i]<<' ';
  // cout<<endl;
  vector<P> G(n + 1);
  rep(i, n + 1) G[i] = make_pair(V[i], i);
  sort(all(G));
  int now = 0, ans = 0;
  while (now < n + 1) {
    vector<int> V0;
    while (now < n + 1) {
      V0.push_back(G[now].S);
      if (G[now].F != G[min(n, now + 1)].F)
        break;
      now++;
    }
    // for(auto x:V0)cout<<x<<' ';
    // cout<<endl;
    now++;
    ans += solve(V0, k);
  }
  cout << ans << endl;
}
Problem p02851 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 58;
const ll mod = 1e9 + 7;

int main() {
  int i, j;
  int n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
  }
  a[0] = 0;
  VI m(k + 1, 0);
  ll sum = 0;
  VI s(n + 1, 0);
  ll ans = 0;
  for (i = 0; i <= n; i++) {
    sum += a[i];
    sum %= k;
    ans += m[sum];
    s[i] = sum;
    m[sum]++;
    if (i >= k - 1)
      m[s[i - (k - 1)]]--;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 58;
const ll mod = 1e9 + 7;

int main() {
  int i, j;
  int n, k;
  cin >> n >> k;
  ll a[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i]--;
  }
  a[0] = 0;
  map<ll, ll> m;
  ll sum = 0;
  VI s(n + 1, 0);
  ll ans = 0;
  for (i = 0; i <= n; i++) {
    sum += a[i];
    sum %= k;
    ans += m[sum];
    s[i] = sum;
    m[sum]++;
    if (i >= k - 1)
      m[s[i - (k - 1)]]--;
  }
  cout << ans << endl;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
map<int, int> Map;
int X[100100];
int main() {
  int N, K, Sum = 0;
  long long Ans = 0;
  cin >> N >> K;
  Map[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (i >= K)
      Map[X[i - K]]--;
    int A;
    cin >> A;
    A--;
    Sum = (Sum + A) % K;
    Ans += Map[Sum];
    Map[Sum]++;
    X[i] = Sum;
  }
  cout << Ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, int> Map;
int X[200200];
int main() {
  int N, K, Sum = 0;
  long long Ans = 0;
  cin >> N >> K;
  Map[0] = 1;
  for (int i = 1; i <= N; i++) {
    if (i >= K)
      Map[X[i - K]]--;
    int A;
    cin >> A;
    A--;
    Sum = (Sum + A) % K;
    Ans += Map[Sum];
    Map[Sum]++;
    X[i] = Sum;
  }
  cout << Ans << endl;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FORV(a, A) for (auto &a : A)

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

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

  map<int, int> M;
  ll ans = 0;
  rep(i, n + 1) {
    if (M.find(S[i - k]) == M.end())
      M[S[i]] = 0;
    if (i >= k)
      M[S[i - k]]--;
    ans += M[S[i]];
    M[S[i]]++;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, a) for (int i = 0; i < (a); ++i)
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FORV(a, A) for (auto &a : A)

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

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

  map<int, int> M;
  ll ans = 0;
  rep(i, n + 1) {
    if (M.find(S[i]) == M.end())
      M[S[i]] = 0;
    if (i >= k)
      M[S[i - k]]--;
    ans += M[S[i]];
    M[S[i]]++;
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
// cout << fixed << setprecision(17) << res << endl;
const ll MOD = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1, 0);
  for (ll i = 0; i < n; ++i) {
    cin >> a[i + 1];
    --a[i + 1];
  }
  for (ll i = 0; i < n; ++i) {
    a[i + 1] += a[i];
    a[i + 1] %= k;
  }
  ll res = 0;
  vector<ll> counts(k, 0);
  for (ll i = 0; i < n + 1; ++i) {
    if (i >= k)
      --counts[a[i - k]];
    res += counts[a[i]];
    ++counts[a[i]];
  }
  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cassert>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int64_t i = 0; i < (int64_t)n; i++)
#define REP_FOR(i, a, b) for (int64_t i = (int64_t)a; i < (int64_t)b; i++)
#define REP_REV(i, a, b) for (int64_t i = (int64_t)a; i > (int64_t)b; i--)
#define FORALL(v) (v).begin(), (v).end()
#define mp(a, b) make_pair(a, b)
#define eb(x) emplace_back(x)
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef pair<int64_t, int64_t> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int64_t> vii;
typedef vector<vii> vvii;
const ll mod = 1e9 + 7;
const int64_t infi = 1147483600;
const ll infl = 4e18 + 5;
const char EOL = '\n';
// cout << fixed << setprecision(17) << res << endl;
const ll MOD = 998244353;

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

Buggy submission (status: Runtime Error):
#pragma GCC optimize "03"
#include "bits/stdc++.h"
using namespace std;

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

map<int, int> m;
int a[N];

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, k, ans = 0;
  ;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] += a[i - 1];
  int l = 0;
  for (int i = 1; i <= n; i++) {
    m[(a[i - 1] - (i - 1) + k) % k]++;
    while (l <= i - k) {
      m[(a[l] - l + k) % k]--;
      l++;
    }
    ans += m[(a[i] - i + k) % k];
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize "03"
#include "bits/stdc++.h"
using namespace std;

#define int long long int
#define ld long double
#define pi pair<int, int>
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#ifndef LOCAL
#define endl '\n'
#endif

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;

map<int, int> m;
int a[N];

signed main() {
  IOS;
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, k, ans = 0;
  ;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] += a[i - 1];
  int l = 0;
  for (int i = 1; i <= n; i++) {
    m[(a[i - 1] - (i - 1) + k) % k]++;
    while (l <= i - k) {
      m[(a[l] - l + k) % k]--;
      l++;
    }
    ans += m[(a[i] - i + k) % k];
  }
  cout << ans;
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int)(n); (i)++)
using namespace std;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> a(n);
  rep(i, n) { scanf("%d", &a[i]); }
  n++;
  vector<long long> acc(n);
  rep(i, n) if (i) { acc[i] = acc[i - 1] + a[i - 1]; }
  map<int, int> cnt;
  vector<int> b(n);
  rep(i, n) { b[i] = ((acc[i] - i) % k + k) % k; }
  long long ans = 0;
  rep(i, k) {
    ans += cnt[b[i]];
    cnt[b[i]]++;
  }
  for (int i = k; i < n; i++) {
    cnt[b[i - k]]--;
    ans += cnt[b[i]];
    cnt[b[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int)(n); (i)++)
using namespace std;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> a(n);
  rep(i, n) { scanf("%d", &a[i]); }
  n++;
  vector<long long> acc(n);
  rep(i, n) if (i) { acc[i] = acc[i - 1] + a[i - 1]; }
  map<int, int> cnt;
  vector<int> b(n);
  rep(i, n) { b[i] = ((acc[i] - i) % k + k) % k; }
  long long ans = 0;
  rep(i, min(n, k)) {
    ans += cnt[b[i]];
    cnt[b[i]]++;
  }
  for (int i = k; i < n; i++) {
    cnt[b[i - k]]--;
    ans += cnt[b[i]];
    cnt[b[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(void) {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

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

  map<int, int64_t> mp;
  for (int i = 0; i < K; i++) {
    mp[A[i]]++;
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    mp[A[i]]--;
    ans += mp[A[i]];
    if (i + K <= N)
      mp[A[i + K]]++;
  }

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


Fixed submission:
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(void) {
  int64_t N, K;
  cin >> N >> K;
  vector<int64_t> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

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

  map<int64_t, int64_t> mp;
  for (int i = 0; i < min(N + 1, K); i++) {
    mp[A[i]]++;
  }

  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    mp[A[i]]--;
    ans += mp[A[i]];
    if (i + K <= N)
      mp[A[i + K]]++;
  }

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

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

#define EPS (1e-10)
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvb = vector<vb>;

constexpr int MOD = 1000000007;

inline int mod(ll n, int k) {
  n %= k;
  if (n < 0) {
    n += k;
  }
  return (int)n;
}

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

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

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

  vi cnt(k, 0);
  vi memory;
  if (n >= k) {
    memory.reserve(k - 1);
    rep(i, 0, k - 1) { memory[i] = -1; }
  }

  ll sum = 0;
  ll ans = 0;

  rep(i, 0, n) {
    ll a;
    cin >> a;
    a %= k;

    sum = mod(sum + a, k);
    ++cnt[mod(a - sum + i, k)];

    if (n >= k) {
      if (memory[i % (k - 1)] != -1) {
        --cnt[memory[i % (k - 1)]];
      }
      memory[i % (k - 1)] = mod(a - sum + i, k);
    }

    ans += cnt[mod(i + 1 - sum, k)];
  }

  cout << ans << endl;

  return 0;
}

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

#define EPS (1e-10)
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvb = vector<vb>;

constexpr int MOD = 1000000007;

inline int mod(ll n, int k) {
  n %= k;
  if (n < 0) {
    n += k;
  }
  return (int)n;
}

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

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

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

  map<int, int> cnt;
  vi memory;
  if (n >= k) {
    memory.reserve(k - 1);
    rep(i, 0, k - 1) { memory[i] = -1; }
  }

  ll sum = 0;
  ll ans = 0;

  rep(i, 0, n) {
    ll a;
    cin >> a;
    a %= k;

    sum = mod(sum + a, k);
    ++cnt[mod(a - sum + i, k)];

    if (n >= k) {
      if (memory[i % (k - 1)] != -1) {
        --cnt[memory[i % (k - 1)]];
      }
      memory[i % (k - 1)] = mod(a - sum + i, k);
    }

    ans += cnt[mod(i + 1 - sum, k)];
  }

  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

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

using namespace std;

typedef long long int ll;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  vector<ll> p(n + 1, 0);
  queue<ll> q;
  map<ll, ll> mp;
  for (int i = 1; i < k && i <= n; i++) {
    if (a[i] % k == 1)
      ans++;
    p[i] = p[i - 1] + a[i];
    ans += mp[(p[i] - i + k) % k];
    mp[(p[i - 1] - (i - 1) + k) % k]++;
    q.push((p[i - 1] - (i - 1) + k) % k);
  }

  for (int i = k; i <= n; i++) {
    if (a[i] % k == 1)
      ans++;
    mp[q.front()]--;
    q.pop();
    p[i] = p[i - 1] + a[i];
    ans += mp[(p[i] - i + k) % k];
    mp[(p[i - 1] - (i - 1) + k) % k]++;
    q.push((p[i - 1] - (i - 1) + k) % k);
  }

  cout << ans << endl;
}


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

using namespace std;

typedef long long int ll;

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  ll ans = 0;
  vector<ll> p(n + 1, 0);
  queue<ll> q;
  map<ll, ll> mp;
  for (int i = 1; i < k && i <= n; i++) {
    if (a[i] % k == 1)
      ans++;
    p[i] = p[i - 1] + a[i];
    ans += mp[(p[i] - i + k) % k];
    mp[(p[i - 1] - (i - 1) + k) % k]++;
    q.push((p[i - 1] - (i - 1) + k) % k);
  }

  for (int i = k; i <= n; i++) {
    if (a[i] % k == 1)
      ans++;
    if (q.size() > 0) {
      mp[q.front()]--;
      q.pop();
    }
    p[i] = p[i - 1] + a[i];
    ans += mp[(p[i] - i + k) % k];
    mp[(p[i - 1] - (i - 1) + k) % k]++;
    q.push((p[i - 1] - (i - 1) + k) % k);
  }

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

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int getVal(const vector<long long> &sum, int i, int mod) {
  int ans = (sum[i] - i) % mod;
  ans += mod;
  ans %= mod;
  return ans;
}

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

  long long ans = 0;
  vector<int> cnt(mod);
  for (int i = 0; i < n; ++i) {
    if (i >= mod - 1)
      --cnt[getVal(sum, i - (mod - 1), mod)];
    ++cnt[getVal(sum, i, mod)];
    ans += cnt[getVal(sum, i + 1, mod)];
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int getVal(const vector<long long> &sum, int i, int mod) {
  int ans = (sum[i] - i) % mod;
  ans += mod;
  ans %= mod;
  return ans;
}

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

  long long ans = 0;
  map<int, int> cnt;
  for (int i = 0; i < n; ++i) {
    if (i >= mod - 1)
      --cnt[getVal(sum, i - (mod - 1), mod)];
    ++cnt[getVal(sum, i, mod)];
    ans += cnt[getVal(sum, i + 1, mod)];
  }
  cout << ans << endl;

  return 0;
}
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 2 * 1e5 + 2;
vector<int> v[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  v[0].pb(0);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int s = a[i] - i;
    s %= k;
    // cout<<s<<" ";
    if (v[s].size() == 0) {
      v[s % k].pb(i);
      continue;
    }
    // cout<<i<<" ";
    int r = i - k;
    int o = v[s].end() - upper_bound(all(v[s]), r);
    ans += o;
    // cout<<i<<" "<<o<<" "<<s<<" "<<v[s].size()<<"\n";
    v[s % k].pb(i);
  }
  cout << ans;
}
// I never lose. I either win or I learn

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
int mpow(int a, int b, int p = MOD) {
  a = a % p;
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const int N = 2 * 1e5 + 2;
map<int, vector<int>> v;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  a[0] = 0;
  for (int i = 1; i <= n; i++)
    a[i] += a[i - 1];
  v[0].pb(0);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int s = a[i] - i;
    s %= k;
    // cout<<s<<" ";
    if (v[s].size() == 0) {
      v[s % k].pb(i);
      continue;
    }
    // cout<<i<<" ";
    int r = i - k;
    int o = v[s].end() - upper_bound(all(v[s]), r);
    ans += o;
    // cout<<i<<" "<<o<<" "<<s<<" "<<v[s].size()<<"\n";
    v[s % k].pb(i);
  }
  cout << ans;
}
// I never lose. I either win or I learn
Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll a[201000];
ll s[201000];
unordered_map<ll, ll> mp;

int main() {
  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  s[0] = (a[0] - 1) % K;
  for (int i = 1; i < N; i++) {
    s[i] = (a[i] + s[i - 1] + K * N - 1) % K;
  }
  for (int i = 0; i < K - 1; i++) {
    if (mp.find(s[i]) == mp.end()) {
      mp[s[i] % K] = 1;
    } else {
      mp[s[i] % K]++;
    }
  }

  ll res = 0;
  ll index = 0;
  for (int i = 0; i < N; i++) {
    res += mp[index];
    //        cout << i << " " << mp[index] << " " << index << endl;
    mp[s[i]]--;
    if (i + K - 1 < N) {
      if (mp.find(s[i + K - 1] % K) == mp.end()) {
        mp[s[i + K - 1] % K] = 1;
      } else {
        mp[s[i + K - 1] % K]++;
      }
    }
    index = (s[i]) % K;
  }

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <unordered_map>
#include <vector>

#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll a[201000];
ll s[201000];
unordered_map<ll, ll> mp;

int main() {
  ll N, K;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  s[0] = (a[0] - 1) % K;
  for (int i = 1; i < N; i++) {
    s[i] = (a[i] + s[i - 1] + K * N - 1) % K;
  }
  for (int i = 0; i < min(N, K - 1); i++) {
    if (mp.find(s[i]) == mp.end()) {
      mp[s[i] % K] = 1;
    } else {
      mp[s[i] % K]++;
    }
  }

  ll res = 0;
  ll index = 0;
  for (int i = 0; i < N; i++) {
    res += mp[index];
    //        cout << i << " " << mp[index] << " " << index << endl;
    mp[s[i]]--;
    if (i + K - 1 < N) {
      if (mp.find(s[i + K - 1] % K) == mp.end()) {
        mp[s[i + K - 1] % K] = 1;
      } else {
        mp[s[i + K - 1] % K]++;
      }
    }
    index = (s[i]) % K;
  }

  cout << res << endl;

  return 0;
}
Problem p02851 (C++)

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> SUM(N + 1);
  long long sum = 0;
  SUM[0] = 0;
  for (long long n = 0; n < N; ++n) {
    cin >> A[n];
    sum += A[n];
    sum %= K;
    SUM[n + 1] = (sum + (n + 1) * K - (n + 1)) % K;
  }
  map<int, int> mp;
  for (int n = 1; n < K; ++n) {
    mp[SUM[n]]++;
  }
  long long ans = 0;
  for (long long n = 0; n <= N; ++n) {
    ans += mp[SUM[n]];
    mp[SUM[n + 1]]--;
    if ((n + K) <= N) {
      mp[SUM[n + K]]++;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> SUM(N + 1);
  long long sum = 0;
  SUM[0] = 0;
  for (long long n = 0; n < N; ++n) {
    cin >> A[n];
    sum += A[n];
    sum %= K;
    SUM[n + 1] = (sum + (n + 1) * K - (n + 1)) % K;
  }
  map<int, int> mp;
  for (int n = 1; n < K; ++n) {
    if (n <= N) {
      mp[SUM[n]]++;
    }
  }
  long long ans = 0;
  for (long long n = 0; n <= N; ++n) {
    ans += mp[SUM[n]];
    mp[SUM[n + 1]]--;
    if ((n + K) <= N) {
      mp[SUM[n + K]]++;
    }
  }
  cout << ans << endl;
  return 0;
}