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


#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 tdp[2019];
ll ans, dp[2019];
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> s;
  clr(dp, 0);
  dp[s[0] - '0'] = 1;
  For(i, 1, s.size() - 1) {
    int d = s[i] - '0';
    clr(tdp, 0);
    tdp[d] = 1;
    For(j, 0, 2019 - 1) {
      if (dp[j] > 0) {
        ll rem = ((j << 3) + (j << 1) + d) % 2019;
        if (rem == 0)
          ans += dp[j];
        tdp[rem] += dp[j];
      }
    }
    For(i, 0, 2019 - 1) dp[i] = tdp[i];
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

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)
#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)) % 2019;
    ans += cnt[now], ++cnt[now];
    p = ((p << 3) + (p << 1));
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using std::cin;
using std::cout;

int main() {
  std::string s;
  cin >> s;
  ll ans = 0;
  ll n = s.size();
  std::vector<ll> val(2019, 0);
  ll mod = 0;
  ll factor = 1;
  val[0]++;
  for (int i = 0; i < n; i++) {
    ll num = s[n - 1 - i] - '0';
    mod = (mod + factor * num) % 2019;
    factor = factor * 10 % 2019;
    val[mod]++;
  }
  for (int i = 0; i < 2019; i++) {
    ll v = val[i];
    ans += v * (v - 1) / 2;
  }

  cout << ans << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using ll = long long;
using std::cin;
using std::cout;

int main() {
  std::string s;
  cin >> s;
  ll ans = 0;
  for (int i = 0; i < s.size() - 3; i++) {
    for (int j = 4; j <= s.size() - i; j++) {
      ll num = std::strtoll(s.substr(i, j).c_str(), nullptr, 10);
      if (num % 2019 == 0) {
        ans++;
      }
      // cout << num << "\\n";
    }
  }
  cout << ans << std::endl;

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

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

int main() {
  string s;
  cin >> s;
  int length = s.size();
  vector<int> surplus(2019, 0);

  int cur = 0;
  surplus[cur]++;
  int fac = 1;

  for (int i = length - 1; i >= 0; i--) {
    int add = s[i] - '0';
    cur = (cur + fac * add) % 2019;
    fac = (fac * 10) % 2019;
    surplus[cur]++;
  }

  int result = 0;
  for (int sur : surplus) {
    result += sur * (sur - 1) >> 1;
  }
  cout << result << "\\n";
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
int main() {
  string s;
  cin >> s;
  cin >> s;
  int length = s.size();

  long long int count = 0;
  for (int i = 0; i < length; i++) {
    int remainder = s.at(i) - '0';
    for (int j = i + 1; j < length; j++) {
      remainder = (remainder * 10 + (s.at(j) - '0')) % 2019;
      if (remainder == 0)
        count++;
    }
  }

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


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

int main() {

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

  auto dp = vector<int>(2019, 0);
  dp[c[0]] = 1;
  auto old = vector<int>(2019, 0);
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    for (int k = 0; k < 2019; k++)
      old[k] = 0;
    swap(dp, old);
    dp[c[i]] += 1;
    for (int j = 0; j < 2019; j++) {
      int j2 = (10 * j + c[i]) % 2019;
      dp[j2] += old[j];
    }
    sum += dp[0];
  }
  cout << sum << endl;
  return 0;
}


Error: MLE

Fixed submission:


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

template <typename T>
inline std::vector<std::vector<T>>
construct_vector_2d(int arraySize1, int arraySize2, T initialValue) {

  auto r = std::vector<std::vector<T>>(arraySize1);
  for (int i = 0; i < arraySize1; i++)
    r[i] = std::vector<T>(arraySize2, initialValue);
  return r;
}

int main() {

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

  auto dp = construct_vector_2d(n, 2019, 0);
  dp[0][c[0]] = 1;
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    dp[i][c[i]] += 1;
    for (int j = 0; j < 2019; j++) {
      int j2 = (10 * j + c[i]) % 2019;
      dp[i][j2] += dp[i - 1][j];
    }
    sum += dp[i][0];
  }
  cout << sum << 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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int p = 2019;
  string s;
  cin >> s;

  int n = s.size();
  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % p;
    d[i] = (d[i + 1] + a) % p;
    ten *= 10;
    ten %= p;
  }
  vector<int> cnt(p);
  ll ans = 0;
  for (int i = n; i >= 0; --i) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

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

int main() {
  int n, p;
  cin >> n;
  p = 2019;
  string s;
  cin >> s;

  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % p;
    d[i] = (d[i + 1] + a) % p;
    ten *= 10;
    ten %= p;
  }
  vector<int> cnt(p);
  ll ans = 0;
  for (int i = n; i >= 0; --i) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  string S;
  int N;
  cin >> S;
  N = S.length();

  ll ans = 0;
  ll tmp = 0;
  ll pow_10 = 1;
  vl d(2020, -1);

  for (int i = N - 1; i >= 0; i--) {
    tmp = ((int(S[i] - '0') * pow_10) + tmp) % 2019;
    pow_10 = (pow_10 * 10) % 2019;
    // cout << tmp << endl;
    d[tmp]++;
  }

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

  // cout << "======" << endl;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<ll>

int main() {

  string S;
  int N;
  cin >> S;
  N = S.length();

  ll ans = 0;
  ll tmp;

  for (int i = 0; i < N; ++i) {
    tmp = 0;
    // cout << int(S[i]-'0') << endl;
    for (int j = i; j < N; j++) {
      tmp = (tmp * 10 + int(S[j]) - '0') % 2019;
      if (tmp == 0)
        ans++;
      // cout << i << " " << j << " " << tmp << endl;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long modExp(long long a, long long b, long long m) {
  long long r = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1)
      r = (r * a) % m;
    b = b / 2;
    a = (a * a) % m;
  }
  return r % m;
}
int main() {
  string s;
  cin >> s;
  int n = s.length();
  reverse(s.begin(), s.end());
  vector<int> val(n);
  for (int i = 0; i < n; i++) {
    val[i] = ((s[i] - '0') * modExp(10, i, 2019)) % 2019;
  }
  vector<int> prefix(n);
  prefix[0] = val[0];
  for (int i = 1; i < n; i++)
    prefix[i] = (prefix[i - 1] + val[i]) % 2019;
  map<long long, long long> count;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (prefix[i] == 0)
      ans++;
    ans += count[prefix[i]];
    count[prefix[i]]++;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.length();
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 4; i + j <= n; j++) {
      long long num = stoll(s.substr(i, j));
      // cout << num << endl;
      if (num % 2019 == 0) {
        count++;
        // cout << num << " " << i << endl;
        i += j - 2;
        // cout << i << endl;
        break;
      }
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
#define int int64_t
  int N, P;
  // cin>>N>>P;
  string S;
  cin >> S;
  N = S.size();
  P = 2019;
  int X = 0;
  if (P == 2 or P == 5) {
    for (int i = 0; i < N; i++) {
      if ((int(S.at(i)) - 48) % P == 0) {
        X += (i + 1);
      }
    }
    cout << X << "\\n";
    return 0;
  }
  unordered_map<int, int> MOD;
  int R = 0;
  int Z = 1;
  MOD[0] += 1;
  for (int i = 0; i < N; i++) {
    R = (R + (Z * (int(S.at(N - i - 1)) - 48))) % P;
    X += MOD[R];
    MOD[R] += 1;
    Z = (Z * 10) % P;
  }
  cout << X << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

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

  int ans = 0;
  long long int tmp = 0;
  const int n = s.size();
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      tmp = std::stoull(s.substr(i, j - i));
      // cout << tmp << endl;
      if (tmp % 2019 == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> A(N + 1);
  vector<int> ten(N + 1);
  vector<int> ch(N + 2);
  ten.at(0) = 1;
  for (int i = 0; i < N; i++) {
    A.at(i + 1) = A.at(i) + (S[N - i - 1] - '0') * ten.at(i);
    A.at(i + 1) %= 2019;
    ten.at(i + 1) = ten.at(i) * 10 % 2019;
  }
  int s = 0;
  for (int i = 0; i < N - 3; i++) {
    if (ch.at(i))
      continue;
    vector<int> a;
    for (int j = i + 4; j <= N; j++) {
      if ((A.at(j) - A.at(i)) % 2019 == 0) {
        s++;
        ch.at(j) = 1;
        j += 3;
        a.push_back(j);
      }
    }
    int b = a.size();
    s += b * (b - 1) / 2;
  }
  cout << s << endl;
}


Error: WA

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> A(N + 1);
  vector<int> ten(N + 1);
  vector<int> ch(N + 2);
  ten.at(0) = 1;
  for (int i = 0; i < N; i++) {
    A.at(i + 1) = A.at(i) + (S[N - i - 1] - '0') * ten.at(i);
    A.at(i + 1) %= 2019;
    ten.at(i + 1) = ten.at(i) * 10 % 2019;
  }
  int s = 0;
  for (int i = 0; i < N - 3; i++) {
    if (ch.at(i))
      continue;
    vector<int> a;
    for (int j = i + 4; j <= N; j++) {
      if ((A.at(j) - A.at(i)) % 2019 == 0) {
        s++;
        j += 3;
        ch.at(j) = 1;
        a.push_back(j);
      }
    }
    int b = a.size();
    s += b * (b - 1) / 2;
  }
  cout << s << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = INT_MAX / 2;
const ll inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

  string s;
  cin >> s;

  map<ll, ll> mp;
  mp[0] = 1;

  reverse(all(s));

  ll now = 0, ten = 1;
  for (auto si : s) {
    now = (now + (si - '0') * ten) % 2019;
    ten *= 10;
    ten %= 2019;
    mp[now]++;
  }

  ll ans = 0;
  for (auto m : mp) {
    ans += m.second * (m.second - 1) / 2; // mC2
  }

  out(ans);

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (int)1e9;
const ll inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;

bool cmp(P a, P b) { return a.second < b.second; }

int a[101], b[101];
int dp[101][101][10010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

  string s;
  cin >> s;

  if (s.length() < 4) {
    out(0);
    return 0;
  }

  string t;
  int l = 0, r = 3;
  int cnt = 0;
  while (l + 3 <= r && r < s.length()) {
    t = s.substr(l, r - l + 1);
    if (atoi(t.c_str()) % 2019 == 0)
      cnt++;
    r++;
    if (r >= s.length()) {
      l++;
      r = l + 3;
    }
  }

  out(cnt);

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

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

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> A(N + 1);
  vector<int> ten(N + 1);
  vector<int> ch(N + 2);
  ten.at(0) = 1;
  for (int i = 0; i < N; i++) {
    A.at(i + 1) = A.at(i) + (S[N - i - 1] - '0') * ten.at(i);
    A.at(i + 1) %= 2019;
    ten.at(i + 1) = ten.at(i) * 10 % 2019;
  }
  int s = 0;
  for (int i = 0; i < N - 3; i++) {
    if (ch.at(i))
      continue;
    vector<int> a;
    for (int j = i + 4; j <= N; j++) {
      if ((A.at(j) - A.at(i)) % 2019 == 0) {
        s++;
        ch.at(j) = 1;
        j += 3;
        a.push_back(j);
      }
    }
    int b = a.size();
    s += b * (b - 1) / 2;
  }
  cout << s << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mp makepair
#define deb(x) cout << x << "\\n";
#define deB(x, y) cout << x << " " << y << "\\n";
#define Deb(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define YES cout << "YES\\n";
#define Yes cout << "Yes\\n";
#define NO cout << "NO\\n";
#define No cout << "No\\n";
#define all(v) (v).begin(), (v).end()
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  string s;
  cin >> s;
  s = '!' + s;
  ll R, r, p, j, i = 1;
  set<ll> a[s.size() + 9];
  ll ans = 0;
  while (i < s.size()) {
    R = 0;
    r = 0;
    p = 0;
    j = i;
    while (j <= s.size()) {
      R *= 10;
      R %= 3;
      R += (ll)(s[j] - '0');
      R %= 3;
      r *= 10;
      r %= 673;
      r += (ll)(s[j] - '0');
      r %= 673;
      if (a[j].find(r) != a[j].end())
        break;
      a[j].insert(r);
      if (r == 0 && R == 0)
        p++;
      j++;
    }
    ans += p * (p + 1) / 2;
    i++;
  }
  deb(ans);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int MOD = 2019;

int c[MOD];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int cur = 0;
  c[cur]++;
  string s;
  cin >> s;
  int ans = 0;
  reverse(s.begin(), s.end());
  int p = 1;
  for (auto x : s) {
    int ncur = cur + p * (x - '0');
    ncur %= MOD;
    ans += c[ncur];
    cur = ncur;
    c[cur]++;
    p *= 10;
    p %= MOD;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int MOD = 2019;

int c[MOD];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int cur = 0;
  c[cur]++;
  string s;
  cin >> s;
  int ans = 0;
  reverse(s.begin(), s.end());
  int p = 1;
  for (auto x : s) {
    int ncur = cur + p * (x - '0');
    ncur %= MOD;
    ans += c[ncur];
    cur = ncur;
    c[cur]++;
    p *= 10;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(int argc, const char *argv[]) {
  string s;
  cin >> s;

  vector<int> mods(s.size());
  mods[s.size() - 1] = s.back() - '0';
  int p = 10;
  for (int i = s.size() - 2; i >= 0; i--) {
    int d = s[i] - '0';
    mods[i] = (p * d + mods[i + 1]) % 2019;
    p = p * 10 % 2019;
  }

  vector<int> cnts(2019, 0);
  cnts[0]++;
  for (int i = 0; i < mods.size(); i++) {
    cnts[mods[i]]++;
  }

  long long ans = 0;
  for (auto c : cnts) {
    ans += c * (c - 1) / 2;
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int solve(string &s, int i, int j, vector<vector<int>> &memo) {
  if (memo[i][j] < 0) {
    if (j - i + 1 == 4) {
      int n = stoi(s.substr(i, 4));
      memo[i][j] = n % 2019;
    } else {
      int prev_mod = solve(s, i, j - 1, memo);
      memo[i][j] = (10 * prev_mod + (s[j] - '0')) % 2019;
    }
  }

  return memo[i][j];
}

int main(int argc, const char *argv[]) {
  string s;
  cin >> s;

  if (s.size() < 4) {
    cout << 0 << '\\n';
    return 0;
  }

  vector<vector<int>> memo(s.size() + 1, vector<int>(s.size() + 1, -1));
  long long ans = 0;
  for (int i = 0; i <= s.size() - 4; i++) {
    for (int j = i + 3; j < s.size(); j++) {
      int mod = solve(s, i, j, memo);
      if (mod == 0) {
        ans++;
      }
    }
  }

  cout << ans << '\\n';
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
// #include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a), i##formax = (b); i < i##formax; i++)
typedef long long ll;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  // string searchStr = "";
  // ll ssize = s.size();
  // ll ans = 0;
  // for(int i = 1; i <= 10000; i++) {　
  //     searchStr = to_string(i * 2019);
  //     if(searchStr.size() > ssize) break;
  //     if(searchStr.find("0") != string::npos) continue;

  //     int pos = 0;
  //     while (pos != string::npos) {

  //         pos = s.find(searchStr, pos );
  //         if(pos != string::npos)pos++;
  //         ans++;
  //     }
  //     ans--;
  // }
  // cout << ans << endl;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
// #include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a), i##formax = (b); i < i##formax; i++)
typedef long long ll;

int main() {
  string s;
  cin >> s;
  string searchStr = "";
  ll ans = 0;
  for (int i = 1; i <= 100000000; i++) {
    searchStr = to_string(i * 2019);
    if (searchStr.find("0") != string::npos)
      continue;

    int pos = 0;
    while (pos != string::npos) {

      pos = s.find(searchStr, pos);
      if (pos != string::npos)
        pos++;
      ans++;
    }
    ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  map<int64_t, int64_t> tmp;
  int64_t size = s.size();
  vector<int64_t> t(size);
  vector<int64_t> amari(size + 100, 0);
  amari.at(1) = 10;
  for (int64_t i = 2; i <= size + 99; i++) {
    amari.at(i) = amari.at(i - 1) * 10 % 2019;
  }
  t.at(0) = (int64_t)s.at(0) - '0';
  tmp[0] = 1;
  tmp[t.at(0)] = 1;
  for (int64_t i = 1; i < size; i++) {
    t.at(i) =
        (t.at(i - 1) + amari.at(i) * ((int64_t)s.at(i) - '0') % 2019) % 2019;
    if (tmp.count(t.at(i)))
      tmp.at(t.at(i))++;
    else
      tmp[t.at(i)] = 1;
  }
  int64_t ans = 0;
  for (auto tmpi : tmp) {
    ans += (tmpi.second * (tmpi.second - 1)) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int size = s.size();
  vector<int> t(size);
  vector<int> amari(size + 1, 0);
  amari.at(1) = 10;
  for (int i = 2; i <= size; i++) {
    amari.at(i) = amari.at(i - 1) * 10 % 2019;
  }
  t.at(0) = (int)s.at(0) - '0';
  for (int i = 1; i < size; i++) {
    t.at(i) = (t.at(i - 1) + amari.at(i) * ((int)s.at(i) - '0') % 2019) % 2019;
  }
  int ans = 0;
  reverse(t.begin(), t.end());
  t.push_back(0);
  for (int i = 0; i < size - 3; i++) {
    for (int j = i + 3; j < size + 1; j++) {
      if (t.at(i) == t.at(j))
        ans++;
    }
  }
  cout << ans << endl;
}