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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

ll GetDigit(ll num) { return log10(num) + 1; }

// n ~ mの和
ll sigma(ll n, ll m) { return ((n + m) * (m - n + 1) * 0.5); }

void Main() {
  str s;
  cin >> s;
  reverse(all(s));
  ll rui = 0;
  vll b(2019);
  ll ans = 0;
  ll h = 1;
  rep(i, s.size()) {
    b[rui]++;
    rui += h * (s[i] - '0');
    rui %= 2019;
    ans += b[rui];
    h *= 10;
    h %= 2019;
  }
  fin(ans);
}

// 前処理
int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cout << fixed << setprecision(20); // 高精度少数表示
  // cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T> void fin(T a) {
  cout << a << endl;
  exit(0);
}

// 桁数
ll GetDigit(ll num) { return log10(num) + 1; }

// 約数全列挙
vector<ll> enum_div(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  sort(begin(ret), end(ret));
  return ret;
}

// 累乗(xのn乗%mod)
ll modpow(ll x, ll n, ll mod = MAX) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// 階乗
ll factorial(ll n) {
  if (n > 0)
    return n * factorial(n - 1);
  else
    return 1;
}

void Main() {
  str s;
  cin >> s;
  ll ans = 0;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = i + 3; j < s.size(); j++) {
      str S = s.substr(i, j + 1 - i);
      ll a = 0;
      for (ll k = 0; k < S.size(); k++) {
        a *= 10;
        a += (S[k] - '0');
      }
      if (a % 2019 == 0) {
        ans++;
        break;
      }
    }
  }
  fin(ans);
}

// 前処理
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20); // 高精度少数表示
  Main();
  return 0;
}

/*
//test時
  g++ test.cpp -o test;./test
*/