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

#include <bits/stdc++.h>
#define pb push_back
#define el "\\n"
using namespace std;
using ll = long long;
using ull = unsigned long long;
typedef vector<ll> vl;
typedef vector<ull> vul;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<ll>> vvl;
typedef vector<vector<string>> vvs;
typedef vector<vector<ull>> vvul;
typedef vector<bool> vb;
const ll md = 1000000007;

ll fn(ll a, ll r, ll n, ll mod = md) {
  ll res = 1, x = r % mod;
  ll ans = 0;
  while (n) {
    if (n & 1)
      ans = (x * ans + res) % mod;
    res = ((res * x) % mod + res % mod) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  ans = (ans * a) % mod;
  return ans;
}

ll nCr(vl &fact, vl &invfact, ll n, ll r) {
  if (r == 0)
    return 1;
  ll num = fact[n];
  ll den = (invfact[r] % md * invfact[n - r] % md) % md;
  return (num * den) % md;
}

ll add(ll a, ll b, ll mod = md) {
  ll res = (a % mod + b % mod) % mod;
  return res;
}

ll mul(ll a, ll b, ll mod = md) {
  ll res = (a % mod * b % mod) % mod;
  return res;
}

ll mulm(ll a, ll b, ll mod = md) {
  ll r = 0;
  a %= mod;
  while (b) {
    if (b & 1)
      r = (r + a) % mod;
    a = (a + a) % mod;
    b = b / 2;
  }
  r %= mod;
  return r;
}

ll power(ll x, ll n, ll mod = md) {
  ll res = 1;
  x %= mod;
  while (n) {
    if (n & 1)
      res = (res * x) % mod;
    n = n / 2;
    x = (x * x) % mod;
  }
  return res;
}

ll power2(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (res * x);
    n = n / 2;
    x = (x * x);
  }
  return res;
}

bool is_prime(ll x) {
  if (x == 1 || x == 0)
    return false;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

ll compgcd(ll n, vector<ll> const &primes) {
  ll res = n;
  for (ll i = 0; primes[i] * primes[i] <= n; i++) {
    if (n % primes[i] == 0) {
      res -= (res / primes[i]);
      while (n % primes[i] == 0) {
        n /= primes[i];
      }
    }
  }
  if (n > 1) {
    res -= (res / n);
  }
  return res;
}

void sieve(vector<bool> &prime, vector<ll> &primes, ll lim) {
  for (int i = 2; i * i <= lim; i++) {
    if (prime[i]) {
      for (int j = 2; j * i <= lim; j++) {
        prime[j * i] = false;
      }
    }
  }
  for (int i = 2; i <= lim; i++) {
    if (prime[i]) {
      primes.push_back(i);
    }
  }
}

void sieveGCD(ll lim, vector<ll> &tab, vector<ll> const &primes) {
  for (int i = 1; i <= lim; i++) {
    tab[i] = i;
  }
  for (int i = 0; i < primes.size() && primes[i] <= lim; i++) {
    tab[primes[i]] = primes[i] - 1;
    for (int j = 2; primes[i] * j <= lim; j++) {
      tab[primes[i] * j] = (tab[primes[i] * j] / primes[i]) * (primes[i] - 1);
    }
  }
}

// void findPermutations(vl a, int n) {
//     sort(a.begin(), a.end());
//     do {
//         for(int i = 0; i < n; i++) {
//             st[x].pb(a[i]);
//         }
//         x++;
//     } while (next_permutation(a.begin(), a.end()));
// }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  ll num = 0, ten = 1, res = 0;
  map<ll, ll> f;
  f[0]++;
  for (int i = n - 1; i >= 0; i--) {
    num = (num + ten * (s[i] - 48)) % 2019;
    ten = (ten * 10) % 2019;
    num %= 2019;
    res += f[num];
    f[num]++;
  }
  cout << res << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

int mod(string num, int a) {
  int res = 0;

  for (int i = 0; i < num.length(); i++)
    res = (res * 10 + (int)num[i] - '0') % a;

  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  ll count = 0;
  int n = s.size();
  for (int l = 4; l <= n; l++) {
    bool found = false;
    for (int i = 0; i < (n / l) * l; i += (found == true ? l - 1 : 1)) {
      string tmp = s.substr(i, l);
      ll res = mod(tmp, 2019);
      if (res == 0 && tmp.size() == l) {
        if (found == false)
          found = true;
        count++;
      }
    }
  }
  cout << count << "\\n";
  return 0;
}