Problem p02757 (C++)

Buggy submission (status: Runtime Error):
/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-03-15 09:47:30
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  os << '{';
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i) {
    os << vec[i];
    if (i != n - 1)
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  os << '{';
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    os << '{' << it->first << ':' << it->second << '}';
    if (it != --mp.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  os << '{';
  for (auto it = st.begin(); it != st.end(); ++it) {
    os << *it;
    if (it != --st.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i)
    is >> vec[i];
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  map<int, int> mp;

  int64 ans = 0;

  mp[0] = 1;

  int t = 0;
  int cur = 1;

  auto ModTen = [&](int x) -> int { return (x * 10) % p; };

  if (p == 2 || p == 5) {
    for (int i = n - 1; i >= 0; --i) {
      abort();
    }
  } else {
    for (int i = n - 1; i >= 0; --i) {
      int d = s[i] - '0';
      t = (d * cur + t) % p;
      ans += mp[t];
      ++mp[t];
      cur = ModTen(cur);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-03-15 09:47:30
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  os << '{';
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i) {
    os << vec[i];
    if (i != n - 1)
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  os << '{';
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    os << '{' << it->first << ':' << it->second << '}';
    if (it != --mp.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  os << '{';
  for (auto it = st.begin(); it != st.end(); ++it) {
    os << *it;
    if (it != --st.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i)
    is >> vec[i];
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  map<int, int> mp;

  int64 ans = 0;

  mp[0] = 1;

  int t = 0;
  int cur = 1;

  auto ModTen = [&](int x) -> int { return (x * 10) % p; };

  if (p == 2) {
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0' || s[i] == '2' || s[i] == '4' || s[i] == '6' ||
          s[i] == '8') {
        ans += (1 + i);
      }
    }

  } else if (p == 5) {
    for (int i = 0; i < n; ++i) {
      if (s[i] == '0' || s[i] == '5') {
        ans += (1 + i);
      }
    }
  } else {
    for (int i = n - 1; i >= 0; --i) {
      int d = s[i] - '0';
      t = (d * cur + t) % p;
      ans += mp[t];
      ++mp[t];
      cur = ModTen(cur);
    }
  }

  cout << ans << endl;

  return 0;
}