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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<int>>;

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

  ll n = s.size();
  vector<int> d(n + 1);
  d[n] = 0;
  ll cnt = 1;
  for (int i = n - 1; i >= 0; i--) {
    ll a = (s[i] - '0') * cnt % p;
    d[i] = (d[i + 1] + a) % p;
    cnt *= 10;
    cnt %= p;
  }

  // 組み合わせの数を計算
  vector<int> sum(p, 0);
  ll ans = 0;
  Rep(i, 0, n) {
    ans += sum[d[i]];
    sum[d[i]]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
using Graph = vector<vector<int>>;

int Stoi(std::string str) {
  int ret;
  std::stringstream ss;
  ss << str;
  ss >> ret;
  return ret;
}

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

  ll mod = 2019;
  ll ans = 0;
  string s;
  cin >> s;
  ll l = s.size();
  rep(i, 0, l - 3) {
    string t = s.substr(i, 3);
    rep(j, i + 3, l) {
      t.push_back(s[j]);
      int n = Stoi(t);
      if (n % mod == 0)
        ans++;
    }
  }
  cout << ans << "\\n";

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"

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

  string s;
  cin >> s;

  reverse(rng(s));
  int n = s.size();
  vector<int> sum(n + 1, 0);
  int ten = 1;
  for (int i = 0; i < n; i++)
    sum[i + 1] = (sum[i] + ((s[i] - '0') * ten) % 2019) % 2019,
            ten = (ten * 10) % 2019;
  map<int, int> m;
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += m[sum[i]];
    m[sum[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()
#define endl "\\n"
#define int ll

template <uint_fast64_t m> int_fast64_t mpow(int_fast64_t x, int_fast64_t n) {
  using i64 = int_fast64_t;
  if (n == 0)
    return 1;
  if (n % 2)
    return ((x % m) * (mpow<m>(x, n - 1) % m)) % m;
  else {
    i64 half = mpow<m>(x, n / 2) % m;
    return (half * half) % m;
  }
}

// 2019=3*673
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string S;
  cin >> S;
  int N = S.size();
  vector<int> sum3(N + 1, 0), sum673(N + 1, 0);
  for (int i = 0; i < N; i++)
    sum3[i + 1] = (10 * sum3[i] + (S[i] - '0')) % 3;
  for (int i = 0; i < N; i++)
    sum673[i + 1] = (10 * sum673[i] + (S[i] - '0')) % 673;
  map<pair<int, int>, int> m;
  ll ans = 0;
  for (int i = 0; i <= N; i++) {
    ans += m[{sum3[i], sum673[i]}];
    m[{sum3[i], sum673[i]}]++;
    auto nm = m;
    for (auto &it : m)
      it.second = 0;
    for (int j = 0; j <= 2019; j++) {
      int a = j % 3;
      int b = j % 673;
      int c = nm[{a, b}];
      nm[{a, b}] = 0;
      a = (a * 10) % 3;
      b = (b * 10) % 673;
      m[{a, b}] += c;
    }
  }
  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;

int memo[2020];
const int modnum = 2019;

int main() {
  string s;

  cin >> s;
  reverse(s.begin(), s.end());

  for (int i = 0; i < 2020; i++) {
    memo[i] = 0;
  }
  memo[0] = 1;

  int sum = 0;
  int moddig = 1;
  for (int i = 0; i < s.size(); i++) {
    sum += ((int)(s[i] - '0') * moddig) % modnum;
    sum %= modnum;
    memo[sum]++;
    moddig = moddig * 10 % modnum;
  }

  ll ans = 0;
  for (int i = 0; i < 2020; i++) {
    if (memo[i] <= 0) {
      continue;
    }

    ans += memo[i] * (memo[i] - 1) / 2;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

const int modnum = 2019;

int main() {
  string s;

  cin >> s;

  ll cnt = 0;
  for (int i = (int)s.size(); i >= 0; i--) {
    int sum = 0;
    int moddig = 1;
    for (int j = i; j >= 0; j--) {
      sum = (sum + ((int)(s[j] - '0') * moddig) % modnum) % modnum;
      if (sum == 0) {
        cnt++;
      }
      moddig = moddig * 10 % modnum;
    }
  }

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define VI vector<int>
#define MOD 1000000007

typedef long long int ll;

const ll INF = (ll)1e19;
// const int INF=(1<<30);

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

  reverse(ALL(s));

  int n = 1;
  vector<int> moda;
  REP(i, s.size()) {
    int m = s[i] - '0';
    moda.emplace_back((n * m) % 2019);

    n = (n * 10) % 2019;
  }

  map<int, int> mp;
  mp[0] = 1;
  int t = 0;
  REP(i, moda.size()) {
    t = (t + moda[i]) % 2019;
    mp[t]++;
  }

  // REP(i,moda.size()){
  //   cout << moda[i] << endl;
  // }

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

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define VI vector<int>
#define MOD 1000000007

typedef long long int ll;

const ll INF = (ll)1e19;
// const int INF=(1<<30);

map<int, int> aa(map<int, int> mp) {
  map<int, int> ret;
  for (auto &v : mp) {
    ret[(v.first * 10) % 2019] = v.second;
  }
  return ret;
}

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

  int n = 0;
  map<int, int> mp;

  ll ans = 0;
  REP(i, s.size()) {
    mp = aa(mp);
    int m = s[i] - '0';
    n = (n * 10 + m) % 2019;
    if (n % 2019 == 0)
      ans++;
    ans += mp[n];

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

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

const int Mod = 2019;
class mll {
public:
  int x;
  mll(int x = 0) : x((x % Mod + Mod) % Mod){};
  void operator+=(const mll &a) {
    if ((x += a.x) >= Mod)
      x -= Mod;
  }
  void operator*=(const mll &a) { (x *= a.x) %= Mod; }
  mll operator+(const mll &a) const {
    mll ans = *this;
    ans += a;
    return ans;
  }
  mll operator*(const mll &a) const {
    mll ans = *this;
    ans *= a;
    return ans;
  }
};
int main() {
  string S;
  cin >> S;
  int N = S.length();
  map<int, int> Mods;

  mll before = S[N - 1] - '0', now;
  Mods[before.x]++;
  mll ten = 1;
  for (int i = 1; i < N; i++) {
    ten *= 10;
    now = before + ten * (S[N - i - 1] - '0');
    Mods[now.x]++;
    before = now;
  }

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


Error: TLE

Fixed submission:

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

int solve(string S, vector<int> &memo) {
  int ans = 0;
  for (int i = 0; i < (int)S.length() - 4; i++) {
    string sub = S.substr(i, 5);
    int k = stoi(sub);
    if (k % 2019 == 0)
      ans++;
    k = k % 2019;
    for (int j = 5; i + j < (int)S.length(); j++) {
      k = k * 10 + (S[i + j] - '0');
      if (k % 2019 == 0)
        ans++;
      k = k % 2019;
    }
    memo[i] = k;
  }
  for (int i = max(0, (int)S.length() - 4); i < S.length(); i++) {
    memo[i] = stoi(S.substr(i, S.length() - i));
  }
  return ans;
}

int main() {
  string S;
  cin >> S;
  string sub = S.substr(0, S.length() / 2),
         sub2 = S.substr(S.length() / 2, S.length() - sub.length());
  int n = sub.length(), m = sub2.length();
  vector<int> A(n), B(m);
  int ans = solve(sub, A) + solve(sub2, B);
  for (int a : A) {
    for (int j = 0; j < m; j++) {
      a = a * 10 + (sub2[j] - '0');
      if (a % 2019 == 0)
        ans++;
      a = a % 2019;
    }
  }
  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 (int i = 0; i < (n); ++i)
#define INF (1LL << 50)
#define MOD 1000000007

int main() {
  string S;
  cin >> S;
  vector<int> v(S.size() + 1);
  int num = 1;
  for (int i = 1; i <= S.size(); i++) {
    if (i != 1) {
      num *= 10;
      num %= 2019;
    }
    v[i] = v[i - 1] + num * (S[S.size() - i] - '0');
    v[i] %= 2019;
  }
  map<int, int> mp;
  for (int i = 0; i <= S.size(); i++) {
    mp[v[i]]++;
  }
  ll ans = 0;
  for (int i = 0; i <= S.size(); i++) {
    ans += mp[v[i]] * (mp[v[i]] - 1) / 2;
    mp[v[i]] = 0;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  vector<int> v(S.size() + 1);
  for (int i = 1; i <= S.size(); i++) {
    int num = 1;
    for (int j = 1; j < i; j++) {
      num *= 10;
      num %= 2019;
    }
    v[i] = v[i - 1] + num * (S[S.size() - i] - '0');
    v[i] %= 2019;
  }
  map<int, int> mp;
  for (int i = 0; i <= S.size(); i++) {
    mp[v[i]]++;
  }
  ll ans = 0;
  for (int i = 0; i <= S.size(); i++) {
    ans += mp[v[i]] * (mp[v[i]] - 1) / 2;
    mp[v[i]] = 0;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iomanip>
#include <iostream>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int16_t> X;

  for (int idx = S.size() - 1; idx >= 0; --idx) {
    X.push_back(S[idx] - '0');
  }

  vector<int> DP = {0};

  DP.push_back(X[0]);

  vector<int> tens;
  tens.push_back(1);
  for (int idx = 1; idx < X.size(); ++idx) {
    tens.push_back((tens[idx - 1] * 10) % 2019);
  }

  for (int ii = 1; ii < X.size(); ++ii) {
    int jj = ii;
    int temp = X[ii] * tens[ii];
    DP.push_back((DP.back() + temp) % 2019);
  }

  vector<int> mods(2019, 0);

  for (auto dp : DP) {
    ++mods[dp];
  }

  uint64_t ans = 0;
  for (auto mod : mods) {
    if (mod > 1)
      ans += mod * (mod - 1) / 2;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  string S;
  cin >> S;
  vector<int16_t> X;

  for (int idx = S.size() - 1; idx >= 0; --idx) {
    X.push_back(S[idx] - '0');
  }

  uint64_t ans = 0;

  vector<int> DP = {0};

  DP.push_back(X[0]);

  for (int ii = 1; ii < X.size(); ++ii) {
    int jj = ii;
    int temp = X[ii];
    while (jj--) {
      temp *= 10;
      if (temp >= 2019)
        temp %= 2019;
    }
    DP.push_back((DP.back() + temp) % 2019);
  }

  vector<int> mods(2019, 0);

  for (auto dp : DP) {
    ++mods[dp];
  }

  for (auto mod : mods) {
    if (mod > 1)
      ans += mod * (mod - 1) / 2;
  }

  cerr << endl;

  cout << ans << endl;

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

// ABC 164 D
#include <bits/stdc++.h>
using namespace std;
int k[2020], f[2000005], a[2000005], g[2000005];
long long sum;
int main() {
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    a[i + 1] = s[i] - '0';
  }
  int l = 1;
  for (int i = n; i >= 1; i--) {
    g[i] = l;
    l *= 10;
    l %= 2019;
  }
  for (int i = n; i >= 1; i--) {
    f[i] = (f[i + 1] + a[i] * g[i]) % 2019;
    k[f[i]]++;
  }
  sum = k[0];
  sum += (k[0] * (k[0] - 1) / 2);
  for (int i = 1; i <= 2018; i++) {
    sum += (k[i] * (k[i] - 1) / 2);
  }
  cout << sum;
  return 0;
}

Error: -11

Fixed submission:

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

int k[2020], g[2000005], f[2000005], a[2000005];
long long sum;

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

  for (int i = 0; i < n; i++)
    a[i + 1] = s[i] - '0';

  int l = 1;
  for (int i = n; i >= 1; i++) {
    g[i] = l;
    l *= 10;
    l %= 2019;
  }

  for (int i = n; i >= 1; i++) {
    f[i] = (f[i + 1] + a[i] * g[i]) % 2019;
    k[f[i]]++;
  }

  sum = k[0];
  sum += (k[0] * (k[0] - 1) / 2);
  for (int i = 1; i <= 2018; i++)
    sum += (k[i] * (k[i] - 1) / 2);

  cout << sum;

  //	system("pause");
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[2][2019];
int mod = 2019;
int main() {
  int res = 0;
  string S;
  cin >> S;
  // cout<<"hi"<<endl;
  for (int i = 0; i < S.size(); i++) {
    int m = S[i] - '0';
    for (int j = 0; j < 2019; j++) {
      dp[1][(j * 10 + m) % mod] += dp[0][j];
      // if(dp[1][j]>0)cout<<i<<" "<<j<<" "<<dp[1][j]<<endl;
    }
    dp[1][m] += 1;
    res += dp[1][0];
    // if(dp[i+1][0]>0)cout<<i<<" "<<dp[i+1][0]<<endl;
    for (int j = 0; j < 2019; j++) {
      dp[0][j] = dp[1][j];
      dp[1][j] = 0;
    }
  }
  cout << res << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[200010][2019];
int mod = 2019;
int main() {
  int res = 0;
  string S;
  cin >> S;

  // cout<<"hi"<<endl;
  for (int i = 0; i < S.size(); i++) {
    int m = S[i] - '0';
    for (int j = 0; j < 2019; j++) {
      dp[i + 1][(j * 10 + m) % mod] += dp[i][j];
      // if(dp[i][j]>0)cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    }
    dp[i + 1][m] += 1;
    res += dp[i + 1][0];
    // if(dp[i+1][0]>0)cout<<i<<" "<<dp[i+1][0]<<endl;
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

#define PB push_back
#define MP make_pair
#define F first
#define S second

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

int main() {
  ll dp[2][2100];
  string s;
  cin >> s;
  for (int i = 0; i < 2100; i++) {
    dp[0][i] = 0;
  }
  dp[0][(s[0] - '0')] = 1;
  ll ans = 0;
  for (ll i = 1; i < (ll)s.size(); i++) {
    for (ll j = 0; j < 2019; j++) {
      dp[i % 2][(j * 10 + (s[i] - '0')) % 2019] = dp[(i - 1) % 2][j];
    }
    dp[i % 2][s[i] - '0']++;
    ans += dp[i % 2][0];
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

#define PB push_back
#define MP make_pair
#define F first
#define S second

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

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

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

int dp[2019] = {0};

int main() {
  string S;
  cin >> S;
  long long mod = 0;
  long long d = 1;
  dp[0] = 1;
  int ans = 0;
  for (int i = S.size() - 1; i >= 0; i--) {
    mod = ((S[i] - '0') * d + mod) % 2019;
    ans += dp[mod]++;
    d = (d * 10) % 2019;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  set<int> st;
  int ans = 0;
  for (int i = 0; i < S.length(); i++) {
    long mod = stoll(S.substr(i, S.length() - i)) % 2019;
    if (mod == 0)
      ans++;
    else {
      int t = st.size();
      st.insert(mod);
      if (t == st.size())
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int pow_mod(int a, int b, int c) { // a < 10^5
  if (b == 0)
    return 1;
  else if (b % 2 == 1)
    return a * pow_mod(a, b - 1, c) % c;
  else {
    int d = pow_mod(a, b / 2, c);
    return d * d % c;
  }
}

int main() {
  string S;
  cin >> S;
  vector<int> intS;
  for (char c : S)
    intS.push_back(c - 48);
  reverse(intS.begin(), intS.end());
  int Ssize = intS.size();
  vector<int> dp(Ssize);
  vector<int> rem(2019, 0);
  rem.at(0)++;
  dp.at(0) = intS.at(0);
  rem.at(intS.at(0))++;
  for (int i = 1; i < Ssize; i++) {
    int work = intS.at(i) * pow_mod(10, i, 2019);
    work = (work + dp.at(i - 1)) % 2019;
    dp.at(i) = work;
    rem.at(work)++;
  }
  int64_t cnt = 0;
  for (int i = 0; i < 2019; i++) {
    int r = rem.at(i);
    if (r != 0)
      cnt += r * (r - 1) / 2;
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

int pow_mod(int a, int b, int c) { // a < 10^5
  if (b == 0)
    return 1;
  else
    return a * pow_mod(a, b - 1, c) % c;
}

int main() {
  string S;
  cin >> S;
  vector<int> intS;
  for (char c : S)
    intS.push_back(c - 48);
  reverse(intS.begin(), intS.end());
  int Ssize = intS.size();
  vector<int> dp(Ssize);
  vector<int> rem(2019, 0);
  rem.at(0)++;
  dp.at(0) = intS.at(0);
  rem.at(intS.at(0))++;
  for (int i = 1; i < Ssize; i++) {
    int work = intS.at(i) * pow_mod(10, i, 2019);
    work = (work + dp.at(i - 1)) % 2019;
    dp.at(i) = work;
    rem.at(work)++;
  }
  int64_t cnt = 0;
  for (int i = 0; i < 2019; i++) {
    int r = rem.at(i);
    if (r != 0)
      cnt += r * (r - 1) / 2;
  }
  cout << cnt << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
ll ans = 0;
vector<vector<int>> dp(2, vector<int>(2020, 0));
const int m = 2019;
int S;
void Main() {
  string s;
  cin >> s;
  int n = s.size();
  For(i, 0, n) {
    S = s[i] - '0';
    ans += dp[i % 2][0];
    For(j, 0, m) { dp[(i + 1) % 2][(j * 10 + S) % m] = dp[i % 2][j]; }
    dp[(i + 1) % 2][S] += 1;
  }
  ans += dp[n % 2][0];
  cout << ans << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
ll ans = 0;
int dp[200020][2020] = {};
int S[200020];
const int m = 2019;
void Main() {
  string s;
  cin >> s;
  int n = s.size();
  For(i, 0, n) S[i] = (s[i] - '0');
  For(i, 0, n) {
    ans += dp[i][0];
    For(j, 0, m) { dp[i + 1][(j * 10 + S[i]) % m] += dp[i][j]; }
    dp[i + 1][S[i]] += 1;
  }
  ans += dp[n][0];
  cout << ans << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

// codeforces
//
#include "bits/stdc++.h"
using namespace std;
int main() {
  char arr[200001];
  cin >> arr;

  long long int len = strlen(arr), nums[2020] = {1}, ans = 0;
  ;
  int mods[len], dp[200005] = {0};
  ;

  mods[len - 1] = 1, mods[len - 2] = 10;

  for (int i = len - 3; i >= 0; i--) {
    mods[i] = (mods[i + 1] * 10) % 2019;
  }

  for (int i = len - 1; i >= 0; i--) {
    dp[i] = (dp[i + 1] + ((arr[i] - '0') * mods[i])) % 2019;
    nums[dp[i]]++;
  }

  for (int i = 0; i < 2020; i++) {
    ans += (nums[i] * (nums[i] - 1)) / 2;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

// codeforces
//
#include "bits/stdc++.h"
using namespace std;
using int64 = long long;
int main() {
  string s;
  cin >> s;
  if (s.size() < 4)
    cout << 0;
  else {
    int n = s.size();
    // int64 rs=0;
    int t = 0;
    for (int i = 0; i < n; i++) {
      int r = 0;
      for (int j = i; j < n; j++) {
        r = r * 10 + s[j] - '0';
        if (r % 2019 == 0)
          t++;
        r %= 2019;
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  long long int L = s.size();
  long long int n = 0;
  long long int p = 1;
  int d[2019] = {0};
  long long int cnt = 0;
  d[0]++;
  for (int i = L - 1; i >= 0; i--) {
    n = (n + (s[i] - '0') * p) % 2019;
    p = (p * 10) % 2019;
    cnt += d[n];
    d[n]++;
  }

  cout << cnt << endl;
}


Error: TLE

Fixed submission:

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

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

  long long int L = s.size();

  long long int n = 0;
  long long int p = 1;
  int POW[L];
  long long int cnt = 0;
  POW[0] = 1;
  for (int i = 1; i < L; i++) {
    p = (p * 10) % 2019;
    POW[i] = (int)p;
  }

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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define db(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\\n'
const int N = 1e6;

int main() {
  ios_base::sync_with_stdio(0);
  int a, b, n, T, cas = 0;
  string s;
  cin >> s;
  map<int, int> mp;
  mp[0] = 1;
  int r = 0, pow10 = 1;
  ll res = 0;
  for (int i = s.size() - 1; ~i; --i) {
    r = (r + (s[i] - '0') * pow10) % 2019;
    res += mp[r];
    mp[r]++;
    pow10 = (pow10 * 10) % 2019;
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string num;
int main() {
  cin >> num;
  int x, ans = 0;
  int n = num.size();
  // cout<<num.size()<<endl;
  for (int i = 0; i < n - 4; i++) {
    x = num[i] - '0';
    for (int j = i + 1; j < n; j++) {
      x = 10 * x + num[j] - '0';
      if (x >= 2019)
        x %= 2019;
      if (x == 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}