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

/// 11:59 AM 26/04/20
/// Author: t_bone_289 (An Immature Programmer.... I'm still developing !!)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef int ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

template <typename T> void ContainerError(T &v);

ll powmod(ll base, ll exp, ll MOD);

ll mpow(ll base, ll exp);

ll gcd(ll a, ll b);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string s;
  cin >> s;
  ll k = 2019;
  ll n = s.size();
  ll ans = 0;
  vector<ll> v(k + 1);
  ll c = 0;
  ll cf = 1;
  v[0]++;
  for (ll i = 0; i < n; i++) {
    c = (c + cf * (s[n - i - 1] - '0') % k) % k;
    cf = cf * 10 % k;
    v[c]++;
  }
  for (ll i = 0; i < k; i++) {
    ans += v[i] * (v[i] - 1) / 2;
  }
  cout << ans;
  return 0;
}

template <typename T> void ContainerError(T &v) {
  cout << "{";
  for (auto &x : v) {
    cout << x << ',';
  }
  cout << "}" << endl;
  return;
}

ll powmod(ll base, ll exp, ll MOD) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}

ll mpow(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return (res);
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }


Error: TLE

Fixed submission:

/// 11:59 AM 26/04/20
/// Author: t_bone_289 (An Immature Programmer.... I'm still developing !!)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

template <typename T> void ContainerError(T &v);

ll powmod(ll base, ll exp, ll MOD);

ll mpow(ll base, ll exp);

ll gcd(ll a, ll b);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string s;
  cin >> s;
  ll k = 2019;
  ll n = s.size();
  ll ans = 0;
  vector<set<ll>> v(n);
  for (ll i = 0; i < n; i++) {
    ll r = 0;
    ll p = 0;
    for (ll j = i; j < n; j++) {
      r = (10 * r + s[j] - '0') % k;
      if (v[j].find(r) != v[j].end()) {
        break;
      }
      v[j].insert(r);
      if (r == 0) {
        p++;
      }
    }
    ans += p * (p + 1) / 2;
  }
  cout << ans;
  return 0;
}

template <typename T> void ContainerError(T &v) {
  cout << "{";
  for (auto &x : v) {
    cout << x << ',';
  }
  cout << "}" << endl;
  return;
}

ll powmod(ll base, ll exp, ll MOD) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return (res % MOD);
}

ll mpow(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return (res);
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }