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

#include <bits/stdc++.h>
using namespace std;

#define str(a) to_string(a)
#define int long long
#define pb push_back
#define mp make_pair
#define SORT(c) sort(c.begin(), c.end())
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vector<int>, greater<int>>
#define mod 1000000007
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define inf LLONG_MAX
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"

int modofbignum(string a, int b = 2019) {
  int res = 0;
  for (int i = 0; i < a.length(); i++) {
    res = (res * 10 + (a[i] - '0')) % b;
  }
  return res;
}
int power(int a, int b, int c = 2019) {
  if (b == 0)
    return 1;
  int short_ans = power(a, b / 2);
  short_ans = (short_ans % c * short_ans % c) % c;
  if (b & 1)
    return (a % c * short_ans % c) % c;
  return short_ans;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  unordered_map<int, int> um;
  int res = 0;
  int count = 0;
  int n = s.length() - 1;
  um[0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    res = (res % 2019 + (power(10, n - i) * (s[i] - '0') % 2019) % 2019) % 2019;
    if (um.find(res) == um.end())
      um[res]++;
    else {
      count += um[res];
      um[res]++;
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define str(a) to_string(a)
#define int long long
#define pb push_back
#define mp make_pair
#define SORT(c) sort(c.begin(), c.end())
#define max_heap priority_queue<int>
#define min_heap priority_queue<int, vector<int>, greater<int>>
#define mod 1000000007
#define setbits(x) __builtin_popcount(x)
#define ps(x, y) fixed << setprecision(y) << x
#define start(t)                                                               \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define inf LLONG_MAX
#define sync                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"

int modofbignum(string a, int b = 2019) {
  int res = 0;
  for (int i = 0; i < a.length(); i++) {
    res = (res * 10 + (a[i] - '0')) % b;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  unordered_map<int, int> um;
  for (int i = 0; i < s.length(); i++) {
    // cout << s.substr(i, s.length() - i) << " " << modofbignum(s.substr(i,
    // s.length() - i)) << endl;
    um[modofbignum(s.substr(i, s.length() - i))]++;
  }
  int count = 0;
  for (auto m : um) {
    if (m.first == 0)
      count += 1;
    if (m.second > 1)
      count += m.second / 2;
  }
  cout << count << endl;
}