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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(in) in.begin(), in.end()
constexpr int INF =
    (numeric_limits<int>::max() == INT_MAX ? (int)1e9 : (long long)1e18);
constexpr long double eps = 1e-6;
const string el = "\\n";
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
void fast_io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
int out(int a) {
  cout << a << el;
  return 0;
}
int out(string s = "NO") {
  cout << s << el;
  return 0;
}
template <class T> inline int out(vector<T> &v, string c = " ") {
  for (int i = 0; i < v.size(); ++i) {
    if (i)
      cout << c;
    cout << v[i];
  }
  cout << el;
  return 0;
}
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;
}
int dp[2][2019] = {};
signed main() {
  fast_io();
  string s;
  cin >> s;
  constexpr int mod = 2019;
  vector<int> v;
  for (const auto &c : s)
    v.push_back(c - '0');
  int ans = 0;
  dp[1][v.front() * 10] = 1;
  dp[0][v.front()] = 1;
  for (int i = 1; i < v.size(); ++i) {
    int crt = v[i];
    int inv = mod - v[i];
    ans += dp[i % 2][inv];
    for (int j = 0; j < 2019; ++j) {
      dp[(i + 1) % 2][(j * 10 + crt * 10) % mod] = dp[i % 2][j];
    }
    dp[(i + 1) % 2][crt * 10] += 1;
  }
  cout << ans << endl;
  ;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(in) in.begin(), in.end()
constexpr int INF =
    (numeric_limits<int>::max() == INT_MAX ? (int)1e9 : (long long)1e18);
constexpr long double eps = 1e-6;
const string el = "\\n";
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
void fast_io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
}
int out(int a) {
  cout << a << el;
  return 0;
}
int out(string s = "NO") {
  cout << s << el;
  return 0;
}
template <class T> inline int out(vector<T> &v, string c = " ") {
  for (int i = 0; i < v.size(); ++i) {
    if (i)
      cout << c;
    cout << v[i];
  }
  cout << el;
  return 0;
}
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;
}
int dp[200001][2019] = {0};
signed main() {
  fast_io();
  string s;
  cin >> s;
  constexpr int mod = 2019;
  vector<int> v;
  for (const auto &c : s)
    v.push_back(c - '0');
  int ans = 0;
  dp[1][v.front() * 10] = 1;
  dp[0][v.front()] = 1;
  for (int i = 1; i < v.size(); ++i) {
    int crt = v[i];
    int inv = mod - v[i];
    ans += dp[i][inv];
    for (int j = 0; j < 2019; ++j) {
      dp[i + 1][(j * 10 + crt * 10) % mod] += dp[i][j];
    }
    dp[i + 1][crt * 10] += 1;
  }
  cout << ans << endl;
  ;
}