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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bits/stdc++.h> // isupper, islower, isdigit, toupper, tolower
#include <bitset>        // bitset
#include <cmath>         // sqrt
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#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 <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pl;
typedef pair<int, int> P;
typedef tuple<ll, ll, string> Tlls;

// Greatest Common Divisor (最大公約数)
ll gcd(ll a, ll b) {
  // Eucledean algorithm
  while (a) {
    if (a < b) {
      std::swap(a, b);
    }
    a %= b;
  }
  return b;
}

// Least Common Multiple 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// key: value of prime factor素因数分解
// value: number of prime factor
map<ll, ll> prime_factorization(ll n) {
  map<ll, ll> prime_factors;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      prime_factors[i]++;
      n /= i;
    }
  }
  if (n != 1)
    prime_factors[n] = 1;
  return prime_factors;
}

// 約数リスト約数の列挙
vector<ll> get_divisors(const ll n) {
  vector<ll> divs;
  for (ll i = 1; i < (ll)sqrt((ld)n) + 1; i++) {
    if (n % i == 0) {
      divs.push_back(i);
      if (i != n / i) {
        divs.push_back(n / i);
      }
    }
  }

  // 必要に応じてソート
  // sort(divs.begin(), divs.end());

  return divs;
}

int main() {
  int m = 2019;
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int total = 0;
  vector<int> count(m);
  ll ans = 0;

  rep(i, n) {
    count.at(total)++;
    total += (s.at(i) - '0') * x;
    total %= m;
    ans += count.at(total);
    x = x * 10 % m;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bits/stdc++.h> // isupper, islower, isdigit, toupper, tolower
#include <bitset>        // bitset
#include <cmath>         // sqrt
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <deque>         // deque
#include <iostream>      // cout, endl, cin
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <sstream>
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> Pl;
typedef pair<int, int> P;
typedef tuple<ll, ll, string> Tlls;

// Greatest Common Divisor (最大公約数)
ll gcd(ll a, ll b) {
  // Eucledean algorithm
  while (a) {
    if (a < b) {
      std::swap(a, b);
    }
    a %= b;
  }
  return b;
}

// Least Common Multiple 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

// key: value of prime factor素因数分解
// value: number of prime factor
map<ll, ll> prime_factorization(ll n) {
  map<ll, ll> prime_factors;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      prime_factors[i]++;
      n /= i;
    }
  }
  if (n != 1)
    prime_factors[n] = 1;
  return prime_factors;
}

// 約数リスト約数の列挙
vector<ll> get_divisors(const ll n) {
  vector<ll> divs;
  for (ll i = 1; i < (ll)sqrt((ld)n) + 1; i++) {
    if (n % i == 0) {
      divs.push_back(i);
      if (i != n / i) {
        divs.push_back(n / i);
      }
    }
  }

  // 必要に応じてソート
  // sort(divs.begin(), divs.end());

  return divs;
}

int main() {
  string S;
  string subS;
  cin >> S;
  long long dp[S.length()][2]; // dp[上から i 桁まで][ smaller ]

  ll left_length = 0;
  ll right_length = 1;
  ll length = S.length();
  std::istringstream ss;
  set<pair<ll, ll>> ans;

  for (left_length = 0; left_length <= length; left_length++) {
    for (right_length = 4; left_length + right_length <= length;
         right_length++) {
      subS = S.substr(left_length, right_length);
      // cout << subS << endl;
      ss = istringstream(subS);
      ll tmp;
      ss >> tmp;
      if (tmp % 2019 == 0) {
        // cout << "Found " << tmp << endl;
        pair<ll, ll> P;
        P.first = left_length;
        P.second = right_length;
        ans.insert(P);
      }
    }
  }
  cout << ans.size() << endl;

  return 0;
}