Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 1000000000000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

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

long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// mod. m での a の逆元 a^{-1} を計算するよ！
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

// aCbをmod.mで割った余りを求める
ll int c(ll int a, ll int b, ll int m) {
  ll int ans = 1;
  for (ll int i = 0; i < b; i++) {
    ans *= a - i;
    ans %= m;
  }
  for (ll int i = 1; i <= b; i++) {
    ans *= modinv(i, m);
    ans %= m;
  }
  return ans;
}
struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }
  int size(int a) { return par[root(a)]; }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  string s;
  cin >> s;
  int n = s.size();
  hei(int) a(n), b(n + 1, 0);
  reverse(s);
  map<int, int> Z;
  rep(i, 0, n) {
    int x = s[i] - '0';
    a[i] = modpow(10, i, 2019) * x % 2019;
  }
  Z[0] = 1;
  llint ans = 0;
  rep(i, 1, n + 1) {
    b[i] = (b[i - 1] + a[i - 1]) % 2019;
    ans += Z[b[i]];
    Z[b[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 1000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
llint gcd(llint a, llint b) {
  if (b > a)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
llint Tow(int m, int n) { // 2のn乗
  if (n == 0)
    return 1;
  llint x = Tow(m, n / 2);
  x *= x;
  if (n % 2 == 1)
    x *= m;
  return x;
}
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}

int SSS(string s) {
  int n = s.size();
  if (n > 4) {
    string t = "0";
    int x = 5;
    rep(i, 5, n + 1) {
      x = i;
      t = s.substr(n - i, i);
      if (stoi(t) > 2019)
        break;
    }
    if (t != "0") {
      int T = stoi(t) % 2019;
      t = t.substr(0, x);
      rep(i, 0, n - x) t += "0";
      s = SSS(t + to_string(T));
    }
  }
  if (stoi(s) == 0)
    return 0;
  if (stoi(s) % 2019 == 0)
    return 1;
  return 0;
}

int main() {
  string s;
  cin >> s;
  int ans = 0;
  if (s.size() >= 4) {
    ll A = 2019;
    for (int i = 1; A * i <= stoll(s); i++) {
      int k = to_string(A * i).size();
      rep(j, 0, s.size() - k + 1) {
        if (stoll(s.substr(j, k)) == A * i)
          ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}