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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pi pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int MOD = 2019;
const int INF = 1e18 + 10;

/*----------------------------------------------*/
int modpow(int x, int n) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % MOD;
    x = (x * x) % MOD;
    n >>= 1;
  }
  return res;
}

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  vi t(n + 3, 0);
  rep(i, n) {
    t[n - 1 - i] = (t[n - i] + (s[n - 1 - i] - '0') * modpow(10, i)) % 2019;
  }
  vi cnt(2019, 0);
  rep(i, n + 1) {
    cnt[t[i]]++;
    // if(cnt[t[i]]>1) cout<<i<<endl;
  }
  int ans = 0;
  rep(i, 2019) {
    ans += cnt[i] * (cnt[i] - 1) / 2;
    // if(cnt[i])cout<<cnt[i]<<endl;
  }
  // cout<<endl;
  cout << ans << endl;
  // rep(i,n) cout<<t[i]<<endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pi pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const int INF = 1e18 + 10;

/*----------------------------------------------*/

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  if (n < 4) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  rep(i, n - 3) {
    int sub = (s[i] - '0') * 100 + (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
    FOR(j, i + 3, n) { // s[i]->s[j]
      sub *= 10;
      sub += s[j] - '0';
      sub %= 2019;
      if (sub % 2019 == 0) {
        ans++;
        // j+=3;
        // cout<<i<<' '<<j<<' '<<sub<<endl;
      }
    }
  }
  // cout<<endl;

  cout << ans << endl;

  return 0;
}