Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define mod 1000000007
#define FOR(x, to) for (int x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define long long long
inline int rei() {
  int x;
  cin >> x;
  return x;
}
inline long rel() {
  long x;
  cin >> x;
  return x;
}
inline string res() {
  string x;
  cin >> x;
  return x;
}
//-------------------------------------------------------
int A[10000];
int Mo[10001];
void Calc() {
  int N = rei();
  int P = rei();
  string S = res();
  for (int i = 0; i < N; i++) {
    A[i] = S[i] - '0';
  }
  if (P == 2 || P == 5) {
    long ans = 0;
    for (int i = 0; i < N; i++) {
      if (A[i] % P == 0) {
        ans += i + 1;
      }
    }
    cout << ans << endl;
  } else {
    int T = 1;
    Mo[N] = 0;
    for (int i = N - 1; i >= 0; i--) {
      Mo[i] = (Mo[i + 1] + T * A[i]) % P;
      T *= 10;
      T %= P;
    }
    sort(Mo, Mo + N + 1);
    long ans = 0;
    int c = 1;
    for (int i = 1; i <= N; i++) {
      if (Mo[i] == Mo[i - 1]) {
        c++;
      } else {
        ans += 1LL * c * (c - 1) / 2;
        c = 1;
      }
    }
    ans += 1LL * c * (c - 1) / 2;
    cout << ans << endl;
  }
}
int main(int argc, char **argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.tie(0);
  Calc();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define mod 1000000007
#define FOR(x, to) for (int x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define long long long
inline int rei() {
  int x;
  cin >> x;
  return x;
}
inline long rel() {
  long x;
  cin >> x;
  return x;
}
inline string res() {
  string x;
  cin >> x;
  return x;
}
//-------------------------------------------------------
int A[200000];
int Mo[200001];
void Calc() {
  int N = rei();
  int P = rei();
  string S = res();
  for (int i = 0; i < N; i++) {
    A[i] = S[i] - '0';
  }
  if (P == 2 || P == 5) {
    long ans = 0;
    for (int i = 0; i < N; i++) {
      if (A[i] % P == 0) {
        ans += i + 1;
      }
    }
    cout << ans << endl;
  } else {
    int T = 1;
    Mo[N] = 0;
    for (int i = N - 1; i >= 0; i--) {
      Mo[i] = (Mo[i + 1] + T * A[i]) % P;
      T *= 10;
      T %= P;
    }
    sort(Mo, Mo + N + 1);
    long ans = 0;
    int c = 1;
    for (int i = 1; i <= N; i++) {
      if (Mo[i] == Mo[i - 1]) {
        c++;
      } else {
        ans += 1LL * c * (c - 1) / 2;
        c = 1;
      }
    }
    ans += 1LL * c * (c - 1) / 2;
    cout << ans << endl;
  }
}
int main(int argc, char **argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  cout.tie(0);
  Calc();
  return 0;
}