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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  // int ti = clock();

  // vvi dp(200001,vi(2020,0));
  vi A(2020, 0), B(2020, 0);

  int ans = 0;
  int N = int(S.length());
  rep(i, N) {
    int a = int(S[i] - '0');
    // dp[i+1][a]++;
    A[a]++;
    rep(j, 2019) {
      // if(dp[i][j] == 0) continue;
      // dp[i+1][(j*10+a)%2019] += dp[i][j];
      // if((j*10+a)%2019 == 0) ans += dp[i][j];
      if (B[j] == 0)
        continue;
      A[(j * 10 + a) % 2019] += B[j];
      if ((j * 10 + a) % 2019 == 0)
        ans += B[j];
    }

    B = A;
    A = vi(2020, 0);
  }

  cout << ans << endl;

  // printf("Execution Time: %.4lf sec\\n", 1.0 * (clock() - ti) /
  // CLOCKS_PER_SEC);

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, s, n) for (int i = (s); i < (n); i++)
#define all(v) v.begin(), v.end()
#define outve(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define outmat(v)                                                              \\
  for (auto i : v) {                                                           \\
    for (auto j : i)                                                           \\
      cout << j << " ";                                                        \\
    cout << endl;                                                              \\
  }
#define in(n, v)                                                               \\
  for (int i = 0; i < (n); i++) {                                              \\
    cin >> v[i];                                                               \\
  }
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vvp = vector<vector<pair<int, int>>>;
template <typename T> using ve = vector<T>;
template <typename T> using pq2 = priority_queue<T>;
template <typename T> using pq1 = priority_queue<T, vector<T>, greater<T>>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  // int ti = clock();

  vvi dp(200001, vi(2020, 0));

  int ans = 0;
  int N = int(S.length());
  rep(i, N) {
    int a = int(S[i] - '0');
    dp[i + 1][a]++;
    rep(j, 2019) {
      if (dp[i][j] == 0)
        continue;
      dp[i + 1][(j * 10 + a) % 2019] += dp[i][j];
      if ((j * 10 + a) % 2019 == 0)
        ans += dp[i][j];
    }
  }

  cout << ans << endl;

  // printf("Execution Time: %.4lf sec\\n", 1.0 * (clock() - ti) /
  // CLOCKS_PER_SEC);

  return 0;
}