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

/*Priyansh Agarwal*/
#include <algorithm>
#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define nline "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define debug(x) cout << #x << " " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<int, vector<int>, function<bool(int, int)>> pq_func;
/*---------------------------------------------------------------------------------------------------------------------------*/
int gcd(int a, int b) {
  if (b > a) {
    return gcd(b, a);
  }
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
ll expo(ll a, ll b, ll mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}
vector<int> sieve(int n) {
  int *arr = new int[n + 1]();
  vector<int> vect;
  for (int i = 2; i <= n; i++)
    if (arr[i] == 0) {
      vect.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        arr[j] = 1;
    }
  return vect;
}
void extendgcd(ll a, ll b, ll *v) {
  if (b == 0) {
    v[0] = 1;
    v[1] = 0;
    v[2] = a;
    return;
  }
  extendgcd(b, a % b, v);
  ll x = v[1];
  v[1] = v[0] - v[1] * (a / b);
  v[0] = x;
  return;
} // pass an arry of size 3
ll mminv(ll a, ll b) {
  ll arr[3];
  extendgcd(a, b, arr);
  return arr[0];
} // for non prime b
ll mminvprime(ll a, ll b) { return expo(a, b - 2, b); }
bool revsort(ll a, ll b) { return a > b; }
void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}
/*--------------------------------------------------------------------------------------------------------------------------*/
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
  freopen("Error.txt", "w", stderr);
#endif
  ll *dp = new ll[2019]();
  string s;
  cin >> s;
  int p = s[0] - '0';
  dp[p]++;
  ll ans = 0;
  for (int i = 1; i < s.length(); i++) {
    int p = s[i] - '0';
    ll *temp_dp = new ll[2019]();
    temp_dp[p] = 1;
    for (int j = 0; j < 2019; j++) {
      int x = (10 * j + p) % 2019;
      temp_dp[x] += dp[j];
      if (x == 0)
        ans += dp[j];
    }
    for (int j = 0; j < 2019; j++)
      dp[j] = temp_dp[j];
    delete[] temp_dp;
  }
  delete[] dp;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

/*Priyansh Agarwal*/
#include <algorithm>
#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define nline "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define debug(x) cout << #x << " " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<int, vector<int>, function<bool(int, int)>> pq_func;
/*---------------------------------------------------------------------------------------------------------------------------*/
int gcd(int a, int b) {
  if (b > a) {
    return gcd(b, a);
  }
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
ll expo(ll a, ll b, ll mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b = b >> 1;
  }
  return res;
}
vector<int> sieve(int n) {
  int *arr = new int[n + 1]();
  vector<int> vect;
  for (int i = 2; i <= n; i++)
    if (arr[i] == 0) {
      vect.push_back(i);
      for (int j = 2 * i; j <= n; j += i)
        arr[j] = 1;
    }
  return vect;
}
void extendgcd(ll a, ll b, ll *v) {
  if (b == 0) {
    v[0] = 1;
    v[1] = 0;
    v[2] = a;
    return;
  }
  extendgcd(b, a % b, v);
  ll x = v[1];
  v[1] = v[0] - v[1] * (a / b);
  v[0] = x;
  return;
} // pass an arry of size 3
ll mminv(ll a, ll b) {
  ll arr[3];
  extendgcd(a, b, arr);
  return arr[0];
} // for non prime b
ll mminvprime(ll a, ll b) { return expo(a, b - 2, b); }
bool revsort(ll a, ll b) { return a > b; }
void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}
/*--------------------------------------------------------------------------------------------------------------------------*/
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
  freopen("Error.txt", "w", stderr);
#endif
  ll *dp = new ll[2019]();
  string s;
  cin >> s;
  int p = s[0] - '0';
  dp[p]++;
  ll ans = 0;
  for (int i = 1; i < s.length(); i++) {
    int p = s[i] - '0';
    ll *temp_dp = new ll[2019]();
    temp_dp[p] = 1;
    for (int j = 0; j < 2019; j++) {
      int x = (10 * j + p) % 2019;
      temp_dp[x] += dp[j];
      if (x == 0)
        ans += dp[j];
    }
    for (int j = 0; j < 2019; j++)
      dp[j] = temp_dp[j];
  }
  cout << ans << endl;
  return 0;
}