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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
using namespace std;

#define M 2019
ll mpow(ll x, ll n) {
  for (int i = 0; i < n; i++) {
    x *= n;
    x %= M;
  }

  return M;
}

int ctoi(char c) { return c - '0'; }
int main() {
  string S;
  cin >> S;
  vi temp(2019), curr(2019);
  int x = ctoi(S[0]);
  temp[x]++;
  ll ans = 0;
  for (int i = 1; i < S.size(); i++) {
    vi curr(2019);
    int u = ctoi(S[i]);
    for (int j = 0; j < 2019; j++) {
      int v = (10 * j + u) % M;
      curr[v] = temp[j];
    }
    curr[u]++;

    ans += curr[0];

    temp = curr;
  }

  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
using namespace std;

#define M 2019
ll mpow(ll x, ll n) {
  for (int i = 0; i < n; i++) {
    x *= n;
    x %= M;
  }

  return M;
}

int ctoi(char c) { return c - '0'; }
int main() {
  string S;
  cin >> S;

  vvi dp(S.size(), vi(2019));
  vi temp(2019), curr(2019);
  int x = ctoi(S[0]);
  temp[x]++;
  ll ans = 0;
  for (int i = 1; i < S.size(); i++) {
    vi curr(2019);
    int u = ctoi(S[i]);
    for (int j = 0; j < 2019; j++) {
      int v = (10 * j + u) % M;
      curr[v] = temp[j];
    }
    curr[u]++;

    ans += curr[0];

    temp = curr;
  }

  cout << ans << endl;
}