alexjercan/bugnet huggingface dataset:
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> ary(n);
  for (int i = 0; i < n; i++)
    ary[i] = s[i] - '0';

  vector<int> ten(n + 1);
  ten[0] = 1;
  for (int i = 1; i <= n; i++)
    ten[i] = ten[i - 1] * 10 % 2019;

  ll res = 0;
  map<int, int> mp;
  mp[0]++;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    sum = (sum + ary[i] * ten[n - i]) % 2019;
    res += mp[sum];
    mp[sum]++;
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

using ll = long long;
const int SIZE = 1e5;

int ary[SIZE];

const int MOD = 2019;
int main() {
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    ary[i] = s[i] - '0';
  }
  ll res = 0;
  int n = s.size();
  for (int i = 0; i < SIZE; i++) {
    int sum = 0;
    for (int j = i; j < SIZE; j++) {
      sum *= 10;
      sum += ary[j];
      sum %= MOD;
      res += ll(sum == 0);
    }
  }
  res -= ll(SIZE - n) * (SIZE - n + 1) / 2;
  int tmp = 1;
  int sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum += ary[i] * tmp;
    tmp *= 10;
    tmp %= MOD;
    sum %= MOD;
    if (sum == 0)
      res -= SIZE - n;
  }
  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 9;
int main() {
  char s[maxn];
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll d[maxn] = {0}, b[maxn] = {0};
  ll ans = 0;
  for (int i = 1; i <= len; i++) {
    int num = s[i] - '0';
    d[num]++;
    for (int j = 0; j < 2019; j++) {
      d[(j * 10 + num) % 2019] += b[j];
    }
    for (int j = 0; j < 2019; j++) {
      b[j] = d[j];
      d[j] = 0;
    }
    ans += b[0];
  }
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e4 + 9;
int main() {
  char s[maxn];
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  ll d[maxn] = {0}, b[maxn] = {0};
  ll ans = 0;
  for (int i = 1; i <= len; i++) {
    int num = s[i] - '0';
    d[num]++;
    for (int j = 0; j < 2019; j++) {
      d[(j * 10 + num) % 2019] += b[j];
    }
    for (int j = 0; j < 2019; j++) {
      b[j] = d[j];
      d[j] = 0;
    }
    ans += b[0];
  }
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;
string S;
int main() {
  cin >> S;
  ll res = 0;
  int Sl = S.length();
  map<ll, ll> V;
  for (int i = 0; i < 2019; i++)
    V.insert(make_pair(i, 0));
  ll MOD = 1;
  ll Ti;
  for (int i = Sl; i >= 0; i--) {
    if (i == Sl) {
      Ti = 0;
      V[Ti]++;
    } else {
      MOD *= Sl - 1 - i > 0 ? 10 : 1;
      MOD %= 2019;
      Ti = MOD * (S[i] - '0') + Ti;
      Ti %= 2019;
      V[Ti]++;
    }
  }
  for (int i = 0; i < 2019; i++) {
    res += V[i] * (V[i] - 1) / 2;
  }

  cout << res << endl;
}

Error: 0

Fixed submission:

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

typedef long long ll;
string S;

int main() {
  cin >> S;
  ll res = 0;
  for (int i = 0; i < S.length() - 3; i++) {
    ll s = (S[i] - '0') * 100;
    s += (S[i + 1] - '0') * 10;
    s += (S[i + 2] - '0');
    for (int j = i + 3; j < S.length(); j++) {
      s *= 10;
      s += (S[j] - '0');
      s %= 2019;
      if (s == 0)
        res++;
    }
  }

  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

const static int YEAR = 2019;

int main(void) {
  std::string s;
  std::cin >> s;
  std::reverse(s.begin(), s.end());

  std::vector<int> cnt(3000);
  int tot = 0;
  int keta = 1;
  int ans = 0;

  for (int i = 0; i < s.size(); i++) {
    cnt[tot]++;
    tot += (s[i] - '0') * keta;
    tot = tot % YEAR;
    // std::cout << tot << std::endl;
    ans += cnt[tot];
    keta = keta * 10 % YEAR;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

const static int NY = 2019;

int main(void) {
  std::string s;
  std::cin >> s;
  std::vector<int> a;
  std::vector<std::vector<int>> b;
  for (int i = 0; i < s.size(); i++) {
    int tmp = s[i] - '0';
    a.push_back(tmp);
  }
  std::reverse(a.begin(), a.end());
  int km = a.size();
  b.resize(km);
  for (int i = 0; i < km; i++) {
    b[i].resize(km);
    for (int j = 0; j < km; j++) {
      b[i][j] = -1;
    }
  }

  for (int i = 0; i < km; i++) {
    for (int j = i; j < km; j++) {
      if (i == j) {
        b[i][j] = a[i] % NY;
      } else {
        int tmp = a[j];
        for (int x = 0; x < j - i; x++) {
          tmp = (tmp * 10) % NY;
        }
        tmp = (tmp + b[i][j - 1]) % NY;
        b[i][j] = tmp;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < km; i++) {
    for (int j = 0; j < km; j++) {
      if (b[i][j] == 0)
        ans++;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define prec(y, x) fixed << setprecision(y) << x
#define inf 1e18
#define pi 3.1415926535
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  ll arr[3][2021];
  for (ll i = 0; i <= 2; i++) {
    for (ll j = 0; j <= 2020; j++)
      arr[i][j] = 0;
  }
  int u = s[0] - '0';
  ll ans = 0;
  arr[0][u] = 1;
  for (int i = 1; i < n; i++) {
    int u = s[i] - '0';
    arr[1][u] = 1;
    for (int j = 0; j < 2019; j++) {
      int y = 10 * j + u;
      y %= 2019;
      arr[1][y] += arr[0][j];
    }
    ans += arr[1][0];
    for (ll j = 0; j <= 2020; j++) {
      arr[0][j] = arr[1][j];
      arr[1][j] = 0;
    }
  }

  cout << ans;
  return 0;
}
// make all values ll


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define endl "\\n"
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define prec(y, x) fixed << setprecision(y) << x
#define inf 1e18
#define pi 3.1415926535
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  cin >> s;
  int n = s.size();
  ll arr[n + 2][2025];
  for (ll i = 0; i <= n + 1; i++) {
    for (ll j = 0; j <= 2022; j++)
      arr[i][j] = 0;
  }
  int u = s[0] - '0';
  arr[0][u] = 1;
  for (int i = 1; i < n; i++) {
    int u = s[i] - '0';
    arr[i][u] = 1;
    for (int j = 0; j < 2019; j++) {
      int y = 10 * j + u;
      y %= 2019;
      arr[i][y] += arr[i - 1][j];
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += arr[i][0];
  cout << ans;
  return 0;
}
// make all values ll
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 56;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int main() {
  string s;
  cin >> s;
  ll n = s.size(), x = 2019;
  ll a[n + 1];
  fill(a, a + n + 1, 0);
  ll tmp = 0;
  ll mod = 1;
  for (ll i = 0; i < n; i++) {
    tmp += (ll(s[n - 1 - i] - '0')) * mod;
    a[i + 1] += tmp % x;
    mod *= 10;
    mod %= x;
  }
  ll ans = 0, c[2019];
  fill(c, c + 2019, 0);
  for (ll i = 0; i < n + 1; i++) {
    c[a[i]]++;
  }
  for (ll i = 0; i < 2019; i++) {
    ans += ((c[i] * (c[i] - 1)) / 2);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 56;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  ll a[n + 1];
  fill(a, a + n + 1, 0);
  ll tmp = 0;
  ll mod = 1;
  for (ll i = 0; i < n; i++) {
    tmp += (ll(s[n - 1 - i] - '0')) * mod;
    a[i + 1] += tmp % MOD;
    mod *= 10;
    mod %= MOD;
  }
  ll ans = 0, c[2019];
  fill(c, c + 2019, 0);
  for (ll i = 0; i < n; i++) {
    c[a[i]]++;
  }
  for (ll i = 0; i < 2019; i++) {
    ans += c[i] * (c[i] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int MOD = 1000000007;

signed main() {
  string s;
  cin >> s;
  vector<int> res(2019, 0);
  int ex;
  int k = 0;
  res[k]++;
  int ten = 1;
  int n = s.size();
  rep(i, n) {
    ex = s[n - 1 - i] - '0';
    k = (k + ex * ten) % 2019;
    ten = (ten * 10) % 2019;
    // cout << k << endl;
    res[k]++;
  }
  int ans = 0;
  for (auto i : res) {
    ans += i * (i - 1) / 2;
  }
  printf("%d", ans);
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define MOD 1e9 + 7
#define INF 1e9
#define SENTINEL 2e9
#define PIE 3.1415926535

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;
}
template <class T> inline T GCD(T a, T b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
template <class T> inline T LCM(T a, T b) { return (a * b) / GCD(a, b); }

int main() {
  string s;
  cin >> s;
  vector<int> v;
  // v[0] = 0;
  int cnt = 0;
  repk(i, 1, 100) {
    v[i] = 2019 * i;
    string a = to_string(v[i]);
    for (string::size_type pos = 0; (pos = s.find(a, pos)) != string::npos;
         pos++) {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
#define mod 1000000007

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int dp[3][2030];
int dpdp[2030];

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

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2019; j++) {
      dp[(i + 1) % 2][(j * 10 + (s[i] - '0')) % 2019] += dp[i % 2][j];
      dpdp[j] += dp[i % 2][j];
      dp[i % 2][j] = 0;
      // dp2[j*10+(s[i]-'0')]+=dp[i][j];
    }
    dp[(i + 1) % 2][(s[i] - '0')]++;
  }

  for (ll j = 0; j < 2019; j++) {
    dpdp[j] += dp[n % 2][j];
  }

  // cout << dp[1][8] << endl;

  cout << dpdp[0] << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
#define mod 1000000007

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int dp[200005][2030];
int dpdp[2030];

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

  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2019; j++) {
      dp[i + 1][(j * 10 + (s[i] - '0')) % 2019] += dp[i][j];
      // dp2[j*10+(s[i]-'0')]+=dp[i][j];
    }
    for (ll j = 0; j < 2019; j++) {
      dpdp[j] += dp[i][j];
    }
    dp[i + 1][(s[i] - '0')]++;
  }

  for (ll j = 0; j < 2019; j++) {
    dpdp[j] += dp[n][j];
  }

  // cout << dp[1][8] << endl;

  cout << dpdp[0] << endl;

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

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ll long long
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007
#define PI 3.141592653589793

const int mod = 2019;

int main() {
  ios;
  int t = 1;
  // cin>>t;
  while (t--) {
    string s;
    cin >> s;
    int n = sz(s);
    int suf = 0;
    int power_of_ten = 1;
    vi cnt_suf(mod + 1);
    ll answer = 0;
    cnt_suf[suf]++;
    for (int i = n - 1; i >= 0; --i) {
      int digit = s[i] - '0';
      suf = (suf + digit * power_of_ten) % mod;
      power_of_ten = (power_of_ten * 10) % mod;
      answer += cnt_suf[suf];
      cnt_suf[suf]++;
    }
    cout << answer;
    cout << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ll long long
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007

int main() {
  ios;
  string s;
  int n;
  cin >> s;
  n = sz(s);
  int numMultiples = 0, remainder, prefixesFound;
  vector<set<int>> seenRemainders(n);
  for (int i = 0; i < n; i++) {
    remainder = 0;
    prefixesFound = 0;
    for (int j = i; j < n; j++) {
      remainder = (10 * remainder + (s[j] - '0')) % 2019;
      if (cpresent(seenRemainders[j], remainder))
        break;
      seenRemainders[j].insert(remainder);
      if (remainder == 0)
        prefixesFound++;
    }
    numMultiples += prefixesFound * (prefixesFound + 1) / 2;
  }
  cout << numMultiples;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int tot = 0;
  vector<int> count(m);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    count[tot]++;
    tot += (s[i] - '0') * x;
    tot %= m;
    ans += count[tot];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

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

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int tot = 0;
  vector<int> count(m);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    count[tot]++;
    tot += (s[i] - '0') * x;
    ans += count[tot];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  long long N = S.size();
  if (N <= 4) {
    cout << 0 << endl;
  } else {
    vector<int> TEN(N + 1);
    TEN.at(0) = 1;
    for (long long i = 1; i < N + 1; i++) {
      TEN.at(i) = (TEN.at(i - 1) * 10) % 2019;
    }
    vector<int> A(N);
    A.at(0) = S.at(N - 1) - '0';
    for (long long i = 1; i < N; i++) {
      int B = S.at(N - 1 - i) - '0';
      A.at(i) = (A.at(i - 1) + (B * TEN.at(i))) % 2019;
    }
    vector<long long> list(2019, 0);
    for (long long i = 0; i < N; i++) {
      list.at(A.at(i)) = list.at(A.at(i)) + 1;
    }
    long long sum = 0;
    for (long long i = 0; i < 2019; i++) {
      sum = sum + ((list.at(i) * (list.at(i) - 1)) / 2);
    }
    cout << sum + list.at(0) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  long long N = S.size();
  if (N <= 4) {
    cout << 0 << endl;
  } else {
    vector<int> TEN(N + 1);
    TEN.at(0) = 1;
    for (long long i = 1; i < N + 1; i++) {
      TEN.at(i) = (TEN.at(i - 1) * 10) % 2019;
    }
    vector<int> A(N);
    A.at(0) = S.at(N - 1) - '0';
    for (long long i = 1; i < N; i++) {
      int B = S.at(N - 1 - i) - '0';
      A.at(i) = (A.at(i - 1) + (B * TEN.at(i))) % 2019;
    }
    vector<long long> list(2019);
    for (long long i = 0; i < N; i++) {
      list.at(A.at(i)) = list.at(A.at(i)) + 1;
    }
    long long sum = 0;
    for (long long i = 0; i < N; i++) {
      sum = sum + list.at(i) + (list.at(i) * (list.at(i) - 1)) / 2;
    }
    cout << sum << endl;
  }
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 2e5 + 2;
map<int, int> cnt;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 2e5 + 9;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s;
  int r = 0, ans = 0;
  cin >> s;
  /*for(int i=0;i<s.size();i++){
      r=( (r*10)+(s[i]-'0') )%2019;
      cout<<r<<" ";
      if(r==0){
          ans++;
          //r=s[i]-'0';
      }
      //cout<<r<<" ";
  }*/
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 0; j <= s.size() - i; j++) {
      string t = s.substr(j, i);
      int r = 0;
      for (int k = 0; k < t.size(); k++) {
        r = ((r * 10) + (t[k] - '0')) % 2019;
      }
      if (r == 0)
        ans++;
    }
  }
  cout << ans << '\\n';

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

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long

#define for(i, a, b) for (i = a; i < b; i++)
#define forb(i, a, b) for (i = a; i > b; i--)
#define all(S) S.begin(), S.end()
#define sort(S) sort(all(S))

const int inf = 1e9;
const ll INF = 1e18;

int dp[2019];

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  string s;
  ll i, j, n, m = 0;
  cin >> s;
  n = s.size();
  for (i, 0, 2019)
    dp[i] = 0;
  dp[s[0] - '0']++;
  dp[0]++;
  for (i, 1, n) {
    int q = s[i] - '0';
    bool b = false;
    vector<int> V(2019);
    for (j, 0, 2019) {
      V[(j * 10 + q) % 2019] = dp[j];
      if ((j * 10 + q) % 2019 == 0) {
        b = true;
        m += dp[j];
        V[(j * 10 + q) % 2019]++;
      }
    }
    if (!b)
      dp[0]++;
    for (j, 0, 2019)
      dp[j] = V[j];
  }
  cout << m << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long

#define for(i, a, b) for (i = a; i < b; i++)
#define forb(i, a, b) for (i = a; i > b; i--)
#define all(S) S.begin(), S.end()
#define sort(S) sort(all(S))

const int inf = 1e9;
const ll INF = 1e18;

vector<pair<int, int>> dp[200000];

int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  string s;
  ll i, j, n, m = 0;
  cin >> s;
  n = s.size();
  dp[0].pb(mp(s[0] - '0', 1));
  dp[0].pb(mp(0, 1));
  for (i, 1, n) {
    int q = s[i] - '0';
    bool b = false;
    for (j, 0, dp[i - 1].size()) {
      dp[i].pb(mp((dp[i - 1][j].f * 10 + q) % 2019, dp[i - 1][j].s));
      // cout << i << " " << (dp[i-1][j].f*10+q)%2019 << " " << dp[i-1][j].s <<
      // endl;
      if ((dp[i - 1][j].f * 10 + q) % 2019 == 0) {
        b = true;
        m += dp[i - 1][j].s;
        dp[i].back().s++;
      }
    }
    if (!b)
      dp[i].pb(mp(0, 1));
  }
  cout << m << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;

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

  u64 k = 2019;
  u64 ans = 0;
  boost::dynamic_bitset<> mat(S.size() + 1);

  for (u64 b = 0; b < S.size(); b++) {
    u64 tdiv = 0;
    u64 bs = 0;

    if (mat[b])
      continue;

    for (u64 e = b; e < S.size(); e++) {
      tdiv = tdiv * 10 + S[e] - '0';
      bool r = (tdiv != 0);

      tdiv = tdiv - (tdiv / k) * k;

      if (r && tdiv == 0) {
        if (mat[e + 1])
          break;

        bs++;
        ans += bs;
        mat[e + 1] = 1;
      }
    }
  }

  cout << ans;
}


Error: TLE

Fixed submission:

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

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

  u64 k = 2019;
  u64 ans = 0;

  for (u64 b = 0; b < S.size(); b++) {
    u64 tdiv = 0;
    for (u64 e = b; e < S.size(); e++) {
      tdiv = tdiv * 10 + S[e] - '0';
      bool r = (tdiv != 0);

      tdiv = tdiv - (tdiv / k) * k;
      if (r && tdiv == 0) {
        ans++;
      }
    }
  }

  cout << ans;
}