Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma region Template
#include <bits/stdc++.h>
using namespace std;
struct ios_accelerate {
  ios_accelerate() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} CALL;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RNG(i, a, b) for (int i = a; i < b; i++)
#define RBF(i, v) for (auto &i : v)
#define ALL(x) x.begin(), x.end()

using LL = long long;
using UL = unsigned long;
using ULL = unsigned long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using P = pair<T, T>;

const double PI = 3.141592653589793;
const int MOD = 1000000007;
const LL INF64 = static_cast<LL>(1e18) + 2;
const int INF = static_cast<int>(1e9) + 2;

template <typename T> inline void upsort(vector<T> &x) {
  sort(x.begin(), x.end());
}
template <typename T> inline void downsort(vector<T> &x) {
  sort(x.begin(), x.end(), greater<T>());
}
template <typename T> inline void col(T x) { cout << x << '\\n'; }
template <> inline void col(double x) {
  cout << fixed << setprecision(12) << x << '\\n';
}
#pragma endregion

signed main() {
  string s;
  cin >> s;
  reverse(ALL(s));

  int n = s.size();
  V<int> mods(2019, 0);
  mods[0] = 1;
  V<P<int>> memo(10);
  REP(i, 10) { memo[i] = {i, 0}; }

  int pre = 0;
  REP(i, n) {
    int x = s[i] - '0';
    while (memo[x].second < i) {
      memo[x].first = 10 * memo[x].first % 2019;
      memo[x].second++;
    }
    pre += memo[x].first;
    mods[pre % 2019]++;
  }

  int ans = 0;
  RBF(m, mods) {
    if (m > 1) {
      ans += m * (m - 1) / 2;
    }
  }

  col(ans);
  return 0;
}


Error: TLE

Fixed submission:

#pragma region Template
#include <bits/stdc++.h>
using namespace std;
struct ios_accelerate {
  ios_accelerate() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} CALL;

#define REP(i, n) for (int i = 0; i < n; i++)
#define RNG(i, a, b) for (int i = a; i < b; i++)
#define RBF(i, v) for (auto &i : v)
#define ALL(x) x.begin(), x.end()

using LL = long long;
using UL = unsigned long;
using ULL = unsigned long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using P = pair<T, T>;

const double PI = 3.141592653589793;
const int MOD = 1000000007;
const LL INF64 = static_cast<LL>(1e18) + 2;
const int INF = static_cast<int>(1e9) + 2;

template <typename T> inline void upsort(vector<T> &x) {
  sort(x.begin(), x.end());
}
template <typename T> inline void downsort(vector<T> &x) {
  sort(x.begin(), x.end(), greater<T>());
}
template <typename T> inline void col(T x) { cout << x << '\\n'; }
template <> inline void col(double x) {
  cout << fixed << setprecision(12) << x << '\\n';
}
#pragma endregion

signed main() {
  string s;
  cin >> s;
  reverse(ALL(s));

  int n = s.size();
  V<int> mods(2019, 0);
  mods[0] = 1;

  int pre = 0;
  REP(i, n) {
    int x = s[i] - '0';
    int j = 0;
    while (j < i) {
      x = 10 * x % 2019;
      j++;
    }
    pre += x;
    mods[pre % 2019]++;
  }

  int ans = 0;
  RBF(m, mods) {
    if (m > 1) {
      ans += m * (m - 1) / 2;
    }
  }

  col(ans);
  return 0;
}