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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "YES" << endl;                                                     \\
  } else {                                                                     \\
    cout << "NO" << endl;                                                      \\
  }
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
  int to, cost;
};
struct pos {
  int x, y, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};

ll gcd(ll a2, ll b2) {
  ll t;
  while (b2 != 0) {
    t = a2 % b2;
    a2 = b2;
    b2 = t;
  }
  return a2;
}

ll d[2019][3] = {};

int main() {

  string s;
  cin >> s;

  ll t = 0;
  for (int i = 0; i < s.size(); i++) {
    int x;
    if (s[i] == '1')
      x = 1;
    if (s[i] == '2')
      x = 2;
    if (s[i] == '3')
      x = 3;
    if (s[i] == '4')
      x = 4;
    if (s[i] == '5')
      x = 5;
    if (s[i] == '6')
      x = 6;
    if (s[i] == '7')
      x = 7;
    if (s[i] == '8')
      x = 8;
    if (s[i] == '9')
      x = 9;

    rep(i, 2019) {
      d[i][1] = d[i][2];
      d[i][2] = 0;
    }
    d[x][2]++;
    for (int j = 0; j < 2019; j++) {
      if (d[j][1] == 0)
        continue;
      d[(j * 10 + x) % 2019][2] += d[j][1];
    }
    t += d[0][2];
  }
  cout << t << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \\
  if (flg) {                                                                   \\
    cout << "YES" << endl;                                                     \\
  } else {                                                                     \\
    cout << "NO" << endl;                                                      \\
  }
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, int> Pi;
typedef pair<double, Pi> Pdi;
typedef pair<ll, int> Pli;
typedef pair<P1, ll> P2;
const ll INF = 100000000000000001;
struct edge {
  int to, cost;
};
struct pos {
  int x, y, cost;
};
int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
struct Road {
  double cost;
  int a, b;
};

ll gcd(ll a2, ll b2) {
  ll t;
  while (b2 != 0) {
    t = a2 % b2;
    a2 = b2;
    b2 = t;
  }
  return a2;
}

int d[200002] = {};

int main() {

  string s;
  cin >> s;

  ll t = 0;
  for (int i = 0; i < s.size(); i++) {
    int x;
    if (s[i] == '1')
      x = 1;
    if (s[i] == '2')
      x = 2;
    if (s[i] == '3')
      x = 3;
    if (s[i] == '4')
      x = 4;
    if (s[i] == '5')
      x = 5;
    if (s[i] == '6')
      x = 6;
    if (s[i] == '7')
      x = 7;
    if (s[i] == '8')
      x = 8;
    if (s[i] == '9')
      x = 9;
    d[i] = x;
    for (int j = 0; j < i; j++) {
      d[j] = (d[j] * 10 + x) % 2019;
      if (d[j] == 0)
        t++;
    }
  }
  cout << t << endl;
  return 0;
}