Problem p03287 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <vector>

#define len(X) ((int)(X).size())

#ifdef __LOCAL
#define DBG(X) cout << #X << "=" << (X) << '\n';
#else
#define DBG(X)
#endif

using std::abs;
using std::cin;
using std::cout;
using std::lower_bound;
using std::pair;
using std::sort;
using std::string;
using std::swap;
using std::unique;
using std::vector;

using int64 = long long int;
using pii = pair<int, int>;
using pii64 = pair<int64, int64>;

const int NIL = -1;
const int INF = (int)2e9;
const int64 INF64 = (int64)2e18;

void init_IO() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &vec) {
  os << '[';
  int fst = 1;

  for (auto &elem : vec) {
    if (fst == 0) {
      os << ' ';
    }

    os << elem;
    fst = 0;
  }

  os << ']';
  return os;
}

void solve() {
  int n, m;
  cin >> n >> m;
  ++n;

  vector<int> a = {0};

  for (int i = 0; i < m; ++i) {
    int value;
    cin >> value;

    a.push_back(value % m);
  }

  for (int i = 1; i < n; ++i) {
    a[i] += a[i - 1];

    if (a[i] >= m)
      a[i] -= m;
  }

  sort(a.begin(), a.end());
  int64 answ = 0;

  for (int i = 0; i < n;) {
    int j = i;

    while (j + 1 < n && a[j + 1] == a[i]) {
      ++j;
    }

    int64 sz = j - i + 1;
    answ += (sz * (sz - 1)) / 2;
    i = j + 1;
  }

  cout << answ << '\n';
}

int main() {
  init_IO();
  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <vector>

#define len(X) ((int)(X).size())

#ifdef __LOCAL
#define DBG(X) cout << #X << "=" << (X) << '\n';
#else
#define DBG(X)
#endif

using std::abs;
using std::cin;
using std::cout;
using std::lower_bound;
using std::pair;
using std::sort;
using std::string;
using std::swap;
using std::unique;
using std::vector;

using int64 = long long int;
using pii = pair<int, int>;
using pii64 = pair<int64, int64>;

const int NIL = -1;
const int INF = (int)2e9;
const int64 INF64 = (int64)2e18;

void init_IO() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &vec) {
  os << '[';
  int fst = 1;

  for (auto &elem : vec) {
    if (fst == 0) {
      os << ' ';
    }

    os << elem;
    fst = 0;
  }

  os << ']';
  return os;
}

void solve() {
  int n, m;
  cin >> n >> m;
  ++n;

  vector<int> a = {0};

  for (int i = 1; i < n; ++i) {
    int value;
    cin >> value;

    a.push_back(value % m);
  }

  for (int i = 1; i < n; ++i) {
    a[i] += a[i - 1];

    if (a[i] >= m)
      a[i] -= m;
  }

  sort(a.begin(), a.end());
  int64 answ = 0;

  for (int i = 0; i < n;) {
    int j = i;

    while (j + 1 < n && a[j + 1] == a[i]) {
      ++j;
    }

    int64 sz = j - i + 1;
    answ += (sz * (sz - 1)) / 2;
    i = j + 1;
  }

  cout << answ << '\n';
}

int main() {
  init_IO();
  solve();

  return 0;
}