Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define sz(x) ((int)(x).size())

// const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL << 60;
// const int INF = INT_MAX;
const int INF = 1 << 30; // 10^9
ll MOD = 1000000007;
// const ll MOD = 998244353;
double EPS = 1e-10;

vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};

void pres(double A) { printf("%.12lf\n", A); }
void BinarySay(ll x, ll y = 60) {
  rep(i, y) cout << (x >> (y - 1 - i) & 1);
  cout << endl;
}
ll cnt_bit(ll x) { return __builtin_popcountll(x); }

ll pow_long(ll x, ll k) {
  ll res = 1;
  while (k > 0) {
    if (k % 2)
      res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}

ll pow_mod(ll x, ll k) {
  x %= MOD;
  ll res = 1;
  while (k > 0) {
    if (k % 2) {
      res *= x;
      res %= MOD;
    }
    x *= x;
    x %= MOD;
    k /= 2;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll x, ll y) {
  ll res = x / gcd(x, y);
  res *= y;
  return res;
};

ll n;
string s;
ll rui[200010];
ll dp[20][200010];
void sub() {
  rep(i, n) {
    dp[i + 1][(s[i] - '0') % MOD]++;
    rep(j, MOD) { dp[i + 1][(j * 10 + s[i] - '0') % MOD] += dp[i][j]; }
  }
  ll ans = 0;
  rep(i, n + 1) { ans += dp[i][0]; }
  cout << ans << endl;
}

void solve() {
  cin >> n >> MOD;
  cin >> s;
  if (MOD < 20) {
    sub();
    return;
  }
  rep(i, n) {
    rui[i + 1] = rui[i] + (s[i] - '0') * pow_mod(10, n - i - 1);
    rui[i + 1] %= MOD;
  }
  vector<ll> cnt(MOD, 0);
  rep(i, n + 1) {
    if (i == 0)
      continue;
    cnt[rui[i]]++;
  }

  ll ans = 0;
  rep(i, n) {
    ans += cnt[rui[i]];
    cnt[rui[i + 1]]--;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  solve();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <complex>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
#define chmin(x, y) x = min(x, y);
#define chmax(x, y) x = max(x, y);
#define sz(x) ((int)(x).size())

// const ll INFL = LLONG_MAX;//10^18 = 2^60
const ll INFL = 1LL << 60;
// const int INF = INT_MAX;
const int INF = 1 << 30; // 10^9
ll MOD = 1000000007;
// const ll MOD = 998244353;
double EPS = 1e-10;

vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};

void pres(double A) { printf("%.12lf\n", A); }
void BinarySay(ll x, ll y = 60) {
  rep(i, y) cout << (x >> (y - 1 - i) & 1);
  cout << endl;
}
ll cnt_bit(ll x) { return __builtin_popcountll(x); }

ll pow_long(ll x, ll k) {
  ll res = 1;
  while (k > 0) {
    if (k % 2)
      res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}

ll pow_mod(ll x, ll k) {
  x %= MOD;
  ll res = 1;
  while (k > 0) {
    if (k % 2) {
      res *= x;
      res %= MOD;
    }
    x *= x;
    x %= MOD;
    k /= 2;
  }
  return res;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll x, ll y) {
  ll res = x / gcd(x, y);
  res *= y;
  return res;
};

ll n;
string s;
ll rui[200010];
ll dp[200010][20];
void sub() {
  rep(i, n) {
    dp[i + 1][(s[i] - '0') % MOD]++;
    rep(j, MOD) { dp[i + 1][(j * 10 + s[i] - '0') % MOD] += dp[i][j]; }
  }
  ll ans = 0;
  rep(i, n + 1) { ans += dp[i][0]; }
  cout << ans << endl;
}

void solve() {
  cin >> n >> MOD;
  cin >> s;
  if (MOD < 20) {
    sub();
    return;
  }
  rep(i, n) {
    rui[i + 1] = rui[i] + (s[i] - '0') * pow_mod(10, n - i - 1);
    rui[i + 1] %= MOD;
  }
  vector<ll> cnt(MOD, 0);
  rep(i, n + 1) {
    if (i == 0)
      continue;
    cnt[rui[i]]++;
  }

  ll ans = 0;
  rep(i, n) {
    ans += cnt[rui[i]];
    cnt[rui[i + 1]]--;
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  solve();
}