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

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  string S;
  cin >> S;

  ll N = S.size();
  ll MOD = 2019;

  // MODで割った余りの情報に落とす
  vector<ll> rem(N, 0);
  ll base = 1;
  for (int ii = N - 1; ii >= 0; --ii) {
    ll v = (S[ii] - '0') * base;
    rem[ii] = v % MOD;
    base = (base * 10) % MOD;
  }

  // 累積和をとる
  ll ans = 0;
  vector<ll> sum(N + 1, 0);
  map<int, int> m;
  for (int ii = N - 1; ii >= 0; --ii) {
    sum[ii] = (sum[ii + 1] + rem[ii]) % MOD;
    m[sum[ii]]++;
    ans += m[sum[ii]] - 1;
    if (sum[ii] == 0) {
      ans++;
    }
  }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long

using namespace std;

int main() {
  string S;
  cin >> S;

  ll N = S.size();

  ll MOD = 2019;

  ll ans = 0;
  for (int ii = N - 1; ii > 0; --ii) {
    ll val = S[ii] - '0';
    ll rem = val % MOD;
    ll base = 10;
    // cout << "val " << val << "\\n";
    for (int jj = ii - 1; jj >= 0; --jj) {
      ll v = (S[jj] - '0') * base;
      // cout << "v " << v << "\\n";
      rem = (rem + v % MOD) % MOD;
      // cout << rem << "\\n";
      if (rem == 0) {
        ans++;
      }
      base = (base * 10) % MOD;
      // base *= 10;
    }
  }

  cout << ans << "\\n";

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

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

int main() {
  long long ans = 0;
  int k, l;
  vector<int> dp(200000);
  string s;
  cin >> s;
  map<int, int> m;
  k = 0;
  m[k] = 1;
  for (int i = s.size() - 1; i >= 0; i--) {
    l = (int)(s[i] - '0');
    if (i == s.size() - 1)
      dp[i] = 1;
    else
      dp[i] = dp[i + 1] * 10;
    dp[i] %= 2019;
    k = l * dp[i] + k;
    k %= 2019;
    if (m.count(k))
      m[k]++;
    else
      m[k] = 1;
  }
  for (int i = 0; i < 2019; i++) {
    if (m.count(i)) {
      ans += m.at(i) * (m.at(i) - 1) / 2;
    }
  }
  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  long long ans = 0;
  cin >> s;
  vector<int> a(200000);
  for (int i = 0; i < s.size(); i++) {
    a[i] = (int)(s[i] - '0');
  }
  queue<long long> q;
  for (int i = 0; i < s.size(); i++) {
    int k;
    k = q.size();
    for (int j = 0; j < k; j++) {
      int x;
      x = q.front();
      q.pop();
      x = x * 10 + a[i];
      if (x >= 2019) {
        x %= 2019;
        if (x == 0)
          ans++;
      }
      q.push(x);
    }
    if (k == 0)
      q.push(a[i]);
    q.push(0);
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int modpow(int a, int n, int mod) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  const static int mod = 2019;
  int cnt_bin[2019] = {};
  string S;
  cin >> S;
  int size = S.size();
  int tmp = 0;
  for (int i = size - 1; i >= 0; --i) {
    int d = S.at(i) - '0';
    tmp += d * modpow(10, size - 1 - i, mod) % mod;
    tmp %= mod;
    ++cnt_bin[tmp];
  }
  int64_t sum = 0;
  sum += (cnt_bin[0] + 1) * cnt_bin[0] / 2;
  for (int i = 1; i < mod; ++i) {
    sum += (cnt_bin[i] - 1) * cnt_bin[i] / 2;
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

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

int modpow(int a, int n, int mod) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res *= a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  const static int mod = 2019;
  int cnt_bin[2019] = {};
  string S;
  cin >> S;
  int size = S.size();
  int tmp = 0;
  for (int i = size - 1; i >= 0; --i) {
    int d = S.at(i) - '0';
    tmp += d * modpow(10, size - 1 - i, mod) % mod;
    tmp %= mod;
    ++cnt_bin[tmp];
  }
  int64_t sum = 0;
  sum += (cnt_bin[0] + 1) * cnt_bin[0] / 2;
  for (int i = 1; i < mod; ++i) {
    sum += (cnt_bin[i] - 1) * cnt_bin[i] / 2;
  }
  cout << sum << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  string s;
  cin >> s;

  reverse(s.begin(), s.end());
  vector<ll> acc;
  const ll mod = 2019;
  acc.resize(mod, 0);
  acc[0] = 1;

  ll d = 1;
  ll sum_of_digits = 0;
  for (auto i = 0; i < s.size(); i++) {
    auto digit = s.at(i) - '0';
    sum_of_digits += digit * d;
    sum_of_digits %= mod;

    // cout << "sumofdigit: " << sum_of_digits << endl;
    d *= 10;
    d %= mod;
    acc[sum_of_digits]++;
  }

  ll ans = 0;
  for (auto i = 0; i < mod; i++) {
    if (acc[i] > 0) {
      // cout << "i: " << i << "sum: " << acc[i] << endl;
      ans += acc[i] * (acc[i] - 1) / 2;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

bool dividable(string s, int mod = 673) {
  auto cur = 0;
  for (auto i = 0; i < s.size(); i++) {
    auto digit = s.at(i) - '0';
    cur *= 10;
    cur += digit;
    cur %= mod;
  }

  return cur == 0;
}

int main() {
  string s;
  cin >> s;

  vector<ll> acc(s.size(), 0);
  map<string, bool> cache = {};

  for (ll i = s.size() - 1; i >= 0; i--) {
    auto digit = s.at(i) - '0';
    if (i == s.size() - 1) {
      acc[i] = digit;
    } else {
      acc[i] = acc[i + 1] + digit;
    }
  }

  ll ans = 0;
  for (ll j = s.size() - 1; j >= 0; j--) {
    for (ll i = j - 2; i >= 0; i--) {
      ll diff;
      if (j + 1 < s.size()) {
        diff = acc[i] - acc[j + 1];
      } else {
        diff = acc[i];
      }

      if (diff % 3 == 0) {
        string target = s.substr(i, j - i + 1);
        if (cache.count(target) > 0) {
          if (cache[target]) {
            ans++;
          }
        } else {
          auto b = dividable(target, 673);
          cache[target] = b;

          if (b) {
            ans++;
          }
        }
      }
    }
  }
  cout << ans << endl;

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

#include <iostream>
#include <map>
using namespace std;
int atl[2][2019];
//      pos kur beidzas un atl
int main() {
  int ans = 0;
  int p = 1;
  string s;
  cin >> s;
  s = "0" + s;
  for (int i = 1; i < s.size(); i++) {
    for (int j = 0; j < 2019; j++)
      atl[p][j] = 0;
    atl[p][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      int n = (j * 10 + s[i] - '0') % 2019;
      atl[p][n] += atl[!p][j];
    }
    ans += atl[p][0];
    p = !p;
  }
  cout << ans;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
int atl[200005][2019];
//      pos kur beidzas un atl
int main() {
  int ans = 0;
  string s;
  cin >> s;
  s = "0" + s;
  for (int i = 1; i < s.size(); i++) {
    atl[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      int n = (j * 10 + s[i] - '0') % 2019;
      atl[i][n] += atl[i - 1][j];
    }
    ans += atl[i][0];
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;

lint app() {
  string s;
  cin >> s;
  int n = s.length();

  lint out = 0;
  vector<vector<lint>> count(2, vector<lint>(2019, 0));
  rep(i, n) {
    count[1 - i % 2][0] += 1;
    int a = s[i] - '0';
    rep(r, 2019) { count[i % 2][(r * 10 + a) % 2019] = count[1 - i % 2][r]; }
    out += count[i % 2][0];
  }

  return out;
}

int main() {
  cout << app() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;

int app() {
  string s;
  cin >> s;

  lint count = 0;
  int n = s.length();
  rep(i, n) {
    lint r = 0;
    rep_range(j, i, n) {
      r = r * 10 + (s[j] - '0');
      r %= 2019;
      if (r == 0)
        count++;
    }
  }

  return count;
}

int main() {
  cout << app() << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 2e5 + 10;
const int mod = 2019;

int dp[2][2050];
int n, a[maxn];
string s;

int main() {
  cin >> s;
  n = (int)s.size();
  for (int i = 0; i < s.size(); i++)
    a[i + 1] = s[i] - '0';
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int now = (i - 1) % 2, nxt = i % 2;
    dp[nxt][a[i]] = 1;
    for (int d = 0; d < mod; d++) {
      int d2 = (d * 10 + a[i]) % mod;
      dp[nxt][d2] += dp[now][d];
    }
    ans += dp[nxt][0];
    for (int d = 0; d < mod; d++)
      dp[now][d] = 0;
  }
  cout << ans << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 2e5 + 10;
const int mod = 2019;

int dp[maxn][2050];
int n, a[maxn];
string s;

int main() {
  cin >> s;
  n = (int)s.size();
  for (int i = 0; i < s.size(); i++)
    a[i + 1] = s[i] - '0';
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[i][a[i]] = 1;
    for (int d = 0; d < mod; d++) {
      int now = (d * 10 + a[i]) % mod;
      dp[i][now] += dp[i - 1][d];
    }
    ans += dp[i][0];
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int D = 2019;
int dp[2019];

void solve() {
  string S;
  cin >> S;
  int L = S.size();
  reverse(S.begin(), S.end());
  dp[0] = 1;
  int m = 0;
  int ten = 1;
  for (int i = 0; i < L; ++i) {
    m = m + (S[i] - '0') * ten;
    m %= D;
    dp[m]++;
    ten *= 10;
    ten %= D;
  }
  int ans = 0;
  for (int i = 0; i < 2019; ++i) {
    int x = dp[i];
    ans += x * (x - 1) / 2;
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int D = 2019;

void solve() {
  string S;
  cin >> S;
  int L = S.size();
  int ans = 0;
  for (int i = 0; i < L - 3; ++i) {
    string sb = S.substr(i, 4);
    int x = stoi(sb);
    int m = x % D;
    if (m == 0)
      ans++;
    for (int j = i + 4; j < L; ++j) {
      int p = S[j] - '0';
      m = (m * 10 % D + p) % D;
      if (m == 0)
        ans++;
    }
  }
  cout << ans << endl;
}

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

#include <iostream>

using namespace std;
const int maxN = 2019;
int a[maxN], b[maxN];

int main() {
  string s;
  cin >> s;
  for (int i = 0; i < 2019; ++i) {
    a[i] = 0;
    b[i] = 0;
  }
  long long ans = 0;
  for (int j = 0; j < s.size(); j++) {
    int x = s[j] - 48;
    b[x]++;
    for (int i = 0; i < maxN; ++i)
      b[(i * 10 + x) % maxN] += a[i];
    ans += b[0];
    for (int i = 0; i < maxN; ++i) {
      a[i] = b[i];
      b[i] = 0;
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
string s;
int main() {
  cin >> s;
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    long long sum = 0;
    for (int j = i; j < s.size(); j++) {
      sum = sum * 10 + (int)s[j] - 48;
      if (sum % 2019 == 0)
        ans++;
      sum = sum % 2019;
    }
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

ll modpow(ll a, ll n, ll m) {
  if (n == 1)
    return a % m;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1, m)) % m;
  ll b = modpow(a, n / 2, m) % m;
  return (b * b) % m;
}

int main() {

  ll mod = 2019;
  string S;
  cin >> S;
  ll N = S.length();
  ll A[N];
  rep(i, N) {
    if (i == 0)
      A[N - 1] = S[N - 1] - 48;
    else
      A[N - 1 - i] = ((ll(S[N - 1 - i]) - 48) * modpow(10, i, mod)) % mod;
  }
  ll B[N];
  rep(i, N) {
    if (i == 0)
      B[N - 1] = ll(S[N - 1]) - 48;
    else
      B[N - 1 - i] = (B[N - i] + A[N - 1 - i]) % mod;
  }
  sort(B, B + N);
  ll out = 0;
  ll cnt = 0;
  rep(i, N - 1) {
    if (B[i] == 0)
      out++;
    if (B[i] == B[i + 1])
      cnt++;
    else if (cnt > 0) {
      out += cnt * (cnt + 1) / 2;
      cnt = 0;
    }
    if (i == N - 2 && cnt > 0)
      out += cnt * (cnt + 1) / 2;
  }
  cout << out << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <queue>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {

  string S;
  cin >> S;
  ll len = S.length();
  ll out = 0;
  rep(i, len) {
    for (ll j = 4; j <= len - i; j++) {
      ll N = ll(stol(S.substr(i, j)));
      if (N % 2019 == 0)
        out++;
    }
  }
  cout << out << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;

int main() {
  string s;
  cin >> s;
  const int m = 2019;
  int n = s.size();
  reverse(s.begin(), s.end()); // 反転させて文字列の先頭を数値を1の位にする
  int x = 1, tot = 0; // totに累積和を求める
  vector<int> cnt(m);
  ll ans = 0;
  rep(i, n) {
    cnt[tot]++;
    /*1の位から10^nの位が2019の倍数であれば余りは0であるから
    次0が来た時の分を予めカウントしておく
    また、前のループででた余りも記録しておく(2019で割った余りよりcntの大きさは2019）*/
    tot += (s[i] - '0') * x;
    tot %= m; // オーバーフロー回避のため2018以内でおさえる
    ans += cnt[tot]; // 複数回目に出てきた余りであれば(出た回数-1)分追加
    x = x * 10 % m; // 次のループで桁上げ、こっちも桁数を4桁でおさえる
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;

int def(int x, string s, int cnt) {
  string T;
  for (int i = 4; i <= x; ++i) {
    T = s.substr(0, i);
    // cout << T << endl;
    if (atoi(T.c_str()) % 2019 == 0) {
      cnt++;
    }
  }
  s.erase(0, 1);
  // cout << s << endl;
  if (s.size() > 0)
    return def(s.size(), s, cnt);
  return cnt;
}

int main() {
  string S;
  cin >> S;
  int cnt = def(S.size(), S, 0);
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

typedef long long ll;
const ll N = 2e5 + 10;
int f[N][2019];
ll a[2019], b[2019];
string s;
ll n;

int main() {
  cin >> s;
  n = s.size();

  ll t = s[0] - '0';
  ll x;
  // f[0][t % 2019] = 1;
  a[t] = 1;

  ll res = 0;
  for (ll i = 1; i < n; i++) {
    for (int j = 0; j < 2019; j++)
      b[j] = a[j];
    memset(a, 0, sizeof a);

    t = s[i] - '0';
    // cout << t << endl;
    // f[i][t % 2019]++;
    a[t % 2019]++;

    /*
    if (f[i - 1][0]) {
            f[i][t] += f[i - 1][0];
    }
    */
    if (b[0]) {
      a[t] += b[0];
    }
    for (ll j = 1; j < 2019; j++) {
      x = j * 10 + t;
      // f[i][x % 2019] =f[i - 1][j];
      a[x % 2019] = b[j];
    }

    res += a[0];
  }
  /*
  for (int i = 0; i < n; i++) {
          for (int j = 0; j < 2019; j++) {
                  if (f[i][j] != 0)
                          cout << i << " " << j << " " << f[i][j] << endl;
          }
  }

  ll res = 0;
  for (ll i = 0; i < n; i++) {
          res += f[i][0];
  }
  */
  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

typedef long long ll;
const ll N = 2e5 + 10;
int f[N][2019];
string s;
ll n;

int main() {
  cin >> s;
  n = s.size();

  ll t = s[0] - '0';
  ll x;
  f[0][t % 2019] = 1;

  for (ll i = 1; i < n; i++) {
    t = s[i] - '0';
    // cout << t << endl;
    f[i][t % 2019]++;

    if (f[i - 1][0]) {
      f[i][t] += f[i - 1][0];
    }
    for (ll j = 1; j < 2019; j++) {
      x = j * 10 + t;
      f[i][x % 2019] = f[i - 1][j];
    }
  }
  /*
  for (int i = 0; i < n; i++) {
          for (int j = 0; j < 2019; j++) {
                  if (f[i][j] != 0)
                          cout << i << " " << j << " " << f[i][j] << endl;
          }
  }
  */
  ll res = 0;
  for (ll i = 0; i < n; i++) {
    res += f[i][0];
  }

  cout << res << endl;

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

#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

int main() {
  string s;
  cin >> s;

  for (auto &&c : s) {
    c -= '0';
  }

  vector<int64_t> t(2019);
  t[0] = 1;
  int64_t m = 0, d = 1;
  for (auto i : irange(0uL, s.size()) | reversed) {
    m += d * s[i];
    m %= 2019;
    d *= 10;
    d %= 2019;
    ++t[m];
  }

  int64_t ans = 0;
  for (auto i : irange(0L, 2019L)) {
    ans += t[i] * (t[i] - 1) / 2;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

int main() {
  string s;
  cin >> s;

  for (auto &&c : s) {
    c -= '0';
  }

  int64_t ans = 0;
  vector<vector<int64_t>> dp(s.size() + 1, vector<int64_t>(2019, 0L));
  for (auto i : irange(0uL, s.size())) {
    dp[i + 1][s[i] % 2019] += 1;
    for (auto j : irange(0L, 2019L)) {
      dp[i + 1][(j * 10 + s[i]) % 2019] += dp[i][j];
    }

    ans += dp[i + 1][0];
  }

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

#include <iostream>
// #include <set>
// #include <map>
// #include <iomanip>
// #include <algorithm>
// #include <numeric>
// #include <queue>
// #include <stack>
// #include <math.h>
#include <string>
#include <vector>
// #include <list>
// #include <deque>
// #include <unordered_map>
typedef long long LL;
// typedef long double LD;
using namespace std;

// #define MOD 1000000007
// #define MAX 200100
// #define NIL -1

LL mod_pow(LL x, LL n) {
  LL ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x % 2019;
    }
    x = x * x % 2019;
    n >>= 1;
  }
  return ans;
}

int main() {
  string s;
  cin >> s;
  int ss = s.size();
  vector<LL> dp(ss);
  vector<int> res(2019, 0);
  LL tmp = s[ss - 1] - '0';
  dp[ss - 1] = tmp;
  res[dp[ss - 1]]++;
  for (int i = ss - 2; i >= 0; i--) {
    tmp = s[i] - '0';
    tmp = tmp * mod_pow(10, ss - 1 - i);
    dp[i] = (dp[i + 1] + tmp) % 2019;
    // cout << tmp << " " << dp[i-1] << " " << dp[i] << endl;
    res[dp[i]]++;
  }

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

  // for(int i=0; i<2019; i++){
  //     if(res[i]>=1){
  //         cout << i << " " << res[i] << endl;
  //     }
  // }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
// #include <set>
// #include <map>
// #include <iomanip>
#include <algorithm>
// #include <numeric>
// #include <queue>
// #include <stack>
// #include <math.h>
#include <string>
#include <vector>
// #include <list>
// #include <deque>
// #include <unordered_map>
typedef long long LL;
// typedef long double LD;
using namespace std;

// #define MOD 1000000007
// #define MAX 200100
// #define NIL -1

int main() {
  string s;
  LL ans = 0;
  LL cand;
  LL tmp;
  cin >> s;
  cand = 0;
  for (LL i = 0; i < s.size() - 3; i++) {
    cand = 0;
    tmp = s[i] - '0';
    cand += tmp;
    cand *= 10;
    tmp = s[i + 1] - '0';
    cand += tmp;
    cand *= 10;
    tmp = s[i + 2] - '0';
    cand += tmp;
    for (int j = i + 3; j < s.size(); j++) {
      cand *= 10;
      tmp = s[j] - '0';
      cand += tmp;
      cand = cand % 2019;
      if (cand == 0) {
        ans++;
      }
    }
  }

  // test
  // for(int i=0; i<start.size(); i++){
  //    cout << start[i] << " " << end[i] << endl;
  //}

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  string s;
  cin >> s;
  v1i v(s.size() + 1, 0), u(2016, 1);
  rep(i, 2015) {
    u[i + 1] = u[i] * 10;
    u[i + 1] %= 2019;
  }
  rrep(i, s.size()) {
    v[i] = v[i + 1] + u[((int)s.size() - i - 1) % 2016] * (s[i] - '0');
    v[i] %= 2019;
  }
  map<int, lint> m;
  rep(i, s.size() + 1) m[v[i]]++;
  lint a = 0;
  rep(i, 2019) a += m[i] * (m[i] - 1) / 2;
  if (s.size() < 5)
    a = 0;
  cout << a << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  string s;
  cin >> s;
  int a;
  v2i v(s.size() - 3, v1i(s.size() + 1, -1));
  rep(i, s.size() - 3) v[i][i] = 0;
  rep(j, s.size() - 3) {
    rep(i, s.size() - j) {
      v[j][i + j + 1] = v[j][i + j] * 10 + s[i + j] - '0';
      v[j][i + j + 1] %= 2019;
      if (v[j][i + j + 1] == 0)
        ++a;
    }
  }
  cout << a << "\\n";
  return 0;
}