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

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

template <int MOD> struct Fp {
  long long val;
  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

const int MOD = 2019;
using mint = Fp<MOD>;

int main(void) {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  mint tmp = 0, p = 1;

  vector<ll> dp(2020, 0);

  for (ll i = 0; i < s.size(); ++i) {
    mint c = s[i] - '0';
    tmp += c * p;
    dp.at(tmp.val)++;
    p *= 10;
  }

  ll ans = dp.at(0);

  rep(i, dp.size()) { ans += dp.at(i) * (dp.at(i) - 1) / 2; }

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


Error: 0

Fixed submission:

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

int main(void) {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  ll tmp = 0, p = 1;

  vector<int> dp(2020, 0);

  rep(i, s.size()) {
    tmp += (s[i] - '0') * p;
    ll md = tmp % 2019;
    dp.at(md)++;
    p *= 10;
  }

  int ans = dp.at(0);

  rep(i, dp.size()) { ans += dp.at(i) * (dp.at(i) - 1) / 2; }

  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)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

// 解説参考.
int main() {
  string s;
  cin >> s;

  vi cnt(2019, 0);
  cnt[0] = 1;
  int tmp = 0;
  int weight = 1;
  llong ans = 0;
  REP(i, s.size()) {
    tmp = (tmp + weight * (s[s.size() - 1 - i] - '0') % 2019) % 2019;
    ans += cnt[tmp];
    cnt[tmp]++;
    weight = (10 * weight) % 2019;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

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

  int n = s.size();
  llong ans = 0;
  REP(i, n) {
    int tmp = 0;
    for (int j = i; j < n; ++j) {
      tmp = 10 * tmp + (s[j] - '0');
      tmp %= 2019;
      if (tmp == 0)
        ans++;
    }
  }

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

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 2019;

ll modpow(int a, int n) {
  ll 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;
  vi cnt(2019, 0);
  cnt[0]++;
  ll sum = 0;
  for (int i = S.size() - 1; i >= 0; i--) {
    sum = (sum + (S[i] - '0') * modpow(10, S.size() - 1 - i) % MOD) % MOD;
    cnt[sum]++;
  }
  ll res = 0;
  for (int i = 0; i < 2019; i++) {
    res += cnt[i] * (cnt[i] - 1) / 2;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 2019;

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

int main() {
  string S;
  cin >> S;
  vi cnt(2019, 0);
  cnt[0]++;
  ll sum = 0;
  for (int i = S.size() - 1; i >= 0; i--) {
    sum = (sum + (S[i] - '0') * modpow(10, S.size() - 1 - i) % MOD) % MOD;
    cnt[sum]++;
  }
  ll res = 0;
  for (int i = 0; i < 2019; i++) {
    res += cnt[i] * (cnt[i] - 1) / 2;
  }
  cout << res << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x) cerr << #x << ": " << x << endl;
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define endl '\\n'
#define MAX 1000000
typedef long long int ll;
using namespace std;

const ll mod = 1e9 + 7;
const double eps = 1e-9;

string s;
int dp[2019];

void solve() {
  cin >> s;
  int n = (int)s.size();
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int tdp[2019];
    memset(tdp, 0, sizeof(tdp));
    int d = (s[i] - '0');
    tdp[d % 2019] = 1;
    for (int j = 0; j < 2019; j++) {
      if (dp[j] > 0) {
        int rem = (10 * j + d) % 2019;
        if (rem == 0)
          ans += dp[j];
        tdp[rem] += dp[j];
      }
    }
    for (int i = 0; i < 2019; i++) {
      dp[i] = tdp[i];
    }
  }
  cout << ans;
}

int main() {
  fastio int test = 1;
  while (test--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x) cerr << #x << ": " << x << endl;
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define endl '\\n'
#define MAX 1000000
typedef long long int ll;
using namespace std;

const ll mod = 1e9 + 7;
const double eps = 1e-9;

void solve() {
  string s;
  cin >> s;
  int n = (int)s.size();
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int k = 4; k <= n - i; k++) {
      ll trial = 0;
      for (int j = i; j < i + k; j++) {
        trial = trial * 10 + (s[j] - '0');
        trial %= 2019LL;
      }
      ans += (trial == 0LL);
    }
  }
  cout << ans;
}

int main() {
  fastio int test = 1;
  while (test--)
    solve();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define MP make_pair

ll mod(ll mod_num) { return mod_num % 2019; }

int main(void) {
  ll mtmp, stmp, num[2020] = {}, tmp, ans = 0, s_size;
  string s;
  cin >> s;

  mtmp = 1;

  s_size = (ll)(s.size());

  stmp = mod((stoll)(s.substr(s_size - 1, 1)));
  num[(stoll)(s.substr(s_size - 1, 1))]++;

  for (int i = 1; i < s_size; i++) {
    // cout<<i<<"\\n";
    stmp = mod(mtmp * 10 * ((s[s_size - i - 1]) - '0') + stmp);
    mtmp = mod(mtmp * 10);
    if ((s[s_size - i - 1]) - '0' != 0)
      num[stmp]++;
  }

  // cout<<"\\n";

  num[0]++;
  for (int i = 0; i <= 2019; i++) {
    tmp = num[i];

    if (tmp != 0) {
      // cout<<i<<" "<<tmp<<"\\n";
      ans += (tmp * (tmp - 1)) / 2;
    }
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
#define MP make_pair

ll mod(ll mod_num) { return mod_num % 2019; }

int main(void) {
  ll mtmp, stmp, num[2020] = {}, tmp, ans = 0, s_size;
  string s;
  cin >> s;

  mtmp = 100;

  s_size = (ll)(s.size());

  stmp = mod((stoll)(s.substr(s_size - 3, 3)));

  for (int i = 3; i < s_size; i++) {
    // cout<<i<<"\\n";
    stmp = mod(mtmp * 10 * ((s[s_size - i - 1]) - '0') + stmp);
    mtmp = mod(mtmp * 10);
    num[stmp]++;
  }

  // cout<<"\\n";

  num[0]++;
  for (int i = 0; i <= 2019; i++) {
    tmp = num[i];

    if (tmp != 0) {
      // cout<<i<<" "<<tmp<<"\\n";
      ans += (tmp * (tmp - 1)) / 2;
    }
  }

  cout << ans << "\\n";

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

/*Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.

My little horse must think it queer
To stop without a farmhouse near
Between the woods and frozen lake
The darkest evening of the year.

He gives his harness bells a shake
To ask if there is some mistake.
The only other sound’s the sweep
Of easy wind and downy flake.

The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
const int maxn = 100010;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  lli p = 1ll, r = 0ll, res = 0ll;
  cin >> s;
  int n = s.length();
  int f[2019] = {0};
  f[0] = 1;
  for (int i = n - 1; i >= 0; --i) {
    r = (r + (s[i] - '0') * p) % 2019;
    p = (p * 10) % 2019;
    res += f[r]++;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

/*Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.

My little horse must think it queer
To stop without a farmhouse near
Between the woods and frozen lake
The darkest evening of the year.

He gives his harness bells a shake
To ask if there is some mistake.
The only other sound’s the sweep
Of easy wind and downy flake.

The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
const int maxn = 100010;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  lli res = 0ll;
  cin >> s;
  int n = s.length();
  map<pair<int, int>, bool> m;
  for (int i = 0; i < n; ++i) {
    int r = 0, ct = 0;
    for (int j = i; j < n; ++j) {
      r = (10 * r + s[j] - '0') % 2019;
      if (m[{j, r}] == true)
        break;
      m[{j, r}] = true;
      if (r == 0)
        ++ct;
    }
    res += 1ll * (ct * (ct + 1)) / 2;
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200004;
int rmd[maxn];
int pref[maxn];
char s[maxn];
int main() {
  cin >> (s + 1);
  int base = 1;
  long long cnt = 0;
  rmd[0] = 1;
  int len = strlen(s + 1);
  for (int i = len; i >= 1; i--) {
    pref[i] = (pref[i + 1] + base * (s[i] - '0')) % 2019;
    base = base * 10 % 2019;
    cnt += rmd[pref[i]];
    rmd[pref[i]]++;
  }
  printf("%lld\\n", cnt);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int mod[200004];
int ten[200004];
int main() {
  int len = 0;
  ten[0] = 1;
  string a;
  cin >> a;
  len = a.length();
  for (int i = 1; i <= len; i++) {
    ten[i] = 10 * ten[i - 1] % 2019;
    int tmp = a[i - 1] - '0';
    mod[i] = (tmp % 2019 + mod[i - 1] * 10 % 2019) % 2019;
  }
  long long ans = 0;
  for (int i = 1; i <= len; i++) {
    for (int j = i + 1; j <= len; j++) {
      if ((mod[j] + 2019 - mod[i - 1] * ten[j - i + 1]) % 2019 == 0) {
        ans++;
      }
    }
  }
  printf("%lld\\n", ans);
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int len = s.size();

  // vector<int> digit_MOD_res(len);
  vector<int> d(len + 1);

  int keta = 1;
  d[0] = 0;

  vector<int> count(2019, 0);
  count[0]++;

  for (int i = 0; i < len; ++i) {
    int digit = (s[i] - '0');
    // digit_MOD_res[i] = (digit * keta) % 2019;
    // d[i+1] = (d[i] + digit_MOD_res[i]) % 2019;
    int a = (digit * keta) % 2019;
    d[i + 1] = (d[i] + a) % 2019;
    keta *= 10;
    keta %= 2019;
    count[d[i + 1]]++;
  }

  long long ans = 0;
  for (int i = 0; i < 2019; ++i) {
    if (count[i] > 1) {
      ans += (count[i] * (count[i] - 1)) / 2;
    }
  }

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

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int len = s.size();

  vector<int> digit_MOD_res(len);
  vector<int> d(len + 1);

  int keta = 1;
  d[0] = 0;

  vector<int> count(2019, 0);

  for (int i = 0; i <= len; ++i) {
    int digit = (s[i] - '0');
    // digit_MOD_res[i] = (digit * keta) % 2019;
    // d[i+1] = (d[i] + digit_MOD_res[i]) % 2019;
    int a = (digit * keta) % 2019;
    d[i + 1] = (d[i] + a) % 2019;
    keta *= 10;
    keta %= 2019;
    count[d[i]]++;
  }

  long long ans = 0;
  for (int i = 0; i < 2019; ++i) {
    if (count[i] > 1) {
      ans += (count[i] + count[i] - 1) / 2;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
#define INF 1LL << 30
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

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

  vector<ll> v;
  v.push_back(0LL);
  ll p = 1;
  ll pre = 0;
  rep(i, n) {
    ll x = s[i] - '0';
    x = (x * p) % 2019;
    x = (x + pre) % 2019;
    v.push_back(x % 2019);
    p = (p * 10) % 2019;
    pre = x;
  }
  map<int, int> m;
  rep(i, n + 1) { m[v[i]]++; }

  ll ans = 0;
  for (auto e : m) {
    int num = e.second;
    if (num > 1) {
      ans += (num * (num - 1)) / 2;
    }
  }

  cout << ans << endl;
}


Error: -11

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string arr[n];
  set<string> s;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    s.insert(arr[i]);
  }
  cout << s.size();
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define rep2(i, j, n) for (int j = i; j < n; j++)
#define rep3(i, j, n) for (int j = i; j <= n; j++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define sp(n) cout << fixed << setprecision(n)
typedef long long ll;
using namespace std;
const int mod = 2019;
long long modpow(long long a, long long n) {
  long long r = 1;
  while (n) {
    r = r * ((n % 2) ? a : 1) % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return r;
}

int main(void) {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  ll res = 0, now = 0;
  map<ll, ll> mp;
  rep(i, s.size()) {
    ll t = modpow(10, i);
    ll d = s[i] - '0';
    d *= t;
    d += now;
    d %= 2019;
    mp[d]++;
    now = d;
  }
  for (auto &p : mp) {
    ll x = p.first, y = p.second;
    if (x == 0) {
      res += (y + 1) * y / 2;
    } else {
      y--;
      res += (y + 1) * y / 2;
    }
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define rep2(i, j, n) for (int j = i; j < n; j++)
#define rep3(i, j, n) for (int j = i; j <= n; j++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define sp(n) cout << fixed << setprecision(n)
typedef long long ll;
using namespace std;

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

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Reverse(x) reverse((x).begin(), (x).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define INF INT_MAX
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15) // 15桁まで表示
#define str string
typedef std::pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<ui, ui> puu;
typedef pair<int, int> P;
typedef vector<int> vi;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a * b / gcd(a, b); };
int floor(int a, int b) { return (a + b - 1) / b; };
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
int d[2019];
int d2[2019];
int main() {

  vi a(200010);
  str s;
  cin >> s;
  int n = s.length();
  int ans = 0;
  rep(i, n) a[i] = s[i] - '0';
  d2[a[0]]++;
  REP(i, 1, n) {
    d[a[i]]++;
    rep(j, 2019) { d[(10 * j + a[i]) % 2019] += d2[j]; }
    ans += d[0];
    rep(j, 2019) {
      d2[j] = d[j];
      d[j] = 0;
    }
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Reverse(x) reverse((x).begin(), (x).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define INF INT_MAX
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15) // 15桁まで表示
#define str string
typedef std::pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<ui, ui> puu;
typedef pair<int, int> P;
typedef vector<int> vi;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a * b / gcd(a, b); };
int floor(int a, int b) { return (a + b - 1) / b; };
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};
int d[200010][2019];
int main() {

  vi a(200010);
  str s;
  cin >> s;
  int n = s.length();
  rep(i, n) a[i] = s[i] - '0';
  d[0][a[0]]++;
  REP(i, 1, n) {
    d[i][a[i]]++;
    rep(j, 2019) { d[i][(10 * j + a[i]) % 2019] += d[i - 1][j]; }
  }
  int ans = 0;
  rep(i, n) ans += d[i][0];
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const int INF = 1e9;
const int MOD = 1e7;
const ll LINF = 1e18;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<ll> v(2020, 0);
  ll cur = 0;
  ll p = 1;
  v[0]++;
  for (int i = 0; i < s.size(); i++) {
    cur = cur + p * (s[i] - '0');
    cur %= 2019;
    p = (p * 10) % 2019;
    v[cur]++;
  }
  ll ans = 0;
  for (int i = 0; i <= 2019; i++) {
    ans += v[i] * (v[i] - 1) / 2;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  ll cnt = 0;
  for (int i = 0; i < n - 3; i++) {
    string t = "";
    t += s[i];
    t += s[i + 1];
    t += s[i + 2];
    for (int j = i + 3; j < n; j++) {
      t += s[j];
      ll a = atoi(t.c_str());
      if (a % 2019 == 0) {
        cnt++;
        break;
      }
    }
  }
  cout << cnt << endl;
}