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

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

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

  vector<int> a(n);
  rep(i, n) { a[i] = (int)s[i] - 48; }

  ll ans = 0;

  vector<unsigned short> pre(2019), now(2019), all0(2019);

  rep(i, n) {
    std::vector<unsigned short>().swap(pre);
    pre = now;
    now = all0;
    if (i == 0) {
      now[a[i]]++;
    } else {
      now[a[i]]++;
      rep(j, 2019) {
        int z = (10 * j + a[i]) % 2019;
        now[z] += pre[j];
      }
    }
    ans += now[0];
  }

  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define MOD 1000000007
#define SORT(s) sort(s.begin(), s.end());
#define REVERSE(s) reverse(s.begin(), s.end());
#define SUM(s) accumulate(s.begin(), s.end(), 0LL);
#define P pair<int, int>
#define mp make_pair
#define valid(x, y, h, w) (0 <= x && x < h && 0 <= y && y < w)
// #define int ll
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ndx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ndy[8] = {1, 0, -1, 0, 1, -1, -1, 1};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }
double log(double a, double b) { return log(b) / log(a); }

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

  vector<vector<int>> dp(n);
  vector<int> a(n);
  rep(i, n) { a[i] = (int)s[i] - 48; }
  rep(i, n) { dp[i].resize(2019); }

  ll ans = 0;

  vector<int> pre(2019), now(2019), all0(2019);

  rep(i, n) {
    pre = now;
    now = all0;
    if (i == 0) {
      now[a[i]]++;
    } else {
      now[a[i]]++;
      rep(j, 2019) {
        int z = (10 * j + a[i]) % 2019;
        now[z] += pre[j];
      }
    }
    ans += now[0];
  }

  cout << ans << endl;
}