Problem p02851 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> a(n);
  REP(i, n) {
    cin >> a[i];
    a[i] %= k;
  }
  vector<long long> sum(n);
  sum[0] = a[0];
  FOR(i, 1, n) sum[i] = (sum[i - 1] + a[i]) % k;
  REP(i, n) {
    (sum[i] -= i + 1) %= k;
    if (sum[i] < 0)
      sum[i] += k;
  }
  // REP(i, n) cout << sum[i] << ' ';
  map<long long, int> mp;
  REP(i, k - 2)++ mp[sum[i]];
  long long ans = 0, now = 0;
  REP(i, n) {
    if (i + k - 2 < n)
      ++mp[sum[i + k - 2]];
    ans += mp[now];
    // cout << ans << ' ' << now << '\n';
    --mp[sum[i]];
    (now += a[i] - 1) %= k;
    if (now < 0)
      now += k;
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
int main() {
  int n;
  long long k;
  cin >> n >> k;
  vector<long long> a(n);
  REP(i, n) {
    cin >> a[i];
    a[i] %= k;
  }
  vector<long long> sum(n);
  sum[0] = a[0];
  FOR(i, 1, n) sum[i] = (sum[i - 1] + a[i]) % k;
  REP(i, n) {
    (sum[i] -= i + 1) %= k;
    if (sum[i] < 0)
      sum[i] += k;
  }
  // REP(i, n) cout << sum[i] << ' ';
  map<long long, int> mp;
  for (int i = 0; i < k - 2 && i < n; ++i)
    ++mp[sum[i]];
  long long ans = 0, now = 0;
  REP(i, n) {
    if (i + k - 2 < n)
      ++mp[sum[i + k - 2]];
    ans += mp[now];
    // cout << ans << ' ' << now << '\n';
    --mp[sum[i]];
    (now += a[i] - 1) %= k;
    if (now < 0)
      now += k;
  }
  cout << ans << '\n';
  return 0;
}