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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
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, tot = 0;
  vector<int> cnt(m);
  ll ans = 0;
  rep(i, 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 <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() {
  string s;
  cin >> s;
  int n = s.size();

  vector<string> bs;
  ll mx = 20000;
  for (ll i = 1; i <= mx; i++)
    bs.push_back(to_string(2019 * i));

  ll ans = 0;
  for (string sub : bs) {
    int size = sub.size();
    for (int i = 0; i < n; i++) {
      // printf("s: %s, sub: %s, strncmp: %d\\n", &s[i], sub.c_str(),
      //        strncmp(&s[i], sub.c_str(), size));
      if (!strncmp(&s[i], sub.c_str(), size))
        ans++;

      // int pos = s.find(sub);
      // while (pos != string::npos) {
      //   pos = s.find(sub, pos + size);
      //   ans++;
    }
  }
  // rep(i, n - 3) {
  //   for (int j = i + 3; j < n; j++) {
  //     string sub = s.substr(i, j - i + 1);
  //     if (stoll(sub) % 2019 == 0) ans++;
  //   }
  // }
  std::cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#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>;
const ll MOD = 2019;

ll modPow(ll a, ll n) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1)) % MOD;
  ll t = modPow(a, n / 2);
  return (t * t) % MOD;
}

int main() {
  string S;
  cin >> S;
  vector<ll> T(S.size());
  T.at(0) = (S.at(S.size() - 1) - '0');
  for (ll i = 1; i < S.size(); i++) {
    T.at(i) = T.at(i - 1) + (S.at(S.size() - i - 1) - '0') * modPow(10, i);
    T.at(i) %= MOD;
  }
  sort(T.begin(), T.end());
  ll t = 0, x = 1, ans = 0;
  for (ll i = 0; i < S.size(); i++) {
    if (t == T.at(i)) {
      x++;
    } else {
      ans += (x * (x - 1)) / 2;
      x = 1;
      t = T.at(i);
    }
  }
  ans += (x * (x - 1)) / 2;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int64_t N = S.size();
  vector<vector<int64_t>> A(N, vector<int64_t>(2019, 0));
  vector<int64_t> s(N);
  for (int i = 0; i < N; i++) {
    s.at(i) = S.at(i) - '0';
  }
  if (N < 3) {
    cout << "0" << endl;
    return 0;
  }
  queue<int64_t> B;
  A.at(3).at(s.at(3) % 2019)++;
  B.push(s.at(3));
  A.at(3).at((s.at(2) * 10 + s.at(3)) % 2019)++;
  B.push(s.at(2) * 10 + s.at(3));
  A.at(3).at((s.at(1) * 100 + s.at(2) * 10 + s.at(3)) % 2019)++;
  B.push(s.at(1) * 100 + s.at(2) * 10 + s.at(3));
  A.at(3).at((s.at(0) * 1000 + s.at(1) * 100 + s.at(2) * 10 + s.at(3)) %
             2019)++;
  B.push((s.at(0) * 1000 + s.at(1) * 100 + s.at(2) * 10 + s.at(3)) % 2019);
  int64_t x = A.at(3).at(0), y;
  y = 4;

  for (int i = 4; i < N; i++) {
    int z = 1;
    A.at(i).at(s.at(i))++;
    B.push(s.at(i));
    for (int j = 0; j < y; j++) {
      if (A.at(i).at((B.front() * 10 + s.at(i)) % 2019) == 0) {
        z++;
        A.at(i).at((B.front() * 10 + s.at(i)) % 2019) +=
            A.at(i - 1).at(B.front());
        B.push((B.front() * 10 + s.at(i)) % 2019);
        B.pop();
      } else {
        A.at(i).at((B.front() * 10 + s.at(i)) % 2019) +=
            A.at(i - 1).at(B.front());
        B.pop();
      }
    }
    x += A.at(i).at(0);
    y = z;
  }
  cout << x << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  cin >> s;
  int len = (int)(s.length());
  int cnt[2019] = {};
  reverse(s.begin(), s.end());
  int total = 0, x = 1, ans = 0;
  for (int i = 0; i < len; i++) {
    cnt[total]++;
    // -'0'によって文字コードを数値に変換している
    total += (s[i] - '0') * x;
    total %= 2019;
    x = x * 10 % 2019;
    // 1+2+3+4+...
    ans += cnt[total];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  string s;
  cin >> s;
  int cnt = 0;
  int len = (int)(s.length());
  for (int i = 0; i < len - 3; i++) {
    for (int j = 5; j <= len - i; j++) {
      int temp = atoi((s.substr(i, j)).c_str());
      if ((temp % 2019) == 0) {
        cnt++;
      }
    }
  }
  cout << cnt;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll ans;
int x;
const int N = 2e5 + 5;
int main() {
  io();
  string s;
  int n, p;

  cin >> s;
  n = s.size();
  map<int, int> f;
  p = 2019;
  if (p == 2) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % 2 == 0) {
        ans += (i + 1);
      }
    }
    cout << ans;
  } else if (p == 5) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '5' || s[i] == '0') {
        ans += (i + 1);
      }
    }
    cout << ans;
  } else {
    reverse(s.begin(), s.end());
    int num = 0;
    f[0] = 1;
    int t = 1;
    for (int i = 0; i < n; i++) {
      num = num + t * (s[i] - '0');
      t = t * 10 % p;
      num = num % p;
      f[num]++;
    }

    for (auto it : f) {

      ans += ((1LL * it.second * (it.second - 1)) / 2);
    }
    cout << ans;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second

void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
ll ans;
int x;
const int N = 2e5 + 5;
int main() {
  io();
  string s;
  int n, p;
  cin >> n;
  cin >> s;
  map<int, int> f;
  p = 2019;
  if (p == 2) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % 2 == 0) {
        ans += (i + 1);
      }
    }
    cout << ans;
  } else if (p == 5) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '5' || s[i] == '0') {
        ans += (i + 1);
      }
    }
    cout << ans;
  } else {
    reverse(s.begin(), s.end());
    int num = 0;
    f[0] = 1;
    int t = 1;
    for (int i = 0; i < n; i++) {
      num = num + t * (s[i] - '0');
      t = t * 10 % p;
      num = num % p;
      f[num]++;
    }

    for (auto it : f) {

      ans += ((1LL * it.second * (it.second - 1)) / 2);
    }
    cout << ans;
  }
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#ifdef KACTL
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#endif
#define all(x) begin(x), end(x)

