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

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 1000000007;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}

void solve() {
  string s;
  cin >> s;
  ll n = s.length();
  vector<ll> dp(2019);
  dp[s[0] - '0'] = 1;
  ll i, j;
  ll ans = 0;
  for (i = 1; i < n; i++) {
    ll d = s[i] - '0';
    vector<ll> dpc(2019);
    dpc[d] = 1;
    for (j = 0; j < 2019; j++) {
      if (dp[j] > 0) {
        ll r = (10 * j + d) % 2019;
        dpc[r] += dp[j];
        if (r == 0)
          ans += dp[j];
      }
    }
    dp = dpc;
  }
  cout << ans;
}

int main() {
  ios;
  ll t;
  t = 1;
  //    cin>>t;
  while (t--)
    solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define pll pair<long long, long long>
#define pld pair<long double, long double>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
using namespace std;
ll mod = 1000000007;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll fastexp(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}

ll modularExp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(ll n) {
  for (ll i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
bool mycomp(pll &a, pll &b) {
  if (a.F != b.F)
    return a.F < b.F;
  else
    return a.S < b.S;
}
vector<ll> par(26);

ll froot(ll x) {
  ll y = x;
  while (x != par[x])
    x = par[x];
  par[y] = x;
  return x;
}

void solve() {
  string s;
  cin >> s;
  ll n = s.length();
  ll k = 2019LL;
  set<ll> v[n];
  ll ans = 0;
  ll i, j;
  for (i = 0; i < n; i++) {
    ll r = 0;
    ll found = 0;
    for (j = i; j < n; j++) {
      ll x = s[j] - '0';
      r = (10 * r + x) % k;
      if (v[j].find(r) != v[j].end())
        break;
      else
        v[j].insert(r);
      if (r == 0)
        found += 1;
    }
    ans += ((found) * (found + 1)) / 2;
  }
  cout << ans;
}

int main() {
  ios;
  ll t;
  t = 1;
  //    cin>>t;
  while (t--)
    solve();
}