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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M1 1000000007
#define M2 998244353
#define ll long long int
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define F first
#define S second
#define PB push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define V(a) vector<a>
#define endl '\\n'
#define test(t) while (t--)
#define PI acos(-1.0)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repp(i, b, a) for (ll i = b - 1; i >= a; i--)
#define clr(ar, val) memset(ar, val, sizeof(ar))
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(y) fixed << setprecision(y)
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};

unsigned int randInt() {
  static unsigned int tx = 123456789, ty = 362436069, tz = 521288629,
                      tw = 88675123;
  unsigned int tt = (tx ^ (tx << 11));
  tx = ty;
  ty = tz;
  tz = tw;
  return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
#define o_set(T)                                                               \\
  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

ll floor_div(ll a, ll b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll ceil_div(ll a, ll b) { return a / b + (((a ^ b) >= 0) and a % b); }

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void solve() {
  string s;
  cin >> s;
  // rep(i,0,2019){
  //     dp[0][i] = dp[1][i] = dp[2][i] = 0;
  // }
  ll ans = 0;
  ll dp[2019];
  clr(dp, 0);
  dp[s[0] - '0'] = 1;
  rep(i, 1, s.size()) {
    int d = s[i] - '0';
    ll tdp[2019];
    clr(tdp, 0);
    tdp[d] = 1;
    rep(j, 0, 2019) {
      if (dp[j] > 0) {
        // cout << i << " " << j << endl;
        ll rem = (10 * j + d) % 2019;
        if (rem == 0)
          ans += dp[j];
        tdp[rem] += dp[j];
      }
    }
    rep(i, 0, 2019) { dp[i] = tdp[i]; }
  }
  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  test(t) { solve(); }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M1 1000000007
#define M2 998244353
#define ll long long int
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define F first
#define S second
#define PB push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define V(a) vector<a>
#define endl '\\n'
#define test(t) while (t--)
#define PI acos(-1.0)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repp(i, b, a) for (ll i = b - 1; i >= a; i--)
#define clr(ar, val) memset(ar, val, sizeof(ar))
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(y) fixed << setprecision(y)
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {-1, 0, 1, 0};

unsigned int randInt() {
  static unsigned int tx = 123456789, ty = 362436069, tz = 521288629,
                      tw = 88675123;
  unsigned int tt = (tx ^ (tx << 11));
  tx = ty;
  ty = tz;
  tz = tw;
  return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
#define o_set(T)                                                               \\
  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// member functions :
// 1. order_of_key(k) : number of elements strictly lesser than k
// 2. find_by_order(k) : k-th element in the set

ll floor_div(ll a, ll b) {
  return a / b - (((a ^ b) < 0) and a % b);
}
ll ceil_div(ll a, ll b) { return a / b + (((a ^ b) >= 0) and a % b); }

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

set<ll> dp[200005];
void solve() {
  string s;
  cin >> s;
  // rep(i,0,2019){
  //     dp[0][i] = dp[1][i] = dp[2][i] = 0;
  // }
  ll ans = 0;
  dp[0].insert(s[0] - '0');
  rep(i, 1, s.size()) {
    int d = s[i] - '0';
    dp[i].insert(d);
    for (ll j : dp[i - 1]) {
      ll rem = (10 * j + d) % 2019;
      if (rem == 0)
        ans++;
      dp[i].insert(rem);
    }
  }

  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  test(t) { solve(); }
  return 0;
}