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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long LL;
const int mod = 2019;

LL modinv(LL x) {
  LL m = mod, u = 1, v = 0;
  while (m) {
    LL t = x / m;
    x -= t * m;
    swap(x, m);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

LL modpow(LL x, LL n) {
  LL r = 1;
  while (n) {
    if (n & 1)
      r = r * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return r;
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  vector<int> dp(N + 1);
  map<int, LL> m;
  m[0]++;
  rep(i, N) {
    int k = S[i] - '0';
    dp[i + 1] = (dp[i] * 10 + k) % mod;
    int num = dp[i + 1] * modinv(modpow(10, i)) % mod;
    m[num]++;
  }
  LL ans = 0;
  for (auto itr : m) {
    LL p = itr.second;
    ans += p * (p - 1) / 2;
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long LL;
typedef pair<int, int> P;

int main() {
  string S;
  cin >> S;
  int dp[200010][675] = {0};
  int c = S[0] - '0';
  dp[0][c] = 1;
  int N = S.size(), sum = c, ans = 0;
  for (int i = 1; i < N; i++) {
    int num = S[i] - '0';
    sum += num;
    sum %= 3;
    rep(j, 673) {
      int k = (j * 10 + num) % 673;
      dp[i][k] = dp[i - 1][j];
    }
    dp[i][num]++;
    ans += dp[i][0];
  }
  cout << ans << endl;

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

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

int main() {
  auto &&as_int = [](char c) { return (int)c - '0'; };
  string s;
  cin >> s;
  size_t n = s.length();
  int cnt = 0;
  vector<int> t(n + 1, 0);
  vector<int> ms(2019, 0);
  {

    t[n] = 0;
    int factor = 1;

    ms[0]++;
    for (int i = n - 1; i >= 0; i--) {
      auto &&tmp = t[i] = (t[i + 1] + as_int(s[i]) * factor) % 2019;
      factor = factor * 10 % 2019;
      ms[tmp]++;
    }
  }
  for (auto &i : ms) {
    cnt += i * (i - 1) / 2;
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  auto &&as_int = [](char c) { return (int)c - '0'; };
  string s;
  cin >> s;
  size_t n = s.length();
  int cnt = 0;
  vector<int> t(n + 1, 0);
  {

    t[n] = 0;
    int factor = 1;

    for (int i = n - 1; i >= 0; i--) {
      auto &&tmp = t[i] = (t[i + 1] + as_int(s[i]) * factor % 2019) % 2019;
      factor = factor * 10 % 2019;
      for (int j = n - 1; j >= i; j--) {
        if (tmp == t[j + 1]) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MOD = 2019;

int main() {
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  long long digit = 1, len = S.size();
  vector<long long> T(len + 1); // 後ろから見ていき、MOD取って収納.
  map<long long, int> ma;       // 数数えのために持ってきたよ.
  T.at(0) = 0;
  ma[0]++;
  for (int i = 1; i <= len; i++) {
    long long tmp = (long long)(S.at(i - 1) - '0') * digit;
    tmp %= MOD;
    T.at(i) = (tmp + T.at(i - 1)) % MOD;
    ma[T.at(i)]++;
    digit *= 10;
    digit %= MOD;
  }

  long long ans = 0;
  for (auto x : ma) {
    long long tmp = x.second;
    ans += tmp * (tmp - 1) / 2;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

const int MOD = 2019;

int main() {
  string S;
  cin >> S;
  map<int, int> ma;
  int N, cnt = 0, len = S.size();
  for (int i = 0; i < len - 3; i++) {
    int plus = 1;
    if (ma[i] != 0) {
      continue;
    }
    N = stoi(S.substr(i, 4));
    if (N % MOD == 0) {
      cnt += plus;
      ma[i + 4]++;
      plus++;
    }
    for (int j = i + 4; j < len; j++) {
      N = ((N % MOD) * 10 + (int)S.at(j) - '0') % MOD;
      if (N == 0) {
        cnt += plus;
        plus++;
        if (j != N - 1) {
          ma[j + 1]++;
        }
      }
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  string s;
  cin >> s;
  mint n = 0;
  V mods(2019, 0);
  mint ex = 1;
  mods[0] += 1;
  rep(i, s.size()) {
    n += (mint)(s[s.size() - i - 1] - '0') * ex;
    mods[n.x] += 1;
    ex *= 10;
  }
  ll ans = 0;
  rep(i, 2019) { ans += mods[i] * (mods[i] - 1) / 2; }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  string s;
  cin >> s;
  vector<vector<vector<vector<ll>>>> dp(3, VVV(s.size() + 1, VV(673, V(2, 0))));
  rep(i, s.size()) {
    ll num = s[i] - '0';
    dp[num % 3][i + 1][num][1] += 1;
    rep(j, 673) {
      rep(k, 3) {
        dp[(k + num) % 3][i + 1][(j * 10 + num) % 673][1] += dp[k][i][j][1];
        dp[k][i + 1][j][0] += dp[k][i][j][1] + dp[k][i][j][0];
      }
    }
  }
  cout << dp[0][s.size()][0][1] + dp[0][s.size()][0][0] << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string str;
  cin >> str;
  int n = str.size();
  reverse(str.begin(), str.end());
  vector<int> cnts(2019);
  cnts[0]++;
  int mod_digits = 1;
  int carry = 0;
  rep(i, n) {
    char c = str[i];
    int x = atoi(&c);
    carry = (carry + x * mod_digits) % 2019;
    mod_digits = (mod_digits * 10) % 2019;
    cnts[carry]++;
  }

  ll cnt = 0;
  rep(i, 2019) { cnt += cnts[i] * (cnts[i] - 1) / 2; }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int strmod(string str) {
  int carry = 0;
  rep(i, str.size()) {
    char c = str[i];
    int x = carry * 10 + atoi(&c);
    carry = x % 2019;
  }
  return carry;
}

int main() {
  string str;
  cin >> str;
  int n = str.size();
  reverse(str.begin(), str.end());
  vector<int> cnts(2019);
  ll val = 0;
  cnts[0]++;
  string curstr = "";
  rep(i, n) {
    char c = str[i];
    curstr = c + curstr;
    // cout << curstr << endl;
    cnts[strmod(curstr)]++;
  }

  ll cnt = 0;
  rep(i, 2019) { cnt += cnts[i] * (cnts[i] - 1) / 2; }

  cout << cnt << endl;

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

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

int main() {
  string s;
  cin >> s;
  ll rem, i, n = s.size(), k = 2019, val, ans = 0, val1 = 1, c;
  unordered_map<ll, ll> m;
  rem = 0;

  for (i = n - 1; i >= 0; i--) {
    val = val1 % k;
    c = s[i] - '0';
    rem = (rem + c * val) % k;
    m[rem]++;
    val1 = (val1 % k * 10) % k;
  }

  m[0]++;

  for (auto x : m) {
    ans += (x.second * (x.second - 1)) / 2;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

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

int main() {
  ll i, j, rem, ans = 0, k = 2019, n;
  string str;
  cin >> str;
  n = str.size();
  ll visit[n];
  memset(visit, -1, sizeof(visit));

  for (i = 0; i < n; i++) {
    rem = 0;
    ll val = 0;
    for (j = i; j < n; j++) {
      rem = (rem * 10 + (str[j] - '0')) % k;
      if (visit[j] == rem) {
        break;
      }

      visit[j] = rem;

      if (rem == 0) {
        val++;
      }
    }

    ans = ans + val * (val + 1) / 2;
  }

  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;

  int sz = s.size(), t = 0, ten = 1;
  map<int, int> mp;
  mp[0] = 1;
  for (int i = 1; i <= sz; ++i) {
    int a = stoi(s.substr(sz - i, 1));
    a = a * ten % 2019;
    t = (t + a) % 2019;
    ++mp[t];
    ten = ten * 10 % 2019;
  }

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

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;

  int sz = s.size();
  map<int, int> mp;
  mp[0] = 1;
  for (long long i = 1; i <= sz; ++i) {
    long long t = stoll(s.substr(sz - i, i));
    ++mp[t % 2019];
  }

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

  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> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;

int main() {
  string s;
  cin >> s;
  int ans = 0;
  int c[200000];
  rep(i, s.size()) {
    c[i] = s.at(i) - '0';
    if (c[i] == 0)
      ans++;
  }
  int a = s.size();
  int dp[2][2019];
  dp[0][c[0]]++;

  for (int i = 1; i < a; i++) {
    if (c[i] == 0) {
      for (int j = 0; j < 2019; j++) {
        dp[1][(10 * j + c[i]) % 2019] = dp[0][j];
      }
      for (int j = 0; j < 2019; j++) {
        dp[0][j] = dp[1][j];
      }
      continue;
    } else {
      for (int j = 0; j < 2019; j++) {
        dp[1][(10 * j + c[i]) % 2019] =
            (10 * j + c[i]) % 2019 == c[i] ? dp[0][j] + 1 : dp[0][j];
      }
      for (int j = 0; j < 2019; j++) {
        dp[0][j] = dp[1][j];
      }
    }
    ans += dp[0][0];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589;

int main() {
  string s;
  cin >> s;
  int c[200000];
  rep(i, s.size()) { c[i] = s.at(i) - '0'; }
  int ans = 0;
  int a = s.size();
  rep(i, a) {
    int r = c[i];
    if (r % 2019 == 0)
      ans++;
    for (int j = i + 1; j < a; j++) {
      r = (10 * r + c[j]) % 2019;
      if (r % 2019 == 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;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
ll mod = 2019;

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

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> dp(2019, 0);
  vector<ll> cum(n + 1, 0);
  rep(i, n) {
    cum[i] = (s[i] - '0') * modpow(10, n - 1 - i, mod);
    if (i > 0)
      cum[i] = (cum[i] + cum[i - 1]) % mod;
  }
  map<ll, ll> m;
  ll res = 0;
  m[0] = 1;
  rep(i, n) {
    res += m[cum[i]];
    m[cum[i]]++;
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  string s;
  cin >> s;
  vector<ll> dp(2019, 0);
  ll n = s.size();
  map<ll, ll> m;
  rep(i, n) {
    ll num = s[i] - '0';
    dp[num]++;
    map<ll, ll> tmp;
    tmp[num]++;
    rep(j, 2019) {
      dp[(j * 10 + num) % 2019] += m[j];
      tmp[(j * 10 + num) % 2019] += m[j];
    }
    m = tmp;
  }
  cout << dp[0] << 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 ans = 0;
  int n = S.size();

  // dp[i]:右端から[i+1]桁までの数mod2019,ten[i]:10^(i+1)mod2019
  vector<int> dp(n, 0), m(2019, 0), ten(n, 0);

  // 初期化
  dp[0] = S[n - 1] - '0';
  ten[0] = 10;
  m[dp[0]]++;
  m[0]++;

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

  for (int i = 1; i < n; i++) {
    dp[i] = (dp[i - 1] + ten[i - 1] * (S[n - i - 1] - '0')) % 2019;
    m[dp[i]]++;
  }

  for (int i = 0; i < 2019; i++) {
    ans += m[i] * (m[i] - 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 ans = 0;

  // dp[開始位置][桁数-1]
  vector<vector<int>> dp(S.size(), vector<int>(S.size()));

  // 初期化
  for (int i = 0; i < S.size(); i++) {
    dp[i][0] = (S[i] - '0') % 2019;
  }

  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; j < S.size() - i; j++) {
      dp[i][j] = (dp[i][j - 1] * 10 + (S[i + j] - '0')) % 2019;
      if (dp[i][j] == 0)
        ans++;
    }
  }

  cout << ans << endl;

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

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

#define ll long long

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  ll a[200005];
  map<ll, ll> mp;
  ll x = 1;
  for (int i = 0; i < s.size(); i++) {
    if (i != 0)
      a[i] = ((s[i] - '0') * x + a[i - 1]) % 2019;
    else
      a[i] = s[i] - '0';
    x *= 10;
    x %= 2019;
  }
  for (int i = 0; i < s.size(); i++)
    mp[a[i]]++;
  ll res = 0;
  for (int i = 0; i < 2019; i++)
    res += mp[i] * (mp[i] - 1) / 2;
  res += mp[0];
  // res+=mp[0]+mp[0]*(mp[0]-1)/2;
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const long long INF = 2147483647;

int main() {
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j + i <= s.size(); j++) {
      long long ans = stoll(s.substr(i, j));
      if (ans % 2019 == 0 && ans != 0 && ans <= INF)
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int pow_mod_2019(int y) {
  int ans = 1;
  for (int i = 0; i < y; i++) {
    ans = (ans * 10) % 2019;
  }
  return ans;
}

int main() {
  std::string S;
  std::cin >> S;
  // S = 2019
  // T[0] = 9;
  // T[1] = 19;
  // T[2] = 19;
  // T[3] = 2019;

  // T[i] - T[j] mod 2019 == 0
  // T[i] mod 2019 - T[j] mod 2019 == 0
  // T[i] == T[j] となるような (i, j) の個数

  std::vector<int> decimal(S.length());
  int d = 1;
  for (int i = 0; i < decimal.size(); i++) {
    decimal[i] = d;
    d = (d * 10) % 2019;
  }

  std::reverse(S.begin(), S.end());
  std::unordered_map<int, int> count;
  count[0] = 1;
  int x = 0;
  for (int i = 0; i < S.length(); i++) {
    x = ((S[i] - '0') * decimal[i] + x) % 2019;
    count[x]++;
  }
  int ans = 0;
  for (auto const &e : count) {
    int diff = e.second * (e.second - 1) / 2;
    ans += diff;
  }
  std::cout << ans << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

int main() {
  std::string S;
  std::cin >> S;
  int count = 0;
  if (S.length() < 4) {
    std::cout << 0 << std::endl;
    return 0;
  }
  for (size_t i = 0; i < S.length() - 4; i++) {
    for (size_t j = i + 4; j <= S.length(); j++) {
      if (std::stoll(S.substr(i, j - i)) % 2019 == 0) {
        count++;
      }
    }
  }
  std::cout << count << std::endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

void print(const std::vector<long long> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

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

  if (S.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  reverse(S.begin(), S.end());

  int m = 2019;
  vector<int> count(m);
  int x = 1;
  int tot = 0;
  int ans = 0;
  for (int i = 0; i < (int)S.size(); i++) {
    count[tot]++;
    tot += ((S[i] - '0') * x);
    tot = tot % m;
    ans += count[tot];
    x = x * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  if (S.size() < 3) {
    cout << 0 << endl;
    return 0;
  }

  long long t;
  int count = 0;
  for (int i = 0; i < (int)(S.size()) - 3; i++) {
    for (int j = i + 3; j < (int)(S.size()); j++) {
      t = stoll(S.substr(i, j));
      if (t % 2019 == 0) {
        count++;
      }
    }
  }

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

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

using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  unordered_map<ll, ll> m;
  m[0] = 1;
  reverse(all(S));
  ll tmp = 0, ten = 1;
  REP(i, S.size()) {
    tmp += char_to_int(S[i]) * ten;
    tmp %= 2019;
    ten *= 10;
    ten %= 2019;
    m[tmp]++;
  }
  for (auto x : m) {
    ans += 0.5 * x.second * (x.second - 1);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

using ll = long long;
#define REP(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }

int main() {
  string S;
  cin >> S;
  ll ans = 0;
  for (int i = 0; i < S.size(); ++i) {
    for (int k = i; k < S.size(); ++k) {
      string tmp = S.substr(i, k - i + 1);
      ll res = 0;
      REP(i, tmp.size()) {
        res += char_to_int(tmp[i]);
        res %= 2019;
        if (i != tmp.size() - 1)
          res *= 10;
      }
      if (res == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  if (10 % p == 0) {
    ll ans = 0;
    rep(i, n) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
    cout << ans << endl;
    return 0;
  }

  ll ten = 1;
  vector<ll> c(n + 1, 0);
  for (int i = n - 1; i >= 0; i--) {
    ll t = s[i] - '0';
    ll u = t * ten % p;
    c[i] = (c[i + 1] + u) % p;
    ten = (ten * 10) % p;
  }
  map<ll, ll> mp;
  mp[0]++;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += mp[c[i]];
    mp[c[i]]++;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  int ans = 0;
  vector<map<int, int>> mp(2);
  for (int i = 0; i < n; i++) {
    int x = i % 2;
    mp[x][0] += 1;
    int t = s[i] - '0';
    for (auto v : mp[i % 2]) {
      mp[(x) ^ 1][(v.first * 10 + t) % mod] += v.second;
    }
    ans += mp[(x) ^ 1][0];
    mp[x].clear();
  }
  cout << ans << endl;

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

#include <iostream>
#include <string>

using namespace std;

int main() {
  // modulo[i][j] : i桁目までの数字に余りがjになるものが存在するか？
  long long modulo_f[2019] = {};
  long long modulo[2019] = {};
  string s;
  cin >> s;
  int i = 0;
  long long ans = 0;
  for (auto c : s) {
    int cv = c - '0';
    if (i == 0) {
      modulo[cv] = 1;
    } else {
      modulo[cv] = 1;
      for (int k = 0; k < 2019; k++) {
        modulo[(k * 10 + cv) % 2019] += modulo_f[k];
      }
    }
    ans += modulo[0];
    i++;
    for (int j = 0; j < 2019; j++) {
      modulo_f[j] = modulo[j];
      modulo[j] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int main() {
  // modulo[i][j] : i桁目までの数字に余りがjになるものが存在するか？
  long long modulo[20001][2019] = {};
  string s;
  cin >> s;
  int i = 0;
  long long ans = 0;
  for (auto c : s) {
    int cv = c - '0';
    if (i == 0) {
      modulo[0][cv] = 1;
    } else {
      modulo[i][cv] = 1;
      for (int k = 0; k < 2019; k++) {
        modulo[i][(k * 10 + cv) % 2019] += modulo[i - 1][k];
      }
    }
    ans += modulo[i][0];
    i++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  int c = 0;
  int b = 1;
  int *r = new int[n + 1]{};
  int *cc = new int[2019]{};
  cc[0]++;
  for (int i = n - 1; i >= 0; i--) {
    r[i] = (r[i + 1] + b * (s[i] - '0')) % 2019;
    b = (b * 10) % 2019;
    c += cc[r[i]];
    cc[r[i]]++;
  }
  cout << c << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  if (n < 4)
    cout << 0 << endl;
  int c = 0;
  for (int i = 0; i < n - 3; i++) {
    for (int j = i; j < n; j++) {
      if (stoull(s.substr(i, j - i + 1)) % 2019ull == 0)
        c++;
    }
  }
  cout << c << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string S;
  cin >> S;
  int n = S.size();
  ll fac = 1;
  ll cur = 0;

  vector<ll> dp(2019, 0);
  dp[0]++;
  for (int i = 0; i < n; i++) {
    ll a = S[n - i - 1] - '0';
    cur = (cur + a * fac) % 2019;
    fac = (fac * 10) % 2019;
    dp[cur]++;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    int m = dp[i];
    ans += m * (m - 1) / 2;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  int n = S.size();
  vector<int> a(n);

  for (int i = 0; i < n; i++) {
    a[i] = S[i] - '0';
  }

  vector<int> dp(2019, 0);
  vector<ll> T(n + 1);

  T[n] = 0;
  dp[0]++;
  for (int i = n - 1; i >= 0; i--) {
    T[i] = T[i + 1] + a[i] * pow(10, n - i - 1);
    dp[T[i] % 2019]++;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; i++) {
    if (dp[i]) {
      int m = dp[i];
      ans += m * (m - 1) / 2;
    }
  }

  cout << ans << endl;
}