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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << ' ';
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
using namespace std;

/* Mathematical Functions */
int power(int x, int y);
bool isPrime(int n);
int modInv(int a, int b);
int gcdExtended(int a, int b, int *x, int *y);
int mpower(int a, int b, int p);

const int inf = (int)1e17;
const int N = (int)3e5 + 5;
vector<int> P(N, 0), dp(N, 0);
vector<int> L;
int32_t main() {

  FASTIO;
  string str;
  cin >> str;
  REP(i, 0, str.size()) { L.pb(str[i] - '0'); }
  int n = L.size(), M = 2019, ans = 0;
  RER(i, n - 1, 0) {
    P[i] = P[i + 1] + mpower(10, n - i - 1, M) * L[i];
    P[i] %= M;
    dp[P[i]]++;
  }
  dp[0]++;
  REP(i, 0, 2019) { ans += dp[i] * (dp[i] - 1) / 2; }
  cout << ans << endl;
  return 0;
}
int modInv(int a, int m) {
  int x, y;
  int g = gcdExtended(a, m, &x, &y);
  int res = (x % m + m) % m;
  return res;
}

int gcdExtended(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}

int mpower(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int power(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  int p = sqrt(n);
  for (int i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << ' ';
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
using namespace std;

/* Mathematical Functions */
int power(int x, int y);
bool isPrime(int n);
int modInv(int a, int b);
int gcdExtended(int a, int b, int *x, int *y);
int mpower(int a, int b, int p);

const int inf = (int)1e17;
const int N = (int)3e5 + 5;
set<int> P[N];
vector<int> L;
int32_t main() {

  FASTIO;
  string str;
  cin >> str;
  REP(i, 0, str.size()) { L.pb(str[i] - '0'); }
  int n = L.size(), M = 2019, ans = 0;
  REP(i, 0, n) {
    int rem = 0, pref = 0;
    REP(j, i, n) {
      rem = rem * 10 + L[j];
      rem %= M;
      if (P[j].find(rem) != P[j].end()) {
        break;
      }
      P[j].insert(rem);
      if (rem == 0)
        pref++;
    }
    ans += pref * (pref + 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
int modInv(int a, int m) {
  int x, y;
  int g = gcdExtended(a, m, &x, &y);
  int res = (x % m + m) % m;
  return res;
}

int gcdExtended(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}

int mpower(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int power(int x, int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  int p = sqrt(n);
  for (int i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}