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

/*
    JAI JAGANNATH!
*/
//@Author : zanj0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<lli, lli>
#define vi vector<lli>
#define mii map<lli, lli>
#define pqb priority_queue<lli>
#define pqs priority_queue<lli, vi, greater<lli>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  lli x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<lli, null_type, less<lli>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void zanj0() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
void solve() {
  string s;
  cin >> s;
  lli dp[2019];
  for (lli i = 0; i < 2019; i++)
    dp[i] = 0;
  dp[s[0] - '0'] = 1;
  lli ret = 0;
  lli n = s.size();
  for (lli i = 1; i < n; i++) {
    lli dp2[2019];
    for (lli i = 0; i < 2019; i++)
      dp2[i] = 0;
    dp2[s[i] - '0']++;
    for (lli j = 0; j < 2019; j++) {
      if (dp[j] > 0) {
        // cout << j << " " << i << endl;
        lli rem = j * 10 + (s[i] - '0');
        rem %= 2019;
        if (rem == 0)
          ret += dp[j];
        // cout << rem << endl;
        dp2[rem] += dp[j];
      }
    }
    for (lli j = 0; j < 2019; j++) {
      dp[j] = dp2[j];
    }
  }
  cout << ret << endl;
}
int main() {
  zanj0();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/*
    JAI JAGANNATH!
*/
//@Author : zanj0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int lli;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<lli, lli>
#define vi vector<lli>
#define mii map<lli, lli>
#define pqb priority_queue<lli>
#define pqs priority_queue<lli, vi, greater<lli>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  lli x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<lli, null_type, less<lli>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void zanj0() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
void solve() {
  string s;
  cin >> s;
  lli n = s.size();
  lli x = 2019;

  lli ret = 0;
  unordered_set<lli> ss[n + 1];
  for (lli i = 0; i < n; i++) {
    lli rem = 0;
    lli pref = 0;
    for (lli j = i; j < n; j++) {

      rem *= 10;
      rem += (s[j] - '0');
      rem %= x;
      if (ss[j].count(rem)) {
        break;
      }
      ss[j].insert(rem);
      if (rem == 0)
        pref++;
    }
    ret += (pref * (pref + 1)) / 2;
  }
  cout << ret << endl;
}

/*for (lli i = 0; i < s.size(); i++) {
        temp *= 10;
        temp += (s[i] - '0');
        temp %= 2019;
        cout << temp << endl;
        if (temp == 0) {
                temp = s[i] - '0';
        }
}*/

int main() {
  zanj0();
  solve();
  return 0;
}