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

/*********************************************JMD**********************************************/
//                                  |\\  |     | |   | /\\ //
//                              | \\ |     | |---| /--\\ //
//                          |  \\| |___| |   | /    \\1999                     //
/*******************************************************************************************/
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define mem(x, val) memset(x, val, sizeof(x))
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) forr(i, 0, n)
#define sz(x) ll((x).size())
#define all(x) ((x).begin(), (x).end())
#define asc(x) ((int)(x))
#define precision(x) cout << fixed << setprecision((x));

const ll mod = 1e9 + 7;

/*******************************************************************************************/
vector<ll> modulo(201900);
void solve() {
  string s;
  cin >> s;

  ll n = s.size();

  ll a[n], ans = 0;
  f(i, n) a[i] = s[i] - '0';

  vector<ll> dp(2019);
  dp[a[0]] = 1;

  for (ll i = 1; i < n; i++) {
    vector<ll> temp(2019);
    temp[a[i]] = 1;
    f(j, 2019) {
      ll rem = (j * 10) + a[i];
      rem = modulo[rem];
      temp[rem] += dp[j];
    }
    ans += temp[0];
    dp = temp;
  }
  cout << ans << "\\n";

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  f(i, 201900) { modulo[i] = i % 2019; }
  ll t = 1;
  // cin>>t;
  clock_t launch = clock();

  while (t--) {
    solve();
  }

  clog << ((long double)(clock() - launch) / CLOCKS_PER_SEC) << "\\n";
}


Error: MLE

Fixed submission:

/*********************************************JMD**********************************************/
//                                  |\\  |     | |   | /\\ //
//                              | \\ |     | |---| /--\\ //
//                          |  \\| |___| |   | /    \\1999                     //
/*******************************************************************************************/
#include <bits/stdc++.h>
using namespace std;

typedef int ll;
typedef unsigned long long int ull;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define mem(x, val) memset(x, val, sizeof(x))
#define forr(i, a, b) for (ll i = a; i < b; i++)
#define f(i, n) forr(i, 0, n)
#define sz(x) ll((x).size())
#define all(x) ((x).begin(), (x).end())
#define asc(x) ((int)(x))
#define precision(x) cout << fixed << setprecision((x));

const ll mod = 1e9 + 7;

/*******************************************************************************************/
vector<ll> modulo(201900);
int dp[200007][2019];
void solve() {
  string s;
  cin >> s;

  ll n = s.size();

  ll a[n];
  f(i, n) a[i] = s[i] - '0';

  f(i, n) f(j, 2019) dp[i][j] = 0;

  dp[0][a[0]] = 1;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll v = a[i];
    dp[i][v] = 1;
    f(j, 2019) {
      ll rem = ((j * 10) + v);
      rem = modulo[rem];
      dp[i][rem] += dp[i - 1][j];
    }
    // cout<<dp[i][0]<<endl;
    ans += dp[i][0];
  }

  cout << ans << "\\n";

  return;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  f(i, 201900) { modulo[i] = i % 2019; }
  ll t = 1;
  // cin>>t;
  clock_t launch = clock();

  while (t--) {
    solve();
  }

  clog << ((long double)(clock() - launch) / CLOCKS_PER_SEC) << "\\n";
}