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

// Catchharsh
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
const ll N = 2019;
ll freq[2019];
int32_t main() {
  IOS;

  string s;
  ll n, m, i, j, k;
  cin >> s;
  n = s.length();
  ll mul = 1;
  ll pre = 0;
  for (i = n - 1; i >= 0; i--) {
    ll x = (mul * (s[i] - '0') + pre) % N;
    // cout<<x<<endl;
    freq[x]++;
    pre = x;
    mul *= 10;
    pre %= N;
    mul %= N;
  }
  ll ans = 0;
  ans += (freq[0] * (freq[0] + 1)) / 2;
  for (i = 1; i < N; i++) {
    // if(freq[i]>=2)
    // cout<<freq[i]<<" "<<i<<endl;
    ans += (freq[i] * (freq[i] - 1)) / 2;
  }
  cout << ans;

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}

Error: TLE

Fixed submission:

// Catchharsh
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
// const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
const ll N = 2019;
const ll M = 200005;
ll seen[M][N];
int32_t main() {
  IOS;

  ll n, m, i, j, k;
  string s;
  cin >> s;
  n = s.length();
  ll ans = 0;
  for (i = 0; i < n - 4; i++) {
    ll rem = 0;
    ll temp = 0;
    for (j = i; j < n; j++) {
      rem = (10 * rem + (s[j] - '0'));
      if (rem >= N)
        rem %= N;
      if (seen[j][rem])
        break;
      seen[j][rem] = 1;
      if (rem == 0) {
        temp++;
      }
    }
    ans += temp * (temp + 1) / 2;
  }
  cout << ans;

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}