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

#include <bits/stdc++.h>

using namespace std;

#define N 200003

string s;
int pot10[N], r[N], v[N], freq[2021];
int main() {
  cin >> s;
  int n = s.size();

  pot10[0] = 1;
  for (int i = 1; i <= n; i++) {
    pot10[i] = (pot10[i - 1] * 10) % 2019;
  }

  s = "0" + s;
  for (int i = 0; i <= n; i++)
    v[i] = (int)(s[i] - '0');

  r[0] = 0;
  for (int i = n; i >= 1; i--)
    r[i] = (r[i + 1] + v[i] * pot10[n - i]) % 2019;

  freq[0] = 1;
  for (int i = 1; i <= n; i++) {
    freq[r[i]]++;
  }
  long long ans = 0;

  for (int i = 0; i < 2019; i++)
    if (freq[i] >= 2)
      ans += (freq[i] * (freq[i] - 1)) / 2;

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

string s;
long long int s_input[200002];
long long int sum_acum[200002];
long long int res_acum[200002];
long long int freq[2019];

int main() {
  cin >> s;

  for (int i = 0; i < s.size(); i++)
    s_input[i] = (int)s[i] - 48;

  long long int pot = s.size() - 1;

  sum_acum[0] = 0;
  for (int i = 1; i <= s.size(); i++) {
    sum_acum[i] = s_input[i - 1] * ((long long)pow(10, pot)) + sum_acum[i - 1];
    pot -= 1;
  }

  res_acum[0] = 0;
  for (int i = 1; i <= s.size(); i++) {
    res_acum[i] = sum_acum[i] % 2019;
  }

  for (int i = 0; i < 2019; i++)
    freq[i] = 0;

  for (int i = 0; i <= s.size(); i++) {
    freq[res_acum[i]]++;
  }

  long long int ans = 0;

  for (int i = 0; i < 2019; i++)
    if (freq[i] >= 2)
      ans += (freq[i] * (freq[i] - 1)) / 2;

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

  map<int, set<int>> mods;
  mods[0].insert(-1);
  reverse(ALL(s));
  lint num = 0;
  int keta = 1;
  REP(i, s.size()) {
    int x = s[i] - '0';
    num = (num + x * keta) % 2019;
    keta = keta * 10 % 2019;
    // cout << num << endl;
    mods[num].insert(i);
  }

  lint sum = 0;
  for (auto p : mods) {
    sum += p.second.size() * (p.second.size() - 1) / 2;
  }

  cout << sum << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
const lint mod = 1e9 + 7;

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

  map<int, int> mods;
  mods[0] = 1;

  REP(i, s.size()) {
    // string->lint
    string tmp = s.substr(s.size() - (i + 1), s.size());
    istringstream ss = istringstream(tmp);
    lint num;
    ss >> num;
    mods[num % 2019]++;
  }

  lint sum = 0;
  for (auto p : mods) {
    sum += p.second / 2 * (p.second - 1);
  }

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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

int mod(int a, int b) {
  int res = a % b;
  if (res < 0)
    res += b;
  return res;
}

int main() {
  string s;
  cin >> s;
  map<int, int> memo;
  vector<int> res;
  int b = 1;
  for (int i = 0; i <= 200005; i++) {
    if (i == 0) {
      res.push_back(1);
      continue;
    }
    b *= 10;
    b = mod(b, 2019);
    res.push_back(b);
  }
  memo[0]++;
  int a = 0;
  for (int i = 0; i < s.length(); i++) {
    a += res[i] * (s[s.size() - 1 - i] - '0');
    a = mod(a, 2019);
    memo[a]++;
  }
  ll ans = 0;
  for (auto itr = memo.begin(); itr != memo.end(); itr++) {
    ans += itr->second * (itr->second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> llP;

int mod(int a, int b) {
  int res = a % b;
  if (res < 0)
    res += b;
  return res;
}

int main() {
  string s;
  cin >> s;
  map<int, int> memo;
  vector<int> res;
  int b = 1;
  for (int i = 0; i <= 20005; i++) {
    if (i == 0) {
      res.push_back(1);
      continue;
    }
    b *= 10;
    b = mod(b, 2019);
    res.push_back(b);
  }
  memo[0]++;
  int a = 0;
  for (unsigned long int i = 0; i < s.size(); i++) {
    a += res[i] * (s[s.size() - 1 - i] - '0');
    a = mod(a, 2019);
    memo[a]++;
  }
  ll ans = 0;
  for (auto itr = memo.begin(); itr != memo.end(); itr++) {
    ans += itr->second * (itr->second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  string S;
  cin >> S;
  long long N = S.size();
  vector<long long> V(N + 1);
  V[0] = 0;
  long long ten = 1;
  map<long long, long long> mp;
  mp[0]++;
  for (int i = 1; i <= N; i++) {
    V[i] = ((S[N - i] - '0') * ten + V[i - 1]) % 2019;
    ten *= 10;
    ten %= 2019;
    mp[V[i]]++;
  }
  long long ans = 0;
  for (auto p : mp) {
    long long k = p.second;
    ans += k * (k - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  string S;
  cin >> S;
  vector<string> V(200000);
  for (long long i = 0; i < 200000; i++)
    V[i] = (std::to_string(2019 * (i + 1)));
  long long N = S.size();
  long long ans = 0;
  for (long long i = 0; i < 200000; i++) {
    string Q = V[i];
    long long M = Q.size();
    if (N < M)
      break;
    for (long long i = 0; i < N - M + 1; i++) {
      if (S.substr(i, M) == Q) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned long long

#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n"
#define db3(x, y, z)                                                           \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y   \\
       << ", " << z << ")\\n"

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  const int MOD = 2019;

  string s;
  cin >> s;
  int n = s.size();
  int suf = 0; // suffix num
  int pot = 1; // power of t0
  ll ans = 0;
  vector<int> cnt_suf(MOD);
  ++cnt_suf[suf]; // empty sufix

  for (int i = n - 1; i >= 0; --i) {
    int digit = s[i] - '0';
    suf = (suf + digit * pot) % MOD;
    pot = 10 * pot % MOD;
    ans += cnt_suf[suf];
    ++cnt_suf[suf];
    // db3(digit, suf, ans);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define ull unsigned long long

#define db(x) cerr << #x << " = " << x << endl
#define db2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n"
#define db3(x, y, z)                                                           \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y   \\
       << ", " << z << ")\\n"

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  const int MOD = 2019;

  string s;
  cin >> s;
  int n = s.size();
  int suf = 0; // suffix num
  int pot = 1; // power of t0
  int ans = 0;
  vector<int> cnt_suf(MOD);
  ++cnt_suf[ans]; // empty sufix

  for (int i = n - 1; i >= 0; --i) {
    int digit = s[i] - '0';
    suf = (suf + digit * pot) % MOD;
    pot = (10 * pot) % MOD;
    ans += cnt_suf[suf];
    ++cnt_suf[suf];
    db3(digit, suf, ans);
  }

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

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

#include <iostream>
using namespace std;
int main() {
  string s;
  cin >> s;

  int divisor = 2019;
  int rem_count[2019] = {1};
  int num = 0;
  int pow10 = 1;
  int total = 0;

  int charac[s.length()];
  for (int i = 0; i < s.length(); i++) {
    charac[i] = s[i] - 48;
  }
  for (int i = s.length() - 1; i >= 0; i--) {
    int dig = charac[i];
    num += dig * pow10;
    int rem = num % divisor;
    total += rem_count[rem];
    pow10 *= 10;
    rem_count[rem] += 1;

    num = rem;
    pow10 %= divisor;
  }

  cout << total;
}

Error: WA

Fixed submission:

#include <iostream>
#include <iterator>
#include <map>
#include <string>

int nc2(int val) {
  if (val < 2)
    return 0;
  return (val * (val - 1)) / 2;
}

using namespace std;
int main() {
  string data;
  int ans = 0;
  cin >> data;
  long subs[data.length()];
  for (int i = 0; i < data.length(); i++) {
    subs[i] = stol(data.substr(i, data.length() - i));
    // cout<<subs[i]<<endl;
  }
  cout << endl;
  int freq[2019] = {0};
  for (int i = 0; i < data.length(); i++) {
    subs[i] = subs[i] % 2019;
    freq[subs[i]]++;
    if (subs[i] == 0)
      ans++;
    // cout<<subs[i]<<endl;
  }

  for (int i = 0; i < 2019; i++) {
    ans += nc2(freq[i]);
  }
  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;
using ld = long double;
template <typename T> using v2 = vector<vector<T>>;
template <typename T> inline v2<T> fill(int r, int c, T t) {
  return v2<T>(r, vector<T>(c, t));
}

void solve() {
  string s;
  cin >> s;
  int n = s.length();

  int ans = 0;
  array<int, 2019> seen = {};
  seen[0] = 1;
  int sum = 0;
  int mult = 1;
  for (int i = n - 1; i >= 0; --i) {
    sum = (sum + mult * (s[i] - '0')) % 2019;
    ans += seen[sum];
    seen[sum]++;
    mult = (mult * 10) % 2019;
  }

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

const int MOD = 2019;
int add(int a, int b) { return (a + b) % MOD; }
int mul(int a, int b) { return (a * b) % MOD; }

void solve() {
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    int sum = 0;
    for (int j = i; j < s.length(); j++) {
      sum = add(s[j] - '0', mul(sum, 10));
      if (sum == 0) {
        ans++;
      }
    }
  }
  cout << ans << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int b[N];    // b[len-i]表示第i位（从后往前）10^(i-1)
int k[2020]; // k[i]表示余数是i的个数
long long ans = 0;
int main() {
  string s;
  cin >> s;
  int len = s.length();

  b[len - 1] = 1;
  for (int i = len - 2; i >= 0; i--) {
    b[i] = (b[i + 1] * 10) % 2019;
  }

  int sum = 0;
  for (int i = len - 1; i >= 0; i--) {
    sum = (sum + (s[i] - '0') * b[i]) % 2019;
    k[sum]++;
  }

  ans += k[0]; // 余数是零直接符合

  // 如果两个数余数相等，相减后余数为0
  for (int i = 0; i <= 2018; i++)
    ans += k[i] * (k[i] - 1) / 2;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, c;
  int n;
  int ans = 0;
  cin >> s;
  int len = s.length();
  for (int i = 0; i < len; i++) {
    for (int j = 4; i + j <= len; j++) {
      c = s.substr(i, j);
      stringstream ss;
      ss << c;
      ss >> n;
      if (n % 2019 == 0)
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

int main() {
  string s;
  int ans = 0;
  cin >> s;
  int start = 0;
  rep(j, s.size()) {
    int c = 0;
    int i = j;
    while (i < s.size()) {
      rep(k, 4) {

        c = c * 10 + s[i] - '0';
        i++;
        if (c % 2019 == 0 || i >= s.size())
          break;
      }
      if (c % 2019 == 0)
        ans++;
      c %= 2019;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
int MOD = 1000000007;

int toNumAndMod(string s, int k) {
  string sub = s.substr(k);
  return atol(sub.c_str()) % 2019;
}
const int m = 2019;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  // int cnt = 0;
  // vector<int> a(2019);
  // a.at(0) += 1;
  // for(int i=0;i<s.size();i++){
  // 	int tmp = toNumAndMod(s,i);
  // 	//cout << "tmp=" << tmp << ", i=" << i << endl;
  // 	a.at(tmp)++;
  // }
  // for(int i=0;i<2019;i++){
  // 	if(a.at(i) > 1){
  // 		cnt += a.at(i)-1;
  // 	}
  // }
  // cout << cnt << endl;
  int n = s.size();
  reverse(s.begin(), s.end());
  int x = 1;
  int total = 0;
  vector<int> cnt(m);
  int ans = 0;
  rep(i, n) {
    cnt[total]++;
    total += (s[i] - '0') * x;
    total %= m;
    ans += cnt[total];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < s.size() - 4; i++) {
    for (int j = i + 4; j < s.size(); j++) {
      string sub = s.substr(i, j - i + 1);
      int num = atol(sub.c_str());
      // cout << num << endl;
      if (num % 2019 == 0) {
        cnt++;
        // cout << "i=" << i << ", j=" << j << endl;
        // cout << num << endl;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int64_t, int64_t>;

// std::cout << std::setprecision(20) << 1.1 << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  constexpr int32_t kP = 2019;
  const int64_t L = static_cast<int64_t>(S.length());
  vector<int64_t> P(L, 0);
  P[L - 1] = 1;
  for (int32_t i = 1; i < L; ++i) {
    P[L - 1 - i] = (P[L - i] * 10) % kP;
  }
  vector<int64_t> A(L, 0);
  vector<int64_t> sum(L + 1, 0);
  vector<int64_t> hist(kP, 0);
  sum[0] = 0;
  hist[0]++;
  for (int64_t i = 0; i < L; ++i) {
    const int64_t n = static_cast<int64_t>(S.c_str()[i] - '0');
    A[i] = (n * P[i]) % kP;
    sum[i + 1] = (sum[i] + A[i]) % kP;
    hist[sum[i + 1]]++;
  }
  int64_t result = 0;
  for (int64_t i = 0; i < static_cast<int64_t>(hist.size()); ++i) {
    if (hist[i] >= 2) {
      result += hist[i] * (hist[i] - 1) / 2;
    }
  }
  cout << result << endl;

  return 0;
}


Error: TLE

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
#include <iostream>

constexpr int32_t kMaxLoop = 1000000;
constexpr int32_t kMaxLength = 200000;
constexpr int32_t kPrime673 = 673;

int32_t look_up_10exp_mod[kPrime673 - 1];

struct LookUp {
  int32_t mod3{0};
  int32_t mod673{0};
};

LookUp look_up[kMaxLength + 1];

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::string text;
  std::cin >> text;
  const int32_t length = static_cast<int32_t>(text.length());
  int32_t count = 0;
  look_up_10exp_mod[0] = 1;
  for (int32_t k = 1; k <= kPrime673 - 2; ++k) {
    look_up_10exp_mod[k] = ((look_up_10exp_mod[k - 1] * 10) % kPrime673);
  }
  for (int32_t sub_length = 1; sub_length <= length; ++sub_length) {
    for (int32_t i = 0; i < length - sub_length + 1; ++i) {
      // const int32_t j = i + sub_length - 1;
      const int32_t value_end_digit =
          static_cast<int32_t>(std::stoi(text.substr(i, 1)));
      look_up[i].mod3 = (look_up[i + 1].mod3 + value_end_digit) % 3;
      look_up[i].mod673 =
          (look_up[i + 1].mod673 +
           value_end_digit *
               look_up_10exp_mod[(sub_length - 1) % (kPrime673 - 1)]) %
          kPrime673;
      if (look_up[i].mod3 == 0 && look_up[i].mod673 == 0) {
        count++;
      }
    }
  }
  std::cout << count << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

#define MOD 2019

int binary_square(int x, int n);

int main() {
  string s;
  cin >> s;
  int s_len = s.length();
  int *dp = new int[s_len + 1];
  dp[s_len] = 0;
  rep2(i, 1, s_len + 1) {
    int a = s[s_len - i] - '0';
    // cout << a << endl;
    dp[s_len - i] =
        (dp[s_len - i + 1] + binary_square(10, i - 1) * a % MOD) % MOD;
    // cout << binary_square(10, i-1) << endl;
  }
  int cnt = 0;
  rep2(i, 0, 2019) {
    int m = 0;
    rep(j, s_len + 1) {
      if (dp[j] == i) {
        m++;
      }
    }
    cnt += m * (m - 1) / 2;
  }
  // rep(i, s_len){
  //     //cout << dp[i] << endl;
  //     rep2(j, i+1, s_len+1){
  //         if(dp[i]==dp[j]) cnt++;
  //     }
  // }
  cout << cnt << endl;
  return 0;
}

int binary_square(int x, int n) {
  int m = MOD;
  int ans = 1;
  while (n) {
    if (n % 2 == 1) {
      ans = ans * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return ans % m;
}

Error: TLE

Fixed submission:

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

#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

#define MOD 2019

int binary_square(int x, int n);

int main() {
  string s;
  cin >> s;
  int s_len = s.length();
  int *dp = new int[s_len + 1];
  dp[s_len] = 0;
  rep2(i, 1, s_len + 1) {
    int a = s[s_len - i] - '0';
    // cout << a << endl;
    dp[s_len - i] =
        (dp[s_len - i + 1] + binary_square(10, i - 1) * a % MOD) % MOD;
    // cout << binary_square(10, i-1) << endl;
  }
  int cnt = 0;
  rep(i, s_len) {
    // cout << dp[i] << endl;
    rep2(j, i + 1, s_len + 1) {
      if (dp[i] == dp[j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}

int binary_square(int x, int n) {
  int m = MOD;
  int ans = 1;
  while (n) {
    if (n % 2 == 1) {
      ans = ans * x % m;
    }
    x = x * x % m;
    n >>= 1;
  }
  return ans % m;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

/* Great things never come from comfort zones,
"whatever the mind of a man can conceive and believe,it can achieve." */

#include <bits/stdc++.h>
#define ll long long
#define scf(n) scanf("%d", &n)
#define lscf(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define inf INT_MAX
#define ninf INT_MIN
#define gcd __gcd
#define bitcount(n) __builtin_popcount(n)
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e6 + 7;

int main() {

  string s;
  cin >> s;

  ll ctr = 0;

  int n = s.size();

  vector<int> v(2019, 0);

  v[s[0] - '0'] = 1;

  rep(i, 1, n) {
    int d = s[i] - '0';

    vector<int> a(2019, 0);

    a[d] = 1;

    rep(j, 0, 2019) {
      if (v[j] > 0) {
        ll r = (10 * j + d) % 2019;

        if (r == 0)
          ctr += v[j];

        a[r] += v[j];
      }
    }

    rep(i, 0, 2019) v[i] = a[i];
  }

  lprin(ctr);
}


Error: TLE

Fixed submission:


// A program to demonstrate the use of stringstream
#include <iostream>
#include <sstream>
using namespace std;
typedef long long ll;

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

  int ctr = 0;

  int n = s.size();

  for (int i = 0; i <= n - 3; i++) {

    for (int j = i + 3; j < n; j++) {

      string r = s.substr(i, j + 1);

      if (r.size() >= 4) {
        stringstream geek(r);

        ll x;
        geek >> x;

        if (x % 2019 == 0)
          ctr++;
      }
    }
  }

  cout << ctr << endl;
}