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

/*input
1817181712114



*/
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>

using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gc getchar_unlocked
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define ms(a, x) memset(a, x, sizeof(a));
#define endl "\\n"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 1000000001;
const int MAX = 1e5 + 1;

int fast_exp(int base, int exp, int MOD) {
  int res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base) % MOD;
    base = (base * base) % MOD;
    exp /= 2;
  }
  return res % MOD;
}

int32_t main() {
  IOS;
  int n, p = 2019;
  string s;
  cin >> s;
  n = s.length();
  map<int, int> mx;
  int count = 0;
  int sum = 0;
  int ans = 0;
  mx[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    sum += ((s[i] - '0') * fast_exp(10, count, p)) % p;

    sum = sum % p;
    // cout<<sum<<endl;
    mx[sum]++;
    ans += (mx[sum] - 1);
    count++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*input
14282668646


*/
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>

using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define gc getchar_unlocked
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define int long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\\n", x)
#define pl(x) printf("%lld\\n", x)
#define ps(s) printf("%s\\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define ms(a, x) memset(a, x, sizeof(a));
#define endl "\\n"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 1000000001;
const int MAX = 1e5 + 1;

int32_t main() {
  IOS;
  string s;
  cin >> s;
  int n = s.size();
  unordered_map<int, int> mx;
  vector<set<int>> seenRemainders(n);
  int ans = 0, num = 0;
  int numMultiples = 0;
  int k = 2019;
  for (int i = 0; i < n; i++) {

    int remainder = 0;
    int prefixesFound = 0;
    for (int j = i; j < n; j++) {
      remainder = (10 * remainder + s[j] - '0') % k;
      if (seenRemainders[j].find(remainder) != seenRemainders[j].end())
        break;

      seenRemainders[j].insert(remainder);
      if (remainder == 0)
        prefixesFound++;
    }
    numMultiples += (prefixesFound * (prefixesFound + 1)) / 2;
  }
  cout << numMultiples << endl;
  return 0;
}