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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
#define END return 0
typedef int64_t ll;
const int mod7 = 1e9 + 7;
const int mod9 = 1e9 + 9;
const int inf = 1 << 30;
const ll INF = 1LL << 60;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : abs(a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  map<int, int> m;
  reverse(s.begin(), s.end());
  ll sum = 0, k = 1;
  ;
  m[sum]++;
  rep(i, n) {
    sum += k * (s[i] - '0'), sum %= 2019;
    k = k * 10 % 2019;
    m[sum]++;
  }
  ll ans = 0;
  for (auto c : m) {
    ll a = c.second;
    ans += a * (a - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
#define END return 0
typedef int64_t ll;
const int mod7 = 1e9 + 7;
const int mod9 = 1e9 + 9;
const int inf = 1 << 30;
const ll INF = 1LL << 60;
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : abs(a); }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int ans = 0;
  rep(i, n) {
    ll sum = 0;
    for (int j = i; j < n; j++) {
      sum *= 10, sum %= 2019;
      sum += (s[j] - '0');
      if (sum % 2019 == 0)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)

using namespace std;

template <class T> using vec = std::vector<T>;
typedef long long ll;
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;
}

const long long INF = 1LL << 60;

int lcm(int a, int b) {
  // 最小公倍数
  return a / __gcd(a, b) * b;
}

const int m = 2019;
string S;

int main() {
  cin >> S;
  reverse(S.begin(), S.end());
  int x = 1, tot = 0;
  int n = S.size();
  // cnt(2019):累積和(mod2019)が何個有るかを数える。(0-2018)
  // 同じ値のものが２つあれば、そのセットでmod2019が0になる
  vec<ll> cnt(m);
  ll ans = 0;
  rep(i, n) {
    cnt[tot]++;
    tot += (S[i] - '0') * x; // 各桁の数を取得
    tot %= m;                // modにする
    ans += cnt[tot];
    x = x * 10 % m;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:



#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)

using namespace std;

template <class T> using vec = std::vector<T>;
typedef long long ll;
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;
}

const long long INF = 1LL << 60;

int lcm(int a, int b) {
  // 最小公倍数
  return a / __gcd(a, b) * b;
}

string S;

int main() {
  cin >> S;

  ll res = 0;
  string tmp;

  rep(i, int(S.length()) - 3) {
    loop(j, 4, int(S.size()) - i + 1) {
      tmp = S.substr(i, j);

      if (stoll(tmp) % 2019 == 0) {
        res++;
      }
    }
  }
  cout << res << 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)
#define sayyes cout << "Yes" << endl;
#define sayno cout << "No" << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1e9 + 7;

ll combi(ll a, ll b) {
  ll ret = 1;
  for (ll i = a; i > a - b; i--) {
    ret *= i;
  }
  for (ll i = b; i > 0; i--) {
    ret /= i;
  }
  return ret;
}

int main() {
  string s;
  cin >> s;
  int n = s.length();
  vector<ll> dp(n + 1, 0);
  ll ans = 0;
  int ten = 1;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = (dp[i + 1] + (s[i] - '0') * ten % 2019) % 2019;
    ten *= 10;
    ten %= 2019;
  }
  vector<int> p(2019, 0);
  for (int i = n; i >= 0; i--) {
    ans += p[dp[i]];
    p[dp[i]]++;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sayyes cout << "Yes" << endl;
#define sayno cout << "No" << endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int n = s.length();
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  ll ans = 0;
  for (int i = 0; i < n - 3; i++) {
    string ini = s.substr(i, 3);
    rep(k, 3) {
      dp[i][i + 2] *= 10;
      dp[i][i + 2] += (ini[k] - '0');
    }
    for (int j = i + 3; j < n; j++) {
      dp[i][j] = (dp[i][j - 1] * 10 % 2019 + (s[j] - '0')) % 2019;
      if (dp[i][j] == 0) {
        ans++;
        j += 3;
      }
    }
  }
  cout << ans;
}
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();
  long long res = 0;
  int suff = 0;
  int pot = 1;
  int mod = 2019;
  unordered_map<int, int> cnt_suff;
  cnt_suff[suff]++;
  for (int i = n - 1; i >= 0; i--) {
    int digit = s[i] - '0';
    suff = (suff + digit * pot) % mod;
    pot = (pot * 10) % mod;
    res += cnt_suff[suff];
    cnt_suff[suff]++;
  }

  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  string s;
  cin >> s;
  long long res = 0;

  for (int i = 0; i < s.size(); i++) {
    for (int j = i; j < s.size(); j++) {
      string t = s.substr(i, j - i + 1);
      if (stol(t) % 2019 == 0)
        res++;
    }
  }

  cout << res << endl;

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

#include <iostream>
#include <string>
#include <vector>
#define MAX 200000
using namespace std;

const long long mod = 2019;
long long dp[2020];

// aC2を返す
long long choose(long long a) {
  long long ans;
  ans = a * (a - 1) / 2;

  return ans;
}

int main() {
  string s;
  cin >> s;
  int sl = s.size(); // sのサイズ
  // cout << sl << endl;
  // sの累積和を入れる
  vector<int> t(sl), tt(sl); // 累積和
  long long ten = 1;         // 10の累乗
  long long ss;              // s.at(i)のlong long 型

  // cout << endl;

  for (int i = sl - 1; i >= 0; i--) {
    ss = (s.at(i) - '0');
    t.at(i) = (ten * ss) % mod;
    ten *= 10;
    ten = ten % mod;
    // cout << i << ":";
    // cout << ten << " ";
    // cout << t.at(i) << endl;
  }

  // 累積
  tt.at(0) = t.at(0);
  // cout << tt.at(0) << endl;
  dp[tt.at(0)]++;
  for (int i = 1; i < sl; i++) {
    tt.at(i) = (tt.at(i - 1) + t.at(i)) % mod;
    // cout << tt.at(i) << endl;
    dp[tt.at(i)]++;
  }

  // mod2019の値が等しいものを見つける

  long long ans = dp[0];

  for (int i = 0; i < mod; i++) {
    if (dp[i] >= 2) {
      ans += choose(dp[i]);
      // cout << "aaa" << endl;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  string s;
  int ans = 0;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    for (int j = i + 3; j < s.size(); j++) {
      string substr = s.substr(i, j);
      long long num = stol(substr);
      if (num % 2019 == 0) {
        ans++;
        // cout << num << endl;
        // cout << i << " " << j << endl;
      }
    }
  }

  cout << ans << endl;

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

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

long long N;
string S;
int P;

long long solve() {
  if (P == 2 || P == 5) {
    long long res = 0;
    for (int i = 0; i < N; ++i)
      if ((S[N - i - 1] - '0') % P == 0)
        res += N - i;
    return res;
  }
  vector<long long> val(P, 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) % P;
    tenfactor = (tenfactor * 10) % P;
    val[cur]++;
  }
  long long res = 0;
  for (int p = 0; p < P; ++p)
    res += val[p] * (val[p] - 1) / 2;
  return res;
}

int main() {
  cin >> S;
  P = 2019;
  N = S.size();
  cout << solve() << endl;
}

Error: -6
terminate called after throwing an instance of 'std::invalid_argument'
  what():  stoi


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define PI acos(-1)
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
ll mod = 1e9 + 7;

int main() {
  fast_io

      string s;
  cin >> s;
  ll ans = 0;
  int n = s.size();

  for (int i = 0; i < n - 4; i++) {
    string str = s.substr(i, 4);
    ll num = stoi(str);
    if (num % 2019 == 0) {
      ans++;
    }
    for (int j = i + 4; j < n; j++) {
      num *= 10;
      num += ll(s[j] - '0');
      // cout<<num<<endl;
      // cout<<"j="<<j<<endl;
      if (num % 2019 == 0) {
        ans++;
        j++;
        str = s.substr(j, 3);
        num = stoi(str);
      }
    }
  }
  cout << ans << 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>;

const int m = 2019;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end()); // 10^xとaxを一致させるため
  int x = 1, tot = 0;          // totは累積和Si
  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: WA

Fixed submission:

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

int main() {
  string a;
  cin >> a;
  ll s = stoll(a);
  cout << s << endl;
  ll b = a.size(); // aの桁数
  ll count = 0;

  for (int i = 0; i < 2000100; i++) {
    ll t = s;
    while (t > 2000) {
      if (t % 2019 == 0)
        count++;
      t /= 10;
    }
    ll x = 1;
    for (int j = 0; j < b - 1; j++) {
      x *= 10;
    }
    s %= x;
    b--;
  }
  cout << count << endl;
}
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;
  auto dp = vector<ll>(2019);
  ll ret = 0;
  for (auto &c : S) {
    auto dp2 = vector<ll>(2019);
    for (int i = 0; i < 2019; i++) {
      dp2[(i * 10 + (c - '0')) % 2019] += dp[i];
    }
    dp2[c - '0']++;
    ret += dp2[0];
    dp.swap(dp2);
  }
  cout << ret << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll len, num, len1, len2, cnt;
string S, strs;

bool check(string str) {
  len = str.length();
  num = 0;
  for (int i = 0; i < len; i++) {
    num = num * 10 + (str[i] - '0');
    num %= 2019;
  }
  if (num)
    return false;
  return true;
}

int main() {
  cin >> S;
  len1 = S.length() - 3;
  for (int i = 0; i < len1; i++) {
    len2 = S.length() - i;
    for (int j = 4; j <= len2; j++) {
      if (check(S.substr(i, j))) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

////D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2019], ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string str;
  cin >> str;
  a[0] = 1;
  ll t = 0, cnt = 1;
  for (int idx = str.length() - 1; idx >= 0; idx--) {
    t = (t + cnt * (str[idx] - '0') % 2019) % 2019;
    // cout<<t<<endl;
    ans += a[t];
    a[t]++;
    cnt = cnt * 10 % 2019;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2019], ans;

ll check(string str) {
  ll len = str.length();
  ll num = 0;
  for (int i = 0; i < len; i++) {
    num = num * 10 + (str[i] - '0');
    num %= 2019;
  }
  return num;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string str;
  cin >> str;
  a[0] = 1;
  for (int idx = str.length() - 1; idx >= 0; idx--) {
    ll t = check(str.substr(idx));
    ans += a[t];
    a[t]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ll INF = 1e18;
#define pu push
#define pb push_back
#define mp make_pair
#define print(x) cout << (x) << endl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define SORT(x) sort(x.begin(), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define fi first
#define sc second

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);
  string S;
  // rep(i,2200) {
  //     S.push_back('1');
  //     S.push_back('2');
  //     S.push_back('3');
  //     S.push_back('4');
  //     S.push_back('5');
  //     S.push_back('6');
  //     S.push_back('7');
  //     S.push_back('8');
  //     S.push_back('9');
  // }
  cin >> S;
  ll ans = 0;
  int T = 0;
  int pow10 = 1;
  vector<int> count(2019, 0);
  count.at(0)++;
  for (int i = S.size() - 1; i >= 0; i--) {
    int Snum = S.at(i) - '0';
    T = (T + Snum * pow10) % 2019;
    pow10 = pow10 * 10 % 2019;
    count.at(T)++;
    // cout << T << endl;
  }
  rep(i, 2019) { ans += count.at(i) * (count.at(i) - 1) / 2; }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ll INF = 1e18;
#define pu push
#define pb push_back
#define mp make_pair
#define print(x) cout << (x) << endl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define SORT(x) sort(x.begin(), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define fi first
#define sc second

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(10);
  string S;
  // rep(i,2200) {
  //     S.push_back('1');
  //     S.push_back('2');
  //     S.push_back('3');
  //     S.push_back('4');
  //     S.push_back('5');
  //     S.push_back('6');
  //     S.push_back('7');
  //     S.push_back('8');
  //     S.push_back('9');
  // }
  cin >> S;
  ll count = 0;
  queue<int> que;
  que.push(-1);
  for (int i = 0; i < S.size(); i++) {
    int Snum = S.at(i) - '0';
    que.push(Snum);
    int top = 0;
    while (true) {
      int top = que.front();
      que.pop();
      if (top == -1) {
        que.push(top);
        break;
      } else {
        que.push((top * 10 + Snum) % 2019);
        if ((top * 10 + Snum) % 2019 == 0)
          count++;
        que.empty();
      }
    }
  }
  cout << count << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define fastIO                                                                 \\
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),              \\
      cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 200009;

ll freq[2020], arr[N];

int main() {
  fastIO;
#ifdef LOCAL
//	freopen("input.in", "rt", stdin);
#endif
  string str;
  cin >> str;
  freq[0] = 1;
  int n = str.size();
  int tmp = 1;
  for (int i = n - 1; i >= 0; --i) {
    arr[i] = (arr[i + 1] % 2019 + ((str[i] - '0') * tmp) % 2019) % 2019;
    freq[arr[i]]++;
    tmp *= 10;
    tmp %= 2019;
  }
  ll ans = 0;
  for (int i = 0; i < 2019; ++i)
    ans += freq[i] * (freq[i] - 1) / 2;
  cout << ans;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fastIO                                                                 \\
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),              \\
      cin.tie(nullptr), cout.tie(nullptr)
using namespace std;

const int N = 2e4 + 9;

int mem[N][2020][2], n;
string str;

int solve(int idx, int rem, int can) {
  if (idx == n) {
    if (can == 2 && rem == 0)
      return 1;
    return 0;
  }
  int &ret = mem[idx][rem][can];
  if (~ret)
    return ret;
  ret = 0;
  if (can == 2)
    return ret = solve(idx + 1, rem, can);
  if (can == 1) {
    int cur = rem * 10;
    cur %= 2019;
    cur += (str[idx] - '0');
    cur %= 2019;
    ret = solve(idx + 1, cur, can);
    ret += solve(idx + 1, cur, 2);
  } else {
    int cur = rem * 10;
    cur %= 2019;
    cur += (str[idx] - '0');
    cur %= 2019;
    ret += solve(idx + 1, rem, can);
    ret += solve(idx + 1, cur, 1);
  }
  return ret;
}

int main() {
  fastIO;
#ifdef LOCAL
//	freopen("input.in", "rt", stdin);
#endif
  memset(mem, -1, sizeof mem);
  cin >> str;
  n = str.size();
  cout << solve(0, 0, 0);
  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, s, n) for (ll i = (ll)(s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
const ll MOD = 1e9 + 7;
ll LLINF = 1LL << 60;
int INF = INT_MAX;

int main() {
  string s;
  cin >> s;
  map<int, int> res;
  int x = 0;
  int p = 1;
  res[0]++;
  for (int i = s.size() - 1; i >= 0; i--) {
    x = (x + (s[i] - '0') * p) % 2019;
    res[x]++;
    p = (p * 10) % 2019;
  }
  ll ans = 0;
  for (auto i : res) {
    ans += i.second * (i.second - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
const ll MOD = 1e9 + 7;
ll LLINF = 1LL << 60;
int INF = INT_MAX;

//

int main() {
  string s;
  cin >> s;
  ll ss = s.size();
  ll cnt = 0;
  for (ll i = 0; i < ss; i++) {
    ll temp = s[i] - '0';
    for (ll j = 1; i + j < ss; j++) {
      temp = (temp * 10 + (s[i + j] - '0')) % 2019;
      if (temp == 0)
        cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)
#define INF 1e18

inline int read() {
  int x = 0, neg = 1;
  char op = getchar();
  while (!isdigit(op)) {
    if (op == '-')
      neg = -1;
    op = getchar();
  }
  while (isdigit(op)) {
    x = 10 * x + op - '0';
    op = getchar();
  }
  return neg * x;
}
inline void print(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}

const int maxn = 2e5 + 10;
string str;
int a[maxn], b[maxn], cnt[2020];
int fac[maxn];
signed main() {
  IO;
  cin >> str;
  reverse(str.begin(), str.end());
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * 10 % 2019;
  for (int i = 0; i < str.size(); i++) {
    a[i] = (str[i] - '0') * fac[i] % 2019;
  }
  b[0] = a[0];
  for (int i = 1; i < str.size(); i++) {
    b[i] = (b[i - 1] + a[i]) % 2019;
  }
  //  for(int i = 0 ; i < str.size() ; i++) cout << b[i] << ' ';
  //  cout << '\\n';
  mset(cnt, 0);
  int ans = 0;
  cnt[0]++;
  for (int i = 0; i < str.size(); i++) {
    ans += cnt[b[i]];
    cnt[b[i]]++;
  }
  cout << ans << '\\n';
  return 0;
}

Error: MLE

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false)
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, _) memset(s, _, sizeof(s))
#define pb push_back
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)
#define INF 0x3f3f3f3f
typedef long long ll;

string s;
int dp[200010][2020];
int main() {
  cin >> s;
  s = " " + s;
  memset(dp, 0, sizeof(dp));
  dp[1][s[1] - '0'] = 1;
  int len = s.size();
  for (int i = 2; i <= len; i++) {
    dp[i][s[i] - '0']++;
    for (int j = 0; j < 2019; j++) {
      int flag = (j * 10 + s[i] - '0') % 2019;
      dp[i][flag] += dp[i - 1][j];
    }
  }
  ll ans = 0;
  for (int i = 1; i <= len; i++)
    ans += dp[i][0];
  printf("%lld\\n", ans);
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long ll;
// template<class itr> void cins(itr first, itr last){ for(auto i = first; i !=
// last; i++){ cin >> (*i); } }
/*
string S_str[H];
cins(S_str, S_str + H);
S_str[i][j];
*/
inline int at(ll x, int k) { return (x >> k) & 1; }
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)

int N;
string S;
int cnt[2019];
int main() {
  cin >> S;
  ll ans = 0;
  ll N = S.length();

  int cu = 0;
  int dg = 1;

  rrep(i, N - 1, 0) {
    cu = (cu + dg * (S[i] - '0')) % 2019;
    dg = (dg * 10) % 2019;

    ans += cnt[cu];
    if (cu == 0)
      ans++;

    cnt[cu]++;
  }

  cout << ans << endl;
}

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

  ll num[n];
  ll cnt[2019]={0};
  ll ans=0;
  rep(i,n){
    num[i]=  stoll(s.substr(i));


    ans += cnt[num[i]%2019];
    cnt[num[i]%2019]++;
  }
  ans += cnt[0];


  cout <<ans<<endl;


}
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// template<class itr> void cins(itr first, itr last){ for(auto i = first; i !=
// last; i++){ cin >> (*i); } }
/*
string S_str[H];
cins(S_str, S_str + H);
S_str[i][j];
*/
inline int at(ll x, int k) { return (x >> k) & 1; }
#define rep(i, n) for (ll i = 0; i < n; i++)

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

  ll num[n];
  ll cnt[2019] = {0};
  ll ans = 0;
  rep(i, n) {
    num[i] = stoll(s.substr(i));

    ans += cnt[num[i] % 2019];
    cnt[num[i] % 2019]++;
  }
  ans += cnt[0];

  cout << ans << endl;
}