alexjercan/bugnet huggingface dataset:
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb push_back;
#define sp " ";
void vout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  string num;
  int ans = 0;
  cin >> num;
  reverse(num.begin(), num.end());
  vi sum(2020, 0);
  lli t = 1, tot = 0;
  rep(i, num.size()) {
    sum.at(tot)++;
    int y = num.at(i) - '0';
    t %= 2019;
    y *= t;
    tot += y % 2019;
    tot %= 2019;
    ans += sum.at(tot);
    t *= 10;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define YN(x) cout << (bool x ? "Yes" : "No") << endl;
#define out(s) cout << s << endl;
#define pb push_back;
#define sp " ";
void vout(vector<int> v) {
  for (int i = 0; i < v.size(); i++)
    cout << v.at(i) << endl;
}
int main() {
  string num;
  int ans = 0;
  cin >> num;
  reverse(num.begin(), num.end());
  vi sum;
  lli t = 1;
  rep(i, num.size()) {
    int y = num.at(i) - '0';
    t %= 2019;
    y *= t;
    if (i == 0) {
      sum.push_back(0);
    }
    sum.push_back((y + sum.at(sum.size() - 1)) % 2019);
    t *= 10;
  }
  for (int i = 1; i <= sum.size(); i++) {
    for (int j = i + 1; j < sum.size(); j++) {
      if (sum.at(i - 1) % 2019 == sum.at(j) % 2019) {
        ans++;
      }
    }
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  int n = s.length();
  int dp[2][2019];
  memset(dp, 0, sizeof(dp));
  dp[1][s[n - 1] - '0'] = 1;
  int curr = 10, ans = dp[1][0];
  for (int i = n - 2; i >= 0; i--) {
    int z = ((s[i] - '0') * curr) % 2019;
    for (int j = 0; j < 2019; j++) {
      if (dp[1][j]) {
        int y = (z + j) % 2019;
        dp[0][y] += dp[1][j];
      }
    }
    dp[0][z] += 1;
    curr = (curr * 10) % 2019;
    ans += dp[0][0];
    for (int i = 0; i < 2019; i++) {
      dp[1][i] = dp[0][i];
      dp[0][i] = 0;
    }
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  cin >> s;
  int n = s.length();
  int dp[n][2019];
  memset(dp, false, sizeof(dp));
  dp[n - 1][s[n - 1] - '0'] = 1;
  int curr = 10;
  for (int i = n - 2; i >= 0; i--) {
    int z = ((s[i] - '0') * curr) % 2019;
    for (int j = 0; j < 2019; j++) {
      if (dp[i + 1][j]) {
        int y = (z + j) % 2019;
        dp[i][y] += dp[i + 1][j];
      }
    }
    dp[i][z] += 1;
    curr = (curr * 10) % 2019;
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    ans += dp[i][0];
  cout << ans << "\\n";
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

#define MAXN 200010

string s;
int n;

int main() {
  cin >> s;
  n = sz(s);

  int res = 0;
  vi PD(2019, 0);

  for (int i = n - 1; i >= 0; i--) {
    vi PDnovo(2019);
    for (int r = 0; r < 2019; r++) {
      int ans = 0;
      int dig = s[i] - '0';
      int oldr = r;
      int nr = r;
      nr *= 10;
      nr %= 2019;
      nr += dig;
      nr %= 2019;
      if (nr == 0) {
        ans++;
      }
      ans += PD[nr];
      PDnovo[oldr] = ans;
    }
    res += PDnovo[0];
    PD = PDnovo;
  }

  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define EPS 10e-9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define st first
#define nd second
#define sz(v) int(v.size())
#define all(X) (X).begin(), (X).end()
#define FOR(I, A, B) for (int I = A; I < B; I++)
#define RFOR(I, A, B) for (int I = A; I >= B; I--)

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<iii> viii;
typedef vector<ll> vll;

#define MAXN 200010

string s;
int n;
int pot10[MAXN];
map<ii, int> has;

int pd(int i, int r) {
  if (i == n)
    return 0;

  if (has.find(mp(i, r)) != has.end()) {
    return has[mp(i, r)];
  }

  int ans = 0;
  // printf("estado: %d %d\\n", i, r);

  int dig = s[i] - '0';
  int oldr = r;
  r *= 10;
  r %= 2019;
  r += dig;
  r %= 2019;
  // printf("novo r %d\\n", r);
  if (r == 0) {
    ans++;
  }
  ans += pd(i + 1, r);
  has[mp(i, oldr)] = ans;
  return ans;
}

int main() {
  cin >> s;
  n = sz(s);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += pd(i, 0);
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;

vector<int> remain(2020, 0);

int main() {
  string s;
  cin >> s;
  int ans = 0, n = s.size();
  ll num = 0, x = 1;
  remain[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    num += (s.at(i) - '0') * x;
    num = num % 2019;
    ans += remain[num];
    remain[num]++;
    x = x * 10 % 2019;
  }
  cout << ans << endl;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;

vector<int> remain(2020, 0);

int main() {
  string s;
  cin >> s;
  int ans = 0, n = s.size();
  vector<ll> dp(n);

  dp[n] = 0;
  remain[0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = dp[i + 1] + (s.at(i) - '0') * pow(10, n - i - 1);
    // cout<<dp[i]<<endl;
    ans += remain[dp[i] % 2019];
    remain[dp[i] % 2019]++;
    // cout<<i<<" "<<dp[i]%2019<<" "<<remain[dp[i]%2019]<<endl;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS const int M = 2019;
  string S;
  cin >> S;
  vector<int> dp(M);

  ll ans = 0;
  REP(i, S.size()) {
    vector<int> tmp(M);
    swap(tmp, dp);
    int c = S[i] - '0';
    REP(j, M) {
      int k = (j * 10 + c) % M;
      dp[k] = dp[k] + tmp[j];
    }
    dp[c] += 1;
    ans += dp[0];
    // cerr << ans << " ";
    // cerr << accumulate(ALL(dp[i+1]), 0);
  }

  cout << ans << '\\n';

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << '\\n';
#define CFYN(n) cout << ((n) ? "YES" : "NO") << '\\n';
#define OUT(n) cout << (n) << '\\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main(void) {
  IOS const int M = 2019;
  string S;
  cin >> S;
  vector<vector<int>> dp(S.size() + 1, vector<int>(M));

  ll ans = 0;
  REP(i, S.size()) {
    int c = S[i] - '0';
    REP(j, M) {
      int k = (j * 10 + c) % M;
      dp[i + 1][k] = dp[i + 1][k] + dp[i][j];
    }
    dp[i + 1][c] += 1;
    ans += dp[i + 1][0];
    // cerr << ans << " ";
    // cerr << accumulate(ALL(dp[i+1]), 0);
  }

  cout << ans << '\\n';

  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// Write From this Line

const int mod = 1e9 + 7;
int main() {
  string s;
  cin >> s;

  int n = s.size();
  vector<int> d(n + 1);
  d[0] = 0;

  int ten = 1;
  rep(i, n) {
    int now = s[n - 1 - i] - '0';
    d[i + 1] = (d[i] + now * ten) % 2019;
    ten *= 10;
    ten %= 2019;
  }

  vector<int> cnt(2019, 0);
  ll ans = 0;
  rep(i, n + 1) {
    int now = d[i];
    ans += cnt[now];
    cnt[now]++;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)

// Write From This Line
int main() {
  string s;
  cin >> s;
  // とりあえず全探索してみる
  int n = s.size();
  int ans = 0;
  rep(i, n) {
    for (int j = 1; j + i <= n; j++) {
      string t = s.substr(i, j);
      if (stoll(t) % 2019 == 0) {
        // cout << stoll(t) << endl;
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string S;
  cin >> S;
  int res = 0;
  int ans = 0;
  int tmp = 1;
  int chk[2019] = {};
  chk[0] = 1;

  for (int i = S.size() - 1; i >= 0; i--) {
    res = (res + (S[i] - '0') * tmp) % 2019;
    tmp = (10 * tmp) % 2019;
    ans += chk[res];
    chk[res]++;
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int n = S.length();

  int res = 0;
  for (int i = 0; i < n; i++) {
    int jcnt = 0;
    int tmp = 0;
    int chk = 0;
    for (int j = i; j < n; j++) {
      if (jcnt >= 6)
        continue;
      tmp = atoi(S.substr(i, j).c_str());
      if (tmp != 0 && tmp % 2019 == 0 && chk != tmp) {
        chk = tmp;
        res++;
      }
      jcnt++;
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7, mod = 2019;
int T[N], cnt[N];

int main() {
#ifdef LOCAL
  freopen("input", "r", stdin);
#endif

  string s;

  cin >> s;

  int n = s.size();

  T[n] = 0;
  cnt[0]++;

  int pot = 1;

  for (int i = n - 1; i >= 0; i--) {
    T[i] = (T[i + 1] % mod + ((s[i] - '0') * pot % mod) % mod) % mod;
    pot = (pot % mod * 10) % mod;
    cnt[T[i]]++;
  }

  long long int ans = 0;

  for (int i = 0; i < N; i++) {
    ans += cnt[i] * (cnt[i] - 1) / 2;
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7, mod = 2019;
int T[N], cnt[N];

int main() {
#ifdef LOCAL
  freopen("input", "r", stdin);
#endif

  string s;

  cin >> s;

  int n = s.size();

  T[n] = 0;
  cnt[0]++;

  int pot = 1;

  for (int i = n - 1; i >= 0; i--) {
    int ai = ((s[i] - '0') * pot % mod) % mod;
    T[i] = (T[i + 1] % mod + (ai)) % mod;
    pot *= 10;
    cnt[T[i]]++;
  }

  int ans = 0;

  for (int i = 0; i < N; i++) {
    ans += cnt[i] * (cnt[i] - 1) / 2;
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))

int main() {
  std::string S;
  cin >> S;
  int modS[S.length() + 1];
  int mod10[200000];
  long long Mnum = 0;
  modS[S.length()] = 0;
  mod10[0] = 1;
  for (int i = 1; i < 200000; i++) {
    mod10[i] = (mod10[i - 1] * 10) % 2019;
  }
  for (int i = S.length() - 1; i >= 0; i--) {
    long long num = atoi(S.substr(i, 1).c_str());
    Mnum += num * mod10[S.length() - 1 - i];
    modS[i] = Mnum % 2019;
  }
  std::sort(modS, modS + SIZE_OF_ARRAY(modS), std::greater<>());
  int count = 1;
  int ans = 0;
  for (int i = 1; i < S.length() + 1; i++) {
    // cout << modS[i];
    if (modS[i] == modS[i - 1]) {
      count++;
    } else {

      ans += count * (count - 1) / 2;
      count = 1;
    }
  }
  ans += count * (count - 1) / 2;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  std::string S;
  cin >> S;
  int count = 0;
  for (int l = 0; l < S.length(); l++) {
    for (int k = 2; k < S.length() - l + 1; k++) {
      std::string substr = S.substr(l, k);
      int num = atoi(substr.c_str());
      if (num % 2019) {
      } else {
        count++;
      }
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int ctoi(char c) { return (int)(c - '0'); }

int main() {
  string s;
  cin >> s;
  int N = (int)(s.length());
  // cout << N << endl;
  int R[N];
  int count[2019];
  for (int i = 0; i < 2019; i++) {
    count[i] = 0;
  }
  count[0] = 1;
  long long int rest = 0;
  long long int k = 1;
  for (int i = 0; i < N; i++) {
    int t = ctoi(s[N - i - 1]);
    int nr = ((t * k) % 2019 + rest) % 2019;
    count[nr] += 1;
    k = (k * 10) % 2019;
    rest = nr;
  }
  long long int sum = 0;
  for (int i = 0; i < 2019; i++) {
    int m = count[i];
    sum += (m * (m - 1)) / 2;
  }

  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int ctoi(char c) { return (int)(c - '0'); }

int main() {
  string s;
  cin >> s;
  int N = (int)(s.length());
  // cout << N << endl;
  int A[200000];
  int B[200000];

  // cout << "1 ok" << endl;
  long long int sum = 0;
  for (int x = 0; x < N; x++) {
    for (int y = x; y >= 0; y--) {
      if (x == y) {
        A[x] = ctoi(s[x]);
        B[x] = A[x];
      } else {
        A[y] = (B[y] * 10 + B[x]) % 2019;
        B[y] = A[y];
        if (x - y >= 3 && A[y] == 0) {
          sum += 1;
        }
      }
    }
  }

  cout << sum << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

long long N;
string S;
int P;

long long solve() {
  vector<long long> val(P, 0);
  long long tenfactor = 1;
  long long cur = 0;
  val[cur]++;
  for (int i = 0; i < N; ++i) {
    cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> S;
  N = S.length();
  P = 2019;
  cout << solve() << endl;
}

Error: -11

Fixed submission:

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

long long N;
string S;
int P;

long long solve() {
  vector<long long> val(P, 0);
  long long tenfactor = 1;
  long long cur = 0;
  val[cur]++;
  for (int i = 0; i < N; ++i) {
    cur = (cur + (S[N - i - 1] - '0') * tenfactor) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> N >> S;
  P = 2019;
  cout << solve() << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#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
using namespace std;
using ll = long long;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> dp(2020, 0);
  dp[0] = 1;
  int now = 0;
  int k = 1;
  for (int i = N - 1; i >= 0; i--) {
    now = (now + k * (S[i] - '0')) % 2019;
    dp[now]++;
    k = k * 10 % 2019;
  }
  ll ans = 0;
  for (int i = 0; i <= 2018; i++) {
    ans += dp[i] * (dp[i] - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#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
using namespace std;
using ll = long long;

int main() {
  string S;
  cin >> S;
  int N = S.size();
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int now = 0;
    for (int j = i; j < N; j++) {
      now = (now * 10 + S[j] - '0') % 2019;
      if (now == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 2e5 + 9;
short dp[N][2019];
ll ls;
string s;
int main() {
  FASTINOUT;
  cin >> s;
  ls = s.length();
  ll ans = 0;
  dp[0][s[0] - '0']++;
  for (int i = 1; i < ls; i++) {
    ll kl = s[i] - '0';
    for (int j = 0; j < 2019; j++) {
      dp[i][(j * 10 + kl) % 2019] += dp[i - 1][j];
    }
    dp[i][kl]++;
    ans += dp[i][0];
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define ll long long
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 2e5 + 9;
short dp[N][2019];
ll ls;
string s;
ll f(ll i, ll m) {
  if (i == ls)
    return 0;
  if (dp[i][m] != 0)
    return dp[i][m];
  ll x = s[i] - '0';
  ll ans = ((m * 10 + x) % 2019 == 0) + f(i + 1, (m * 10 + x) % 2019);
  return dp[i][m] = ans;
}
int main() {
  FASTINOUT;
  cin >> s;
  ls = s.length();
  ll ans = 0;
  for (ll i = 0; i < ls; i++) {
    if (s[i] == '0')
      ans++;
    else
      ans += f(i, 0);
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const double PI = cos(-1.0);
const double eps = 1e-12;
#define For(i, n, m) for (int i = n; i <= m; ++i)
#define clr(a, b) memset(a, b, sizeof(a))

string s;
ll cnt[2019];
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> s;
  cnt[0] = 1;
  ll p(1), now(0), ans(0);
  for (int i = s.size() - 1; i >= 0; --i) {
    now = (now + (s[i] - '0') * p) % 2019;
    ans += cnt[now], ++cnt[now];
    p = ((p << 3) + (p << 1)) % 2019;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const double PI = cos(-1.0);
const double eps = 1e-12;
#define For(i, n, m) for (int i = n; i <= m; ++i)

string s;
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> s;
  ll cnt(0);
  for (int i = 0; s[i] != '\\0'; ++i) {
    ll t(0), res(0);
    while (s[i] == '0')
      ++i;
    if (s[i] == '\\0')
      break;
    int j = i;
    for (j = i; s[j] != '\\0' && j <= i + 3; ++j)
      t = (t << 3) + (t << 1) + s[j] - '0';
    if (t % 2019 == 0)
      ++cnt, t = 0;
    else
      t %= 2019;
    for (j; s[j] != '\\0'; ++j) {
      t = (t << 3) + (t << 1) + s[j] - '0';
      if (t % 2019 == 0)
        ++cnt, t = 0;
      else
        t %= 2019;
    }
  }
  cout << cnt << "\\n";
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
int main() {
  std::string s;
  std::cin >> s;
  int count = 0;
  const int n = s.size();
  int a[n],
      b[n]; // a存从第i位到最后一位这个数对2019的余数，b存10的n-i-1次方对2019的余数
  int c[2019] = {0};         // 不同的余数出现的次数
  a[n - 1] = s[n - 1] - '0'; // a【n-1】是s【n-1】转数字
  b[n - 1] = 1;              // b【n-1】是10^0就是1
  int temp = 1;              // 计算b中数值
  for (int i = 1; i < n; ++i) {
    temp = (10 * temp) % 2019;
    b[n - i - 1] = temp;
  }
  // 计算a
  for (int i = n - 2; i >= 0; --i) {
    a[i] = ((s[i] - '0') * b[i] + a[i + 1]) % 2019;
  }
  // 从a到c
  for (int i = 0; i < n; ++i) {
    c[a[i]] += 1;
  }
  count = c[0];
  for (int i = 0; i < 2019; ++i) {
    count += ((c[i] - 1) * c[i] / 2);
  }
  std::cout << count << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
int main() {
  std::string s;
  std::cin >> s;
  int count = 0;
  const int n = s.size();
  int a[n];
  int b[2019] = {0};
  a[0] = s[0] - '0';
  for (int i = 1; i < n; ++i) {
    a[i] = (a[i - 1] * 10 + s[i] - '0') % 2019;
  }
  for (int i = 0; i < n; ++i) {
    if (a[i] == 0)
      ++count;
    for (int j = 1; j < n - i; ++j) {
      a[i] = (10 * a[i]) % 2019;
      if (a[i] == a[i + j]) {
        ++count;
      }
    }
  }
  std::cout << count << std::endl;
  return 0;
}