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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll, ll> pii;
const ll INF = 1LL << 58;
const ll MOD = 1000000007;
typedef vector<pii> vpii;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  string S;
  cin >> S;

  int m[2100];
  REP(i, 2100) { m[i] = 0; }
  m[0] = 1;
  int ten = 1;
  int temp = 0;
  REP(i, S.size()) {
    ten %= 2019;
    temp += (ten * stoi(S.substr(S.size() - 1 - i, 1)));
    temp %= 2019;
    ten *= 10;
    m[temp]++;
  }
  int ans = 0;
  REP(i, 2019) { ans += (m[i] * (m[i] - 1)) / 2; }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll, ll> pii;
const ll INF = 1LL << 58;
const ll MOD = 1000000007;
typedef vector<pii> vpii;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

string tmp = "2019";

bool add(int idx, string s) {
  string t;
  idx = tmp.size() - 1 - idx;
  if (idx < 0) {
    t = '1';
    tmp = t + tmp;
    return false;
  }
  t = tmp[idx];
  if (s == "2") {
    if (t == "0") {
      tmp[idx] = '2';
      return false;
    } else if (t == "1") {
      tmp[idx] = '3';
      return false;
    } else if (t == "2") {
      tmp[idx] = '4';
      return false;
    } else if (t == "3") {
      tmp[idx] = '5';
      return false;
    } else if (t == "4") {
      tmp[idx] = '6';
      return false;
    } else if (t == "5") {
      tmp[idx] = '7';
      return false;
    } else if (t == "6") {
      tmp[idx] = '8';
      return false;
    } else if (t == "7") {
      tmp[idx] = '9';
      return false;
    } else if (t == "8") {
      tmp[idx] = '0';
      return true;
    } else if (t == "9") {
      tmp[idx] = '1';
      return true;
    }
  } else if (s == "1") {
    if (t == "0") {
      tmp[idx] = '1';
      return false;
    } else if (t == "1") {
      tmp[idx] = '2';
      return false;
    } else if (t == "2") {
      tmp[idx] = '3';
      return false;
    } else if (t == "3") {
      tmp[idx] = '4';
      return false;
    } else if (t == "4") {
      tmp[idx] = '5';
      return false;
    } else if (t == "5") {
      tmp[idx] = '6';
      return false;
    } else if (t == "6") {
      tmp[idx] = '7';
      return false;
    } else if (t == "7") {
      tmp[idx] = '8';
      return false;
    } else if (t == "8") {
      tmp[idx] = '9';
      return false;
    } else if (t == "9") {
      tmp[idx] = '0';
      return true;
    }
  } else if (s == "9") {
    if (t == "0") {
      tmp[idx] = '9';
      return false;
    } else if (t == "1") {
      tmp[idx] = '0';
      return true;
    } else if (t == "2") {
      tmp[idx] = '1';
      return true;
    } else if (t == "3") {
      tmp[idx] = '2';
      return true;
    } else if (t == "4") {
      tmp[idx] = '3';
      return true;
    } else if (t == "5") {
      tmp[idx] = '4';
      return true;
    } else if (t == "6") {
      tmp[idx] = '5';
      return true;
    } else if (t == "7") {
      tmp[idx] = '6';
      return true;
    } else if (t == "8") {
      tmp[idx] = '7';
      return true;
    } else if (t == "9") {
      tmp[idx] = '8';
      return true;
    }
  }
  return false;
}

void getmp() {
  bool kuri = false;
  int idx = 0;
  kuri = add(idx, "9");
  while (kuri) {
    idx++;
    kuri = add(idx, "1");
  }

  idx = 1;
  kuri = add(idx, "1");
  while (kuri) {
    idx++;
    kuri = add(idx, "1");
  }

  idx = 3;
  kuri = add(idx, "2");
  while (kuri) {
    idx++;
    kuri = add(idx, "1");
  }
}

signed main() {
  //  以降 cin の入力元が 'input.txt' になる
  // std::ifstream in("input.txt");
  // std::cin.rdbuf(in.rdbuf());
  string S;
  cin >> S;
  int ans = 0;
  while (tmp.size() < 10) {
    int len = tmp.size();
    int i = 0;
    while (1) {
      if (i + len - 1 > S.size())
        break;
      if (S.substr(i, len) == tmp)
        ans++;
      i++;
    }

    getmp();
  }
  cout << ans << endl;

  return 0;
}