Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define F first
#define S second
#define pb push_back
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vLL vector<LL>
#define vpint vector<pair<int, int>>
#define vpLL vector<pair<LL, LL>>
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define fr(n) for (int i = 0; i < n; i++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
using namespace std;
typedef long long LL;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, p, prev = 0, k = 1;
  cin >> n >> p;
  string s;
  vector<int> a(n);
  cin >> s;
  for (int i = n - 1; i >= 0; i--) {
    a[i] = (k * (int)(s[i] - '0') + prev) % p;
    prev = a[i];
    k = (k * 10) % p;
  }
  LL ans = 0;
  vector<int> M(n);
  if (p != 2 && p != 5) {
    for (int i = 0; i < n; i++) {
      if (i) {
        ans += M[a[i]];
      }
      if (a[i] == 0) {
        ans++;
      }
      M[a[i]]++;
    }
  } else if (p == 2) {
    fr(n) {
      if ((int)(s[i] - '0') % 2 == 0) {
        ans += i + 1;
      }
    }
  } else {
    fr(n) {
      if ((int)(s[i] - '0') % 5 == 0) {
        ans += i + 1;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define F first
#define S second
#define pb push_back
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vLL vector<LL>
#define vpint vector<pair<int, int>>
#define vpLL vector<pair<LL, LL>>
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)
#define fr(n) for (int i = 0; i < n; i++)
#define present(container, element) (container.find(element) != container.end())
#define cpresent(container, element)                                           \
  (find(all(container), element) != container.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
using namespace std;
typedef long long LL;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, p, prev = 0, k = 1;
  cin >> n >> p;
  string s;
  vector<int> a(n);
  cin >> s;
  for (int i = n - 1; i >= 0; i--) {
    a[i] = (k * (int)(s[i] - '0') + prev) % p;
    prev = a[i];
    k = (k * 10) % p;
  }
  LL ans = 0;
  vector<int> M(p);
  if (p != 2 && p != 5) {
    for (int i = 0; i < n; i++) {
      if (i) {
        ans += M[a[i]];
      }
      if (a[i] == 0) {
        ans++;
      }
      M[a[i]]++;
    }
  } else if (p == 2) {
    fr(n) {
      if ((int)(s[i] - '0') % 2 == 0) {
        ans += i + 1;
      }
    }
  } else {
    fr(n) {
      if ((int)(s[i] - '0') % 5 == 0) {
        ans += i + 1;
      }
    }
  }
  cout << ans << "\n";
  return 0;
}