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

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int n, m, p, q, k, ans;
string s;
char ch;
vector<int> v;

vector<int> add_i_to_tenj[10];
int dp[2][2021];

void solve() {
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j < 2020; j++) {
      if ((10 * j + i) % 2019 == 0)
        add_i_to_tenj[i].pb(j);
    }
  }

  cin >> s;

  int n = s.length();
  int x = s[0] - 48;
  int num;

  dp[0][x % 2019] += 1;

  for (int i = 1; i < n; i++) {

    x = s[i] - 48;

    for (int j = 0; j < (int)add_i_to_tenj[x].size(); j++) {
      num = add_i_to_tenj[x][j];
      ans += dp[0][num];
    }

    for (int j = 0; j <= 2018; j++) {
      dp[1][(10 * j + x) % 2019] += dp[0][j];
    }

    dp[1][x] += 1;

    for (int j = 0; j <= 2019; j++) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }
  cout << ans;
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int n, m, p, q, k, ans;
string s;
char ch;
vector<int> v;

vector<int> add_i_to_tenj[10];
int dp[200011][2021];

void solve() {
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j < 2020; j++) {
      if ((10 * j + i) % 2019 == 0)
        add_i_to_tenj[i].pb(j);
    }
  }

  cin >> s;

  int n = s.length();
  int x = s[0] - 48;
  int num;

  dp[0][x % 2019] += 1;

  for (int i = 1; i < n; i++) {

    x = s[i] - 48;

    for (int j = 0; j < (int)add_i_to_tenj[x].size(); j++) {
      num = add_i_to_tenj[x][j];
      ans += dp[i - 1][num];
    }

    for (int j = 0; j <= 2018; j++) {
      dp[i][(10 * j + x) % 2019] += dp[i - 1][j];
    }

    dp[i][x] += 1;
  }
  cout << ans;
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}