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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<double> vdbl;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<int>> vvint;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<int, vector<int>, greater<int>>
    spqint; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<int, vector<int>, less<int>> bpqint; // 大きい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); i++)
#define FOR(i, a, b) for (int(i) = a; (i) < (int)b; i++)
#define IREP(i, v) for (auto i = v.begin(); i != v.end(); i++)
#define MOD 1000000007
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define TS to_string
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const double PI = M_PI;
const int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
//-----------------------------------------

ll pow(ll x, ll y, ll m) {
  ll res = 1;
  if (y > 0) {
    res = pow(x, y / 2, m);
    if (y % 2 == 0) {
      res = (res * res) % m;
    } else {
      res = (((res * res) % m) * x) % m;
    }
  }
  return res;
}
//-----------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = SZ(s);
  vll cnt(2019, 0);
  vll sum(n);
  cnt[0]++;
  sum[n - 1] = s[n - 1] - '0';

  REP(i, n - 1) {
    sum[n - 2 - i] =
        sum[n - 1 - i] + (s[n - 2 - i] - '0') * pow(10, i + 1, 2019);
    sum[n - 2 - i] %= 2019;
  }

  REP(i, n) { cnt[sum[i]]++; }

  ll ans = 0;

  REP(i, 2019) { ans += (cnt[i] - 1) * cnt[i] / 2; }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
typedef vector<double> vdbl;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<int>> vvint;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<int, vector<int>, greater<int>>
    spqint; // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll; // 小さい順に取り出し
typedef priority_queue<int, vector<int>, less<int>> bpqint; // 大きい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll; // 大きい順に取り出し

#define REP(i, n) for (int(i) = 0; (i) < (int)(n); i++)
#define FOR(i, a, b) for (int(i) = a; (i) < (int)b; i++)
#define IREP(i, v) for (auto i = v.begin(); i != v.end(); i++)
#define MOD 2019
#define NIL -1
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define TS to_string
#define BS binary_search
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)

const int INF = 1e9;
const ll LINF = 1e18;
const double EPS = 1e-9;
const double PI = M_PI;
const int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }     // 最小公倍数

void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
//-----------------------------------------

//-----------------------------------------
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  ll n = SZ(s);
  ll ans = 0;

  REP(i, n - 1) {
    int a = s[i] - '0';
    if (a == 0) {
      ans++;
    }
    FOR(j, i + 1, n) {
      a = a * 10 + (s[j] - '0');
      a %= 2019;
      if (a == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;

  return 0;
}