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

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

const int mod = 2019;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint m) {
    if ((x += m.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint m) {
    if (x += mod - m.x >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint m) {
    (x *= m.x) %= mod;
    return *this;
  }
  mint operator+(const mint m) const { return mint(*this) += m; }
  mint operator-(const mint m) const { return mint(*this) -= m; }
  mint operator*(const mint m) const { return mint(*this) *= m; }
  mint pow(ll n) const {
    if (!n)
      return 1;
    mint m = pow(n >> 1);
    m *= m;
    if (n & 1)
      m *= *this;
    return m;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint m) { return *this *= m.inv(); }
  mint operator/(const mint m) const { return mint(*this) /= m; }
};
istream &operator>>(istream &is, const mint &m) { return is >> m.x; }
ostream &operator<<(ostream &os, const mint &m) { return os << m.x; }

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact.at(0) = 1;
    for (int i = 1; i <= n; i++) {
      fact.at(i) = fact.at(i - 1) * i;
    }
    ifact.at(n) = fact.at(n).inv();
    for (int i = n; i >= 1; i--) {
      ifact.at(i - 1) = ifact.at(i) * i;
    }
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact.at(n) * ifact.at(k) * ifact.at(n - k);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  vector<mint> dp(s.length() + 1);
  dp.at(s.length()) = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    mint num = s.at(i) - '0';
    mint digit(10);
    digit = digit.pow(s.length() - 1 - i);
    num *= digit;
    dp.at(i) = dp.at(i + 1) + num;
  }
  // for(auto num : dp) cout << num << '\\n';
  vector<int> numod(2019, 0);
  for (int i = 0; i <= s.length(); i++) {
    numod.at(dp.at(i).x)++;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    ans += numod.at(i) * (numod.at(i) - 1) / 2;
  }
  cout << ans << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vpi = vector<pii>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpl = vector<pll>;
using vs = vector<string>;

int main() {
  string s;
  cin >> s;
  ll cnt = 0;
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = i + 3; j < s.size(); j++) {
      string n(s, i, i + j);
      ll num = atoi(n.c_str());
      if (num % 2019 == 0) {
        // cout << num << endl;
        // cout << i << ", " << j << endl;
        cnt++;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  string S;
  cin >> S;
  ll length = S.length();
  vector<ll> mod_map(2019, 0);
  ll cur_num = 0;
  ll base_10 = 1;
  for (ll i = 0; i < length; i++) {
    cur_num += (S[length - 1 - i] - '0') * base_10;
    mod_map[cur_num % 2019] += 1;
    base_10 = base_10 * 10 % 2019;
  }
  ll ans = mod_map[0];
  for (auto m : mod_map) {
    if (m > 1)
      ans += m * (m - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  string S;
  cin >> S;
  int ans = 0;
  ll length = S.length();
  for (ll i = 0; i < length - 3; i++) {
    for (ll j = i + 3; j < length; j++) {
      ll num;
      sscanf(S.substr(i, j + 1 - i).c_str(), "%ld", &num);
      if (num % 2019 == 0) {
        ans++;
        j += 3;
      }
    }
  }
  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;
  int n = s.length();
  reverse(s.begin(), s.end());

  vector<int> mod(n + 1);
  int pow10 = 1;
  for (int i = 0; i < n; i++) {
    mod[i + 1] = mod[i] + (s[i] - '0') * pow10;
    mod[i + 1] %= 2019;

    pow10 *= 10;
    pow10 %= 2019;
  }

  map<int, int> pool;
  for (int i = 0; i <= n; i++) {
    if (pool.find(mod[i]) == pool.end())
      pool[mod[i]] = 1;
    else
      pool[mod[i]] += 1;
  }

  long long int ans = 0;
  for (auto p : pool) {
    ans += p.second * (p.second - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  vector<vector<long long int>> dp(n + 1, vector<long long int>(2019));
  for (int i = 0; i <= n; i++)
    dp[i][0] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2019; j++) {
      if (dp[i][j] != 0) {
        // cout << i << " " << j << endl;
        dp[i + 1][(j * 10 + (s[i] - '0')) % 2019] += dp[i][j];
      }
    }
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += dp[i][0] - 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MOD = 2019;

int pow(int x, int n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return x % MOD;
  }
  if (n & 1) {
    int tmp = pow(x, (n - 1) / 2);
    return tmp * tmp % MOD * x % MOD;
  } else {
    int tmp = pow(x, n / 2);
    return tmp * tmp % MOD;
  }
}

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

  int n = s.size();
  vector<int> t(n + 1, 0);
  for (int i = n - 1; i >= 0; i--) {
    t.at(i) = (t.at(i + 1) + pow(10, n - 1 - i) * (s.at(i) - '0') % MOD) % MOD;
  }

  vector<int> m(2019);
  for (int i = 0; i < n + 1; i++) {
    m.at(t.at(i)) += 1;
  }
  int res = 0;
  for (int i = 0; i < 2019; i++) {
    int mi = m.at(i);
    res += mi * (mi - 1) / 2;
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

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

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

  int64_t res = 0;
  for (int i = 0; i < s.size(); i++) {
    int num = 0;
    for (int j = i; j < s.size(); j++) {
      num = ((num * 10) % 2019 + (s.at(j) - '0')) % 2019;
      if (num == 0) {
        res++;
      }
    }
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int dp[2][2019];

int main() {
  string s;
  cin >> s;
  // s.insert(s.begin(),'0');
  int n = s.size();

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int x = s[i] - '0';
    dp[1][x]++;
    for (int j = 0; j < 2019; j++) {
      dp[1][(j * 10 + x) % 2019] += dp[0][j];
    }
    ans += dp[1][0];
    for (int j = 0; j < 2019; j++) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }

  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

int dp[200001][2019];

int main() {
  string s;
  cin >> s;
  // s.insert(s.begin(),'0');
  int n = s.size();

  for (int i = 0; i < n; ++i) {
    int x = s[i] - '0';
    dp[i + 1][x]++;
    for (int j = 0; j < 2019; j++) {
      dp[i + 1][(j * 10 + x) % 2019] += dp[i][j];
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += dp[i][0];
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int i = ini; i < n; i++)
#define _rep(i, ini, n) for (int i = ini; i >= n; i--)
#define ToEnd(a) a.begin(), a.end()
uint64_t MOD = 1000000007;

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

  vector<vector<int64_t>> Dp(2, vector<int64_t>(2019, 0));
  int64_t ans = 0;
  rep(i, 0, S.size()) {
    int n = S.at(i) - '0';
    rep(j, 0, 2019) Dp.at(i & 1).at(j) = 0;
    rep(j, 0, 2019) Dp.at(i & 1).at((j * 10 + n) % 2019) +=
        Dp.at(!(i & 1)).at(j);
    Dp.at(i & 1).at(n)++;
    ans += Dp.at(i & 1).at(0);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, ini, n) for (int64_t i = ini; i >= n; i--)

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

  uint64_t ans = 0;
  vector<vector<int>> dp(s.size(), vector<int>(s.size() + 1, 0));

  for (int i = s.size() - 1; i >= 0; i--) {
    for (int j = i + 1; j < s.size() + 1; j++) {
      if (j == i + 1)
        dp.at(i).at(i + 1) = s.at(i) - '0';
      else {
        int num = dp.at(i).at(j - 1) * 10 + dp.at(j - 1).at(j);
        dp.at(i).at(j) = num % 2019;
        if (dp.at(i).at(j) == 0)
          ans++;
      }
    }
  }

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

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

typedef long long ll;
typedef pair<int, int> pii;

int MOD = 2019;
int mPow(int a, int b) {
  int t = 1;
  while (b) {
    if (b & 1)
      t = (t * a) % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return t % MOD;
}

int main() {
  cout << setprecision(10);
  string S;
  cin >> S;
  vector<int> v(S.size());
  reverse(S.begin(), S.end());

  int mod = 0;
  for (int i = 0; i < S.size(); i++) {
    int num = S[i] - '0';
    // num * pow(10, i) を2019で割ったあまりがほしい
    num = num * mPow(10, i);
    num %= 2019;
    mod += num;
    v.at(i) = mod % 2019;
  }
  ll ans = 0;
  map<int, int> m;
  for (auto x : v) {
    if (x == 0)
      ans++;
    if (m.count(x) == 0) {
      m[x] = 1;
    } else {
      m[x]++;
    }
  }

  for (auto x : m) {
    if (x.second >= 2 || x.first == 0) {
      ans += x.second * (x.second - 1) / 2;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;

long long MOD = 1000000000 + 7;

int main() {
  cout << setprecision(10);
  string S;
  cin >> S;
  vector<int> v(S.size());
  reverse(S.begin(), S.end());

  int mod = 0;
  for (int i = 0; i < S.size(); i++) {
    int num = S[i] - '0';
    // num * pow(10, i-1) を2019で割ったあまりがほしい
    for (int j = 0; j < i; j++) {
      num = num * 10;
      num %= 2019;
    }
    mod += num;
    v.at(i) = mod % 2019;
  }
  ll ans = 0;
  map<int, int> m;
  for (auto x : v) {
    if (x == 0)
      ans++;
    if (m.count(x) == 0) {
      m[x] = 1;
    } else {
      m[x]++;
    }
  }

  for (auto x : m) {
    if (x.second >= 2 || x.first == 0) {
      ans += x.second * (x.second - 1) / 2;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 << 60;
#define mod 2019

vector<ll> cnt(2019);

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

  cnt[0] = 1;
  ll p = 1;
  ll now = 0;
  for (int i = N - 1; i >= 0; i--) {
    now = (now + (S[i] - '0') * p) % mod;
    cnt[now]++;
    p = (p * 10) % mod;
  }

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

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 << 60;

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

  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      ll x = stoll(S.substr(i, j - i + 1));
      if (x % 2019 == 0) {
        cnt++;
        i = j;
      }
    }
  }

  cout << cnt << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

int main() {
  string S;
  cin >> S;
  ll Sz = S.size();
  vector<ll> T(Sz + 1, 0), U(Sz + 1, 0);
  ll Sm = 0;
  ll Bi = 0;
  vector<vector<ll>> TT(3, vector<ll>(0));
  vector<vector<ll>> UU(673, vector<ll>(0));
  TT.at(0).push_back(Sz);
  UU.at(0).push_back(Sz);
  ll Ten = 1;
  for (ll i = Sz - 1; i >= 0; i--) {
    Sm += S.at(i) - '0';
    Bi += (S.at(i) - '0') * Ten;
    Sm %= 3;
    Bi %= 673;
    // cout<<i<<"Sm="<<Sm<<"Bi="<<Bi<<endl;
    T.at(i) = Sm;
    U.at(i) = Bi;
    TT.at(Sm).push_back((ll)i);
    UU.at(Bi).push_back((ll)i);
    Ten *= 10;
    Ten %= 673;
  }
  ll Ans = 0;
  for (ll i = 0; i <= 672; i++) {
    ll A = UU.at(i).size();
    if (A > 1) {
      for (ll j = 0; j < A; j++) {
        for (ll k = j + 1; k < A; k++) {
          // cout<<"!";
          if (T.at(UU.at(i).at(j)) == T.at(UU.at(i).at(k))) {
            // cout<<UU.at(i).at(j)<<" "<<UU.at(i).at(k)<<endl;
            Ans++;
          }
        }
      }
    }
  }
  cout << Ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

int main() {
  string S;
  cin >> S;
  ll Sz = S.size();
  vector<ll> T(Sz + 1, 0), U(Sz + 1, 0);
  ll Sm = 0;
  ll Bi = 0;
  vector<vector<ll>> TT(3, vector<ll>(0));
  vector<vector<ll>> UU(673, vector<ll>(0));
  TT.at(0).push_back(Sz);
  UU.at(0).push_back(Sz);
  ll Ten = 1;
  for (ll i = Sz - 1; i >= 0; i--) {
    Sm += S.at(i) - '0';
    Bi += (S.at(i) - '0') * Ten;
    Sm %= 3;
    Bi %= 673;
    // cout<<i<<"Sm="<<Sm<<"Bi="<<Bi<<endl;
    T.at(i) = Sm;
    U.at(i) = Bi;
    TT.at(Sm).push_back((ll)i);
    UU.at(Bi).push_back((ll)i);
    Ten *= 10;
  }
  ll Ans = 0;
  for (ll i = 0; i <= 672; i++) {
    ll A = UU.at(i).size();
    // cout<<A<<" "<<B<<endl;
    for (ll j = 0; j < A; j++) {
      for (ll k = j + 1; k < A; k++) {
        // cout<<"!";
        if (T.at(UU.at(i).at(j)) == T.at(UU.at(i).at(k))) {
          // cout<<UU.at(i).at(j)<<" "<<UU.at(i).at(k)<<endl;
          Ans++;
        }
      }
    }
  }
  /*ll Zero=UU.at(0).size();
      for(int i=0; i<Zero; i++){
      for(int j=i+1; j<Zero; j++){
              if(T.at(UU.at(0).at(i)==T.at(UU.at(0).at(j)))){
              Ans++;
          }
      }
  }*/
  // cout<<Zero<<endl;
  cout << Ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;
using ull = unsigned long long;

#define MOD 1000000007

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;

int main(void) {
  string S;
  ll count = 0;
  ll ten = 1;
  ll current = 0;
  map<ll, ll> m;
  cin >> S;
  reverse(S.begin(), S.end());
  m[0] = 1;
  for (auto &c : S) {
    current = (current + (c - '0') * ten) % 2019;
    ten *= 10;
    ten %= 2019;
    m[current]++;
  }
  for (auto &i : m) {
    count += i.second * (i.second - 1) / 2;
  }
  cout << count << endl;
  return 0;
}


Error: 0

Fixed submission:

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

using ll = long long;
using ull = unsigned long long;

#define MOD 1000000007

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;

int main(void) {
  string S;
  ll count = 0;
  cin >> S;
  for (ll i = 0; i < S.size() - 3; i++) {
    ll sum = (S[i] - '0') + (S[i + 1] - '0') + (S[i + 2] - '0');
    for (ll j = i + 3; j < S.size(); j++) {
      sum += (S[j] - '0');
      if (sum % 3 == 0) {
        string s(S.begin() + i, S.begin() + j + 1);
        Bint a(s);
        if (a % 2019 == 0)
          count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

int main() {
  using namespace std;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  int pow_mod = 1;
  int sum = 0, ans = 0, cnt;
  vector<int> mod;
  vector<int> modLast;
  cin >> s;
  // 1文字ずつの余りをチェックしていく
  for (int i = (int)s.length() - 1; i >= 0; i--) {
    // cout << s[i] << endl;
    mod.push_back((pow_mod * (int)(s[i] - '0')) % 2019);
    // cout << (int)(s[i]-'0') << " " << pow_mod << " " <<
    // (pow_mod*(int)(s[i]-'0'))%2019 << endl;
    pow_mod = (pow_mod * 10) % 2019;
  }
  // まず、最後の文字を固定した場合の余りを全て求める
  modLast.push_back(0);
  for (int i = 1; i <= (int)s.length(); i++) {
    sum = (sum + mod[i - 1]) % 2019;
    modLast.push_back(sum);
  }
  // cout << count(modLast.begin(), modLast.end(), 0) << endl;
  for (int i = 0; i < 2019; i++) {
    cnt = (int)count(modLast.begin(), modLast.end(), i);
    if (cnt >= 2) {
      ans += (cnt * (cnt - 1)) / 2;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>

int main() {
  using namespace std;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s;
  int pow_mod = 1;
  int sum = 0, ans = 0;
  vector<int> mod;
  vector<int> modLast;
  cin >> s;
  // 1文字ずつの余りをチェックしていく
  for (int i = (int)s.length() - 1; i >= 0; i--) {
    // cout << s[i] << endl;
    mod.push_back((pow_mod * (int)(s[i] - '0')) % 2019);
    // cout << (int)(s[i]-'0') << " " << pow_mod << " " <<
    // (pow_mod*(int)(s[i]-'0'))%2019 << endl;
    pow_mod = (pow_mod * 10) % 2019;
  }
  // まず、最後の文字を固定した場合の余りを全て求める
  modLast.push_back(0);
  for (int i = 1; i <= (int)s.length(); i++) {
    sum = (sum + mod[i - 1]) % 2019;
    modLast.push_back(sum);
  }
  for (int i = 1; i <= (int)s.length(); i++) {
    for (int j = i; j <= (int)s.length(); j++) {
      if (modLast[j] == modLast[i - 1])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iomanip>
#include <iostream>
#include <string>
typedef long long ll;
using namespace std;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define repi(i, n, init) for (ll i = init; i < (n); i++)

int main() {
  string s;
  cin >> s;
  int sum_of_digits = 0, ans = 0;
  int cnt[2019] = {0};
  cnt[0] = 1;
  int n = s.size(), d = 1;
  repi(i, n, 0) {
    sum_of_digits += (s.at(n - i - 1) - '0') * d;
    d *= 10;
    d %= 2019;
    sum_of_digits %= 2019;
    cnt[sum_of_digits]++;
  }
  repi(i, 2019, 0) { ans += cnt[i] * (cnt[i] - 1) / 2; }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
using Bint = mp::cpp_int;

typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

int main() {
  string s;
  cin >> s;
  ll ans = 0, n = s.size();
  Bint num = 0, tens = 1;
  repi(i, n - 4, 0) {
    num = 0;
    repi(k, 4, 0) {
      tens = 1;
      repi(l, 3, k) { tens *= 10; }
      num += (s.at(i + k) - '0') * tens;
      if (num % Bint(2019) == 0) {
        ans++;
      }
    }
    repi(j, n - i - 4, 0) {

      num *= 10;
      num += s.at(i + 4 + j) - '0';
      if (num % Bint(2019) == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MOD 2019

int m[2020];

int choose(int n) {
  if (n < 2)
    return 0;
  return n * (n - 1) / 2;
}

int main() {
  string s;
  cin >> s;
  memset(m, 0, sizeof(m));
  int n = s.size();
  vector<int> arr(n + 1);
  vector<int> p(n);
  p[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    p[i] = p[i + 1] * 10 % MOD;
  arr[n] = 0;
  int c = 0;
  for (int i = n - 1; i >= 0; i--) {
    arr[i] = ((s[i] - '0') * p[i] + arr[i + 1]) % MOD;
    c = c - choose(m[arr[i]]) + choose(++m[arr[i]]);
  }
  c = c - choose(m[0]) + choose(++m[0]);
  cout << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MOD 2019

int main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> arr(n + 1);
  vector<int> p(n);
  p[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    p[i] = p[i + 1] * 10 % MOD;
  arr[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    arr[i] = ((s[i] - '0') * p[i] + arr[i + 1]) % MOD;
  }

  // for (int i : arr) cout << i << " ";
  // cout << endl;

  int c = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      // if (arr[i] == arr[j]) cout << i << " " << j << endl;
      c += (arr[i] == arr[j]) ? 1 : 0;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, N, ansPre[2019] = {0}, ans[2019] = {0}, res = 0;
  string S;
  cin >> S;
  N = S.length();
  for (int i = 0; i < N; i++) {
    n = S[i] - '0';
    fill(ans, ans + 2019, 0);
    ans[n] += 1;
    for (int j = 0; j <= 2018; j++)
      ans[(j * 10 + n) % 2019] += ansPre[j];
    for (int j = 0; j <= 2018; j++)
      ansPre[j] = ans[j];
    res += ans[0];
  }
  cout << res;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, N, ans[200000][2019], res = 0;
  string S;
  cin >> S;
  N = S.length();
  for (int i = 0; i < N; i++) {
    n = S[i] - '0';
    ans[i][n] += 1;
    if (i == 0)
      continue;
    for (int j = 0; j <= 2018; j++) {
      ans[i][(j * 10 + n) % 2019] += ans[i - 1][j];
    }
    res += ans[i][0];
  }
  cout << res;
  return 0;
}