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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion
#pragma region chminmax
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion

const int MAX_N = 2e5 + 5;
vint dpa(2019), dpb(2019);

int main() {
  string s;
  cin >> s;
  int n = s.length();

  ll ans = 0;
  rep(i, n) {
    int now = s[i] - '0';

    rep(j, 2019) {
      int here = (j * 10) + now;
      here %= 2019;
      dpa[here] += dpb[j];
    }
    ans += dpa[0];

    dpa[now]++;
    dpb = dpa;
    dpa = vint(2019);
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a) { return is >> a.first >> a.second; }
ostream &operator<<(ostream &os, const P &a) {
  return os << "(" << a.first << "," << a.second << ")";
}

template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
#pragma endregion
#pragma region chminmax
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#pragma endregion

const int MAX_N = 2e5 + 5;
int dp[MAX_N][2019];

int main() {
  string s;
  cin >> s;
  int n = s.length();

  ll ans = 0;
  rep(i, n) {
    int now = s[i] - '0';
    //     rep(j, 673)
    //     {
    //         int here = (j * 10) + now;
    //         here %= 673;
    //         dp[i + 1][here] += dp[i][j];
    //     }
    //     rep(j, 3)
    //     {
    //         int here = j + now;
    //         here %= 3;
    //         dp3[i + 1][here] += dp3[i][j];
    //     }

    //     ans += min(dp[i + 1][0], dp3[i + 1][0]);

    //     dp[i + 1][now]++;
    //     dp3[i + 1][now % 3]++;
    // }
    dp[i + 1][now]++;
    rep(j, 2019) {
      int here = (j * 10) + now;
      here %= 2019;
      dp[i + 1][here] += dp[i][j];
    }
    ans += dp[i + 1][0];
  }

  cout << ans << endl;

  return 0;
}