Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll N, P;
  std::cin >> N >> P;
  std::string S;
  std::cin >> S;
  ll ans = 0;
  if (P == 2 || P == 5) {
    rep(i, N) if ((S[i] - '0') % P == 0) ans += 1LL * (i + 1);
  } else {
    std::vector<ll> dp(N);
    ll d = 1, x = 0;
    for (int i = N - 1; i >= 0; i--) {
      x += (S[i] - '0') * d;
      x %= P;
      d *= 10;
      d %= P;
      ans += dp[x];
      dp[x]++;
      if (x == 0)
        ans++;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  ll N, P;
  std::cin >> N >> P;
  std::string S;
  std::cin >> S;
  ll ans = 0;
  if (P == 2 || P == 5) {
    rep(i, N) if ((S[i] - '0') % P == 0) ans += 1LL * (i + 1);
  } else {
    std::vector<ll> dp(2e5 + 7);
    ll d = 1, x = 0;
    for (int i = N - 1; i >= 0; i--) {
      x += (S[i] - '0') * d;
      x %= P;
      d *= 10;
      d %= P;
      ans += dp[x];
      dp[x]++;
      if (x == 0)
        ans++;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}