using ll = int64_t;
using ld = long double;
using pii = pair<int32_t, int32_t>;
using vi = vector<int32_t>;

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

  string s;
  cin >> s;
  vi m(2019, 0);
  m[0] = 1;

  // p[i] 10^-i = p[j] * 10^-j
  // p[i] * 202^i = p[j] * 202^j

  ll ans = 0;
  int cur = 0;
  int b = 1;
  for (char c : s) {
    cur = (10 * cur + c - '0') % 2019;
    ans += m[(b * cur) % 2019]++;

    b = 202 * b % 2019;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef KACTL
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#endif
#define all(x) begin(x), end(x)

using ll = int64_t;
using ld = long double;
using pii = pair<int32_t, int32_t>;
using vi = vector<int32_t>;

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

  string s;
  cin >> s;
  map<int, int> m;
  m[0] = 1;

  ll ans = 0;
  int cur = 0;
  for (char c : s) {
    map<int, int> nm;
    for (auto &p : m) {
      nm[10 * p.first % 2019] += p.second;
    }
    swap(m, nm);

    cur = (10 * cur + c - '0') % 2019;
    ans += m[cur];
    ++m[cur];
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int main() {
  int n;
  string s;
  cin >> s;
  n = s.size();
  vector<int> num(n);
  vector<int> v2019(n, 1);
  // for(int i=1;i<n;i++)

  rep(i, n) num.at(i) = (int)s.at(i) - (int)'0';

  vector<int> dp(n, 0);
  reverse(all(num));
  dp.at(0) = num.at(0);
  in ans = 0;
  rep(i, n) {
    if (i == 0)
      continue;
    v2019.at(i) = (v2019.at(i - 1) * 10) % 2019;
    dp.at(i) = (dp.at(i - 1) + num.at(i) * v2019.at(i)) % 2019;
    if (dp.at(i) == 0)
      ans++;
  }

  vector<int> h(2019);
  rep(i, 2019) {
    rep(j, n) {
      if (dp.at(j) == i)
        h.at(i) = h.at(i) + 1;
    }
  }
  rep(i, 2019) {
    if (h.at(i) > 1)
      ans += (h.at(i) * (h.at(i) - 1)) / 2;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
#define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x))
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const int inf = 1e9 + 7;
const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};

int main() {
  int n;
  string s;
  cin >> s;
  n = s.size();
  vector<int> num(n);
  vector<int> v2019(n, 1);
  for (int i = 1; i < n; i++)
    v2019.at(i) = (v2019.at(i - 1) * 10) % 2019;

  rep(i, n) num.at(i) = (int)s.at(i) - (int)'0';

  vector<int> dp(n, 0);
  reverse(all(num));
  dp.at(0) = num.at(0);

  rep(i, n) {
    if (i == 0)
      continue;
    dp.at(i) = (dp.at(i - 1) + num.at(i) * v2019.at(i)) % 2019;
  }
  in ans = 0;
  for (int i = 0; i < n - 3; i++) {
    for (int j = i + 3; j < n; j++) {
      int b;
      if (i != 0)
        b = dp.at(i - 1);
      else
        b = 0;
      if ((dp.at(j) - b) == 0) {
        ans++;
        // cout<<"(i,j) = "<<"( "<<i<<" , "<<j<<" )"<<endl;
      }
    }
  }
  printf("%ld\\n", ans);
  /*rep(i,n)
    cout<<num.at(i)<<" ";
  cout<<endl;
  /*rep(i,n)
    cout<<v2019.at(i)<<endl;*/
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) begin(a), end(a)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1e+9;
const ll mod = 1000000007;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int cans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

int fac(int num) {
  if (num == 1)
    return 1;
  else
    return num * fac(num - 1);
}

int sum[210000];
vector<int> v(210000);
ll ans = 0;
string s;
int pw[210000];

map<ll, ll> mp;

int main() {
  pw[0] = 1;
  cin >> s;
  reverse(all(s));
  int N = s.size();
  rep(i, 210000) { pw[i + 1] = pw[i] * 10 % 2019; }
  rep(i, N) {
    int num = s[i] - '0';
    sum[i + 1] = sum[i] + pw[i] * num;
    sum[i + 1] %= 2019;
  }

  rep(i, N + 1) v[sum[i]]++;
  rep(i, 210000) {
    if (v[i])
      ans += v[i] * (v[i] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) begin(a), end(a)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1e+9;
const ll mod = 1000000007;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int cans(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

int fac(int num) {
  if (num == 1)
    return 1;
  else
    return num * fac(num - 1);
}

int sum[210000];
ll ans = 0;
string s;

bool isok(int l, int r) {
  int now = 0;
  for (int i = l; i <= r; i++) {
    now *= 10;
    now += s[i] - '0';
    now %= 2019;
  }
  if (now % 2019 == 0)
    return true;
  return false;
}

int main() {
  cin >> s;
  int N = s.size();
  rep(i, N) { sum[i + 1] = (s[i] + sum[i]) % 3; }
  int now = 0;
  rep(i, N) {
    for (int j = i + 3; j < N; j++) {
      if (sum[j + 1] - sum[i] != 0)
        continue;
      int l = i;
      int r = j;
      if (isok(l, r)) {
        ans++;
        l = r;
        r = l + 4;
      }
    }
  }
  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;

#define ALL(A) A.begin(), A.end()

ll tri(ll A) {
  if (A == 0)
    return 0;
  else if (A == 1)
    return 0;
  else
    return A * (A - 1) / 2;
}

ll njou10(ll A) {
  if (A == 0)
    return 1;
  else if (A == 1)
    return 10;
  else if (A % 2 == 0)
    return (njou10(A / 2) % 2019) * (njou10(A / 2) % 2019) % 2019;
  else
    return (njou10((A + 1) / 2) % 2019) * (njou10((A - 1) / 2) % 2019) % 2019;
}

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

  ll ssize = s.size();

  vector<ll> last(ssize);
  last.at(0) = s.at(ssize - 1) - '0';
  ll beki10 = 10;
  for (int i = 1; i < ssize; i++) {
    last.at(i) = (last.at(i - 1) + (s.at(ssize - i - 1) - '0') * beki10) % 2019;
    beki10 *= 10;
    beki10 %= 2019;
  }

  map<int, int> M;
  M[0]++;
  for (int i = 0; i < ssize; i++) {
    M[last.at(i)]++;
  }

  ll answer = 0;
  for (auto x : M) {
    answer += tri(x.second);
  }

  cout << answer << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define ALL(A) A.begin(), A.end()

ll tri(ll A) {
  if (A == 0)
    return 0;
  else if (A == 1)
    return 0;
  else
    return A * (A - 1) / 2;
}

ll njou10(ll A) {
  if (A == 0)
    return 1;
  else if (A == 1)
    return 10;
  else if (A % 2 == 0)
    return (njou10(A / 2) % 2019) * (njou10(A / 2) % 2019) % 2019;
  else
    return (njou10((A + 1) / 2) % 2019) * (njou10((A - 1) / 2) % 2019) % 2019;
}

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

  ll ssize = s.size();

  vector<ll> last(ssize);
  last.at(0) = s.at(ssize - 1) - '0';
  for (int i = 1; i < ssize; i++) {
    last.at(i) =
        (last.at(i - 1) + (s.at(ssize - i - 1) - '0') * njou10(i)) % 2019;
  }

  map<int, int> M;
  M[0]++;
  for (auto x : last) {
    M[x]++;
  }

  ll answer = 0;
  for (auto x : M) {
    answer += tri(x.second);
  }

  cout << answer << endl;

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

#include <bits/stdc++.h>
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define pii pair<int, int>
#define REP(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define RREP(i, a) for (int(i) = a - 1; (i) >= (0); (i)--)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
typedef long long ll;
using namespace std;

const ll mod = 2019;
ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) { // 全てのbitが捨てられるまで。
    if (n & 1)
      ans = (ans * x) % mod; // 1番右のbitが1のとき。
    x = (x * x) % mod;
    n = n >> 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return ans;
}

int main() {
  string S;
  cin >> S;
  ll n = S.size();
  vll s(n);
  for (int i = 0; i < n; i++)
    s[i] = S[i] - '0';
  ll ans = 0;
  vll t(n, 0);
  t[n - 1] = s[n - 1];
  map<ll, ll> mp;
  mp[0]++;
  for (int i = n - 2; i >= 0; i--) {
    t[i] = t[i + 1] + s[i] * mpow(10, n - 1 - i);
    t[i] %= 2019;
  }
  REP(i, n) mp[t[i]]++;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    ans += itr->second * (itr->second - 1) / 2;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define pii pair<int, int>
#define REP(i, a) for (int(i) = 0; (i) < (a); (i)++)
#define RREP(i, a) for (int(i) = a - 1; (i) >= (0); (i)--)
#define REP2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define m0(x) memset(x, 0, sizeof(x))
typedef long long ll;
using namespace std;

const ll mod = 2019;
ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) { // 全てのbitが捨てられるまで。
    if (n & 1)
      ans = (ans * x) % mod; // 1番右のbitが1のとき。
    x = (x * x) % mod;
    n = n >> 1; // bit全体を右に1つシフトして一番右を捨てる。
  }
  return ans;
}

int main() {
  string S;
  cin >> S;
  ll n = S.size();
  vll s(n);
  for (int i = 0; i < n; i++)
    s[i] = S[i] - '0';
  ll ans = 0;
  vll t(n, 0);
  t[n - 1] = 0;
  t[n - 2] = s[n - 1];
  for (int i = n - 3; i >= 0; i--) {
    t[i] = (t[i + 1] + s[i + 1] * mpow(10, n - 2 - i)) % 2019;
  }
  map<ll, ll> mp;
  for (int i = 0; i < n; ++i)
    mp[t[i]]++;
  ll tlast = t[0] + s[0] * mpow(10, n - 1);
  tlast %= 2019;
  mp[tlast]++;
  for (auto x = mp.begin(); x != mp.end(); x++) {
    ans += x->second * (x->second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007
#define iP pair<int, int>
#define gcd(a, b) __gcd(a, b)
#define pb push_back
#define inf 1e15
#define mod1 998244353
#define pi 3.141592653589793238
int power(int x, int y) {
  int res = 1;
  x = x % 2019;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % 2019;
    x = (x * x) % 2019;
    y /= 2;
  }
  return res;
}
void solve(int tc) {
  // int m=2019;
  string str;
  cin >> str;
  int n = str.size();
  map<int, int> prev;
  prev[0] = 1;
  vector<int> v(str.size() + 1, 0);
  // int k=0;
  for (int i = n - 1; i >= 0; i--) {
    int z = str[i] - '0';
    v[i] = (v[i + 1] + power(10, n - i - 1) * z) % 2019;
    // cout<<v[i]<<" ";
    prev[v[i]]++;
  }
  int ans = 0;
  for (auto it : prev) {
    int ctr = it.second;
    // cout<<ctr<<" ";
    ans += (ctr * (ctr - 1)) / 2;
  }
  cout << ans;
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cerr.tie(NULL);
  cout.precision(20);
  int t = 1, ctr = 0;
  // cin>>t;
  while (t--) {
    solve(ctr + 1);
    ctr++;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define mod 1000000007
#define iP pair<int, int>
#define gcd(a, b) __gcd(a, b)
#define pb push_back
#define inf 1e15
#define mod1 998244353
#define pi 3.141592653589793238
void solve(int tc) {
  string str;
  cin >> str;
  int n = str.size(), ans = 0;
  set<int> arr[n];
  for (int i = 0; i < n; i++) {
    int r = 0, pr = 0;
    for (int j = i; j < n; j++) {
      r = (r * 10 + (str[j] - '0')) % 2019;
      if (arr[j].count(r))
        break;
      arr[j].insert(r);
      if (r == 0)
        pr++;
    }
    ans += (pr * (pr + 1)) / 2;
  }
  cout << ans;
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cerr.tie(NULL);
  cout.precision(20);
  int t = 1, ctr = 0;
  // cin>>t;
  while (t--) {
    solve(ctr + 1);
    ctr++;
  }
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

string S;

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

int main() {
  cin >> S;
  cout << solve() << endl;
}

Error: -11

Fixed submission:

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

long long N;
string S;

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

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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

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

  string s;
  cin >> s;

  ll n = s.size();

  reverse(all(s));

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

  ll cnt = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cnt += modpow(10, i, 2019) * (s[i] - '0');
    cnt %= 2019;

    ans += mp[cnt];
    mp[cnt]++;
  }

  cout << ans << ln;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const string ln = "\\n";
constexpr int INF = 1001001001;
constexpr int MOD = 1000000007;

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

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

  string s;
  cin >> s;

  ll n = s.size();

  ll md[210000] = {};
  md[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    md[i] = modpow(10, i);
  }

  ll ans = 0;
  for (int l = 0; l < n - 4; l++) {
    for (int r = l + 4; r <= n; r++) {
      string sub = s.substr(l, r);
      ll sum = 0;
      for (int i = 0; i < sub.size(); i++) {
        ll num = sub[sub.size() - 1 - i] - '0';
        ll t = (num * md[i]) % 2019;
        sum += t;
        sum %= 2019;
      }

      if (sum % 2019 == 0) {
        ans++;
      }
    }
  }

  cout << ans << ln;

  return 0;
}