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

#include <bits/stdc++.h>
using namespace std;
#define int long long // aaaaaaaaaaaaaaaaaa
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define in(s, x) s.find(x) != s.end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff) / sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
const ll mod = 1000000007;
const ll INF = 1LL << 60 - 1;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}
template <class tContainer> long long sum(tContainer const &iContainer) {
  return std::accumulate(std::begin(iContainer), std::end(iContainer), 0LL);
}

// head

int m, n, k, res, tmp, tmp2, tmp3, h, w, x, y;
VI a;
deque<PII> que;
string s;
VI b;
vector<PII> g[2100600];
// int b[2100000];
int c[2100000] = {};
int d[2100000] = {};
int dp[2020] = {};
int dp2[2020] = {};
// set<int> ch;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s;
  n = s.length();
  res = 0;
  rep(i, 0, n) {
    dp[0] += 1;
    rep(j, 0, 2019) { dp2[(j * 10 + ((int)s[i] - (int)'0')) % 2019] += dp[j]; }
    rep(j, 0, 2019) {
      dp[j] = dp2[j];
      dp2[j] = 0;
    }
    res += dp[0];
  }
  cout << res << '\\n';
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define in(s, x) s.find(x) != s.end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define SIZE(buff) (sizeof(buff) / sizeof(buff[0]))
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<pair<int, int>> VPII;
const ll mod = 1000000007;
const ll INF = 1LL << 60 - 1;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << pair_var.first << " " << pair_var.second;
  return os;
}
template <class tContainer> long long sum(tContainer const &iContainer) {
  return std::accumulate(std::begin(iContainer), std::end(iContainer), 0LL);
}

// head

int m, n, k, res, tmp, tmp2, tmp3, h, w, x, y;
VI a;
deque<PII> que;
string s;
VI b;
int dp[200001][2020] = {};
// set<int> ch;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s;
  n = s.length();
  rep(i, 0, n + 1) dp[i][0] = 1;
  rep(i, 0, n) {
    rep(j, 0, 2019) {
      dp[i + 1][(j * 10 + ((int)s[i] - (int)'0')) % 2019] += dp[i][j];
    }
  }
  res = -n - 1;
  rep(i, 0, n + 1) { res += dp[i][0]; }
  cout << res << '\\n';
}