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

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

#define ll long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
ll mod = 2019;
ll dp[2019];
ll waru[200010];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  string s;
  cin >> s;
  ll n = s.size();
  ll locate = 0;
  waru[0] = 1;
  rep(i, 1, 200010) { waru[i] = (waru[i - 1] * 10) % mod; }
  rel(i, n, 0) {
    ll tmp = s[i] - '0';
    (tmp *= waru[n - i]) %= mod;
    (locate += tmp) %= mod;
    dp[locate]++;
    // cout << locate << endl;
  }
  ll ans = 0;
  ans += dp[0];
  rep(i, 0, 2019) { ans += dp[i] * (dp[i] - 1) / 2; }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define ll long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define rel(i, x, y) for (int i = x - 1; i >= y; i--)
#define all(x) x.begin(), x.end()
ll mod = 2019;
ll dp[2019];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  string s;
  cin >> s;
  ll n = s.size();
  ll locate = 0;
  rel(i, n, 0) {
    int tmp = s[i] - '0';
    rep(j, 0, n - i)(tmp *= 10) %= mod;
    (locate += tmp) %= mod;
    dp[locate]++;
    // cout << locate << endl;
  }
  ll ans = 0;
  ans += dp[0];
  rep(i, 0, 2019) { ans += dp[i] * (dp[i] - 1) / 2; }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
constexpr ll MOD = 1000000007;
constexpr ll T = 2019;

int main(int argc, char **argv) {
  string str;
  cin >> str;

  ll N = (ll)str.size();
  vector<ll> val(2020, 0);
  ll tenfactor = 1;
  ll cur = 0;
  ++val[cur];
  for (ll i = 0; i < N; ++i) {
    (cur += (str[N - 1 - i] - '0') * tenfactor) %= 2019;
    (tenfactor *= 10) %= 2019;
    ++val[cur];
  }

  ll res{0};
  for (ll i = 0; i < 2019; ++i) {
    res += val[i] * (val[i] - 1) / 2;
  }

  std::cout << res << std::endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
constexpr ll MOD = 1000000007;
constexpr ll T = 2019;

int main(int argc, char **argv) {
  string str;
  cin >> str;

  ll res{0};
  for (ll left = 0; left < (ll)str.size(); ++left) {
    for (ll right = left + 1; right <= (ll)str.size(); ++right) {
      ll sub = stoull(str.substr(left, right - left));
      if (sub % T == 0) {
        // std::cout << str.substr(left, right-left)<< std::endl;
        ++res;
      }
    }
  }
  std::cout << res << std::endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  string s;
  cin >> s;

  int pot = 1;
  map<int, int> cnt;
  cnt[0] = 1;
  long long ans = 0;
  int rem = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    rem += (s[i] - '0') * pot;
    rem %= 2019;
    ans += cnt[rem]++;
    pot *= 10;
    pot %= 2019;
  }

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

  // 42
  //
}

Error: TLE

Fixed submission:

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

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

  string s;
  cin >> s;

  int n = s.length();
  long long cnt = 0;
  set<pair<int, int>> set;
  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    cnt = 0;
    for (int j = i; j >= 0; j--) {
      // long long k = cnt * 10 + (s[j] - '0');
      long long k = (s[j] - '0') * pow(10, i - j + 1);
      cnt += k;
      if (cnt % 2019 != 0)
        continue;
      ans++;
      // cout << i << " " << j << "\\n";
      // set.insert({j, i});
      // ans++;
    }
  }

  // cout << set.size() << "\\n";
  cout << ans << "\\n";

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

#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

long long ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}

int main() {
  char a[2000000];

  scanf("%s", a);

  // printf("%s\\n", a);

  int amari[2019];
  long long length = strlen(a);
  long long ten = 1;

  // printf("%lld\\n", length);

  long long T[length + 1];
  T[length] = 0;

  for (long long i = length; i > 0; i--) {
    // printf("%c\\n", a[i - 1]);
    // ten = (long long)  pow(10, length - i);
    T[i - 1] = ten * ctoi(a[i - 1]) + T[i];
    // printf(" : %f\\n", pow(10, length - i));
    T[i - 1] = T[i - 1] % 2019;
    ten = ten * 10;
    ten = ten % 2019;

    // printf("%lld : %lld\\n", i, T[i-1]);
    // printf("%lld\\n", ten);
  }

  /*for(long long i = 0; i <= length; i++){
      T[i] = T[i] % 2019;
  }*/

  /*for(long i = 0; i <= length; i++){
      printf("%ld : %lld\\n", i, T[i]);
  }*/

  for (int j = 0; j < 2019; j++) {
    amari[j] = 0;
  }

  for (long long i = 0; i <= length; i++) {
    amari[T[i]]++;
  }

  for (int j = 0; j < 2019; j++) {
    if (amari[j] > 0) {
      amari[j] = amari[j] * (amari[j] - 1) / 2;
    }
  }

  int count = 0;

  for (int j = 0; j < 2019; j++) {
    count = count + amari[j];
  }

  printf("%d\\n", count);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

int ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}

int main() {
  char a[2000000];

  scanf("%s", a);

  int amari[2019];
  long length = strlen(a);

  long T[length + 1];
  T[length] = 0;

  for (long i = length; i > 0; i--) {
    T[i - 1] = pow(10, length - i) * ctoi(a[i - 1]) + T[i];
  }

  for (long i = 0; i <= length; i++) {
    T[i] = T[i] % 2019;
  }

  /*for(long i = 0; i <= length; i++){
      printf("%ld : %ld\\n", i, T[i]);
  }*/

  for (int j = 0; j < 2019; j++) {
    amari[j] = 0;
  }

  for (long i = 0; i <= length; i++) {
    amari[T[i]]++;
  }

  for (int j = 0; j < 2019; j++) {
    if (amari[j] > 0) {
      amari[j] = amari[j] * (amari[j] - 1) / 2;
    }
  }

  int count = 0;

  for (int j = 0; j < 2019; j++) {
    count = count + amari[j];
  }

  printf("%d\\n", count);

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

#include <bits/stdc++.h>
#define endl "\\n"
#define ll long long
using namespace std;

int main() {
  string s;
  cin >> s;
  int sum = 0;
  int count = 0;
  unordered_map<int, int> mp;
  mp[0] = 1;

  int base = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    sum = (sum + (s[i] - '0') * base) % 2019;
    base = (base * 10) % 2019;
    mp[sum]++;
  }

  for (auto it = mp.begin(); it != mp.end(); it++) {
    count = count + ((it->second) * (it->second - 1)) / 2;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define ll long long
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  string s;
  cin >> s;
  int sum = 0;
  int count = 0;
  for (int i = 0; i < s.length(); i++) {
    sum = 0;
    for (int j = i; j < s.length(); j++) {
      sum = (((sum * 10) % 2019) + ((s[j] - '0'))) % 2019;
      if (sum == 0)
        count++;
    }
  }
  cout << count << endl;
}
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();
  reverse(s.begin(), s.end());

  vector<int> v(n, 0);
  int x = 1;

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

  int ans = 0;
  vector<int> count(2100, 0);
  count[0] = 1;

  for (int i = 0; i < n; i++) {
    if (count[v[i]] == 0) {
      count[v[i]] = 1;
    } else {
      ans += count[v[i]];
      count[v[i]]++;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

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

  vector<string> v(n);
  for (int i = 0; i < n; i++) {
    if (i == 0)
      v[i] = s[i];
    else
      v[i] = v[i - 1] + s[i];
  }

  int ans = 0;
  for (int i = 3; i < n; i++) {
    for (int j = 0; i + 1 - j > 3; j++) {
      string word = v[i];
      long long num = stoll(v[i].erase(0, j));
      if (num % 2019 == 0)
        ans++;
      v[i] = word;
    }
  }

  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;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
const int MOD = 1000000007;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  if (n < 4) {
    cout << 0 << endl;
    return 0;
  }
  int keta = 1, bnum = 0;
  unordered_map<int, ll> mp;
  per(i, n) {
    int c = s[i] - '0';
    bnum += c * keta % 2019;
    bnum %= 2019;
    keta = keta * 10 % 2019;
    mp[bnum]++;
  }
  ll ret = 0;
  for (auto &p : mp) {
    ll c = p.second;
    ret += c * (c - 1) / 2;
    if (p.first == 0)
      ret += c;
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
const int MOD = 1000000007;

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

  vector<string> buf;
  rep(i, 9999) {
    string is = to_string(2019 * (i + 1));
    buf.emplace_back(is);
  }
  vector<int> check(s.size(), 0);
  for (auto &is : buf) {
    int n = is.size();
    rep(i, s.size() - n + 1) {
      if (s.compare(i, n, is) == 0) {
        check[i] = n;
      }
    }
  }
  ll ret = 0;
  int n = check.size();
  rep(i, n) {
    int c = check[i];
    if (c) {
      ll cnt = 1;
      c += i;
      while (c < n && check[c] > 0) {
        int tc = check[c];
        check[c] = 0;
        c += tc;
      }
      ret += cnt * (cnt + 1) / 2;
    }
  }
  cout << ret << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define each(i, mp) for (auto &i : mp)

vector<int> A;

int main() {
  string s;
  cin >> s;
  map<ll, ll> mp;
  mp[0] = 1;
  reverse(s.begin(), s.end());
  ll now = 0, ten = 1;
  each(e, s) {
    now = (now + (e - '0') * ten) % 2019;
    ten *= 10;
    ten %= 2019;
    mp[now]++;
  }
  ll ans = 0;
  each(e, mp) { ans += e.second * (e.second - 1) / 2; }

  cout << ans << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int count = 0;
  for (int i = 0; i < S.size() - 3; i++) {
    for (int j = i + 3; j < S.size(); j++) {
      string tmp_s(S, i, j);

      if (stoll(tmp_s) % 2019 == 0) {
        count++;
      }
    }
  }

  cout << count;
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

int dp[2][2019];
int main() {
  std::string S;
  std::cin >> S;
  int n = S.length();
  int value = int(S[0]) - int('0');
  dp[0][value] = 1;
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    int value = int(S[i]) - int('0');
    dp[i % 2][value] = 1;
    for (int j = 0; j < 2019; j++) {
      if (dp[(i + 1) % 2][j] != 0) {
        dp[i % 2][(j * 10 + value) % 2019] += dp[(i + 1) % 2][j];
      }
    }
    ans += dp[i % 2][0];
    for (int j = 0; j < 2019; j++) {
      dp[(i + 1) % 2][j] = 0;
    }
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

int dp[200004][2019];
int main() {
  std::string S;
  std::cin >> S;
  int n = S.length();
  int value = int(S[0]) - int('0');
  dp[0][value] = 1;
  for (int i = 1; i < n; i++) {
    int value = int(S[i]) - int('0');
    dp[i][value] = 1;
    for (int j = 0; j < 2019; j++) {
      if (dp[i - 1][j] != 0) {
        dp[i][(j * 10 + value) % 2019] += dp[i - 1][j];
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += dp[i][0];
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

//  Copyright © 2020 Diego Garcia Rodriguez del Campo. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define pii pair<ll, ll>
#define fi first
#define se second
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define forn(i, a, b) for (ll i = a; i <= b; i++)
#define mod(n, k) (((n % k) + k) % k)
using namespace std;
typedef long long ll;

ll N, ans;
string s;
ll dp[2][2040];

/*
ll dp( ll x , ll m ) {
    if( x == N )
        return m == 0;
    if( vis[{ x , m }] )
        return mp[{x , m }];
    vis[{x , m}] = 1;
    return mp[{x , m}] = dp( x + 1 , ( 10 * m + s[x] - '0' ) % 2019 ) + ( m == 0
);
}
*/

int main() {
  optimiza_io cin >> s;
  N = s.size();
  ll act = 0, nxt = 1;
  dp[act][0] = 1;
  for (ll x = N - 1; x >= 0; x--) {
    swap(act, nxt);
    for (ll m = 0; m < 2019; m++)
      dp[act][m] = dp[nxt][(10 * m + s[x] - '0') % 2019] + (m == 0);
    ans += dp[nxt][s[x] - '0'];
  }

  /* for( ll i = 0; i < N; i ++ )
      ans += dp( i + 1 , s[i] - '0' );
      */

  cout << ans;
  return 0;
}

// CHECAR LIMITES

Error: TLE

Fixed submission:

//  Copyright © 2020 Diego Garcia Rodriguez del Campo. All rights reserved.
#include <bits/stdc++.h>
#define MAX 200005
#define pii pair<int, int>
#define fi first
#define se second
#define optimiza_io                                                            \\
  cin.tie(0);                                                                  \\
  ios_base::sync_with_stdio(0);
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define mod(n, k) (((n % k) + k) % k)
using namespace std;
typedef long long ll;

ll N, ans;
string s;
map<pii, bool> vis;
map<pii, int> mp;

ll dp(int x, int m) {
  if (x == N)
    return m == 0;
  if (vis[{x, m}])
    return mp[{x, m}];
  vis[{x, m}] = 1;
  return mp[{x, m}] = dp(x + 1, (10 * m + s[x] - '0') % 2019) + (m == 0);
}

int main() {
  optimiza_io cin >> s;
  N = s.size();
  for (int i = 0; i < N; i++)
    ans += dp(i + 1, s[i] - '0');
  cout << ans;
  return 0;
}

// CHECAR LIMITES
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#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>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int p = 2019;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  vector<int> d(n + 1);
  int ten = 1;
  for (int i = n - 1; i >= 0; i--) {
    int a = (s[i] - '0') * ten % p;
    d[i] = (d[i + 1] + a) % p;
    ten *= 10;
    ten %= p;
  }
  vector<int> cnt(p);
  ll ans = 0;
  for (int i = n; i >= 0; i--) {
    ans += cnt[d[i]];
    cnt[d[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int num = 0, dig = 1;
      for (int k = j; k >= i; k--) {
        num += (s[k] - '0') * dig;
        dig *= 10;
      }
      if (num % 2019 == 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
int c[2019];
ll bpow(ll b, ll p, ll m) {
  ll rt = 1;
  for (; p; b = b * b % m, p >>= 1) {
    if (p & 1)
      rt = rt * b % m;
  }
  return rt;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  ll rem = 0;
  ll ans = 0;
  for (int i = 0; i < s.length(); i++) {
    rem = (rem + (s[i] - '0') * bpow(10, i, 2019)) % 2019;
    c[rem]++;
  }
  for (int i = 0; i < 2019; i++) {
    if (c[i]) {
      ans += 1LL * (c[i]) * (c[i] - 1) / 2;
    }
  }
  cout << ans + c[0] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<ll, ll>;
set<int> dp[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string s;
  cin >> s;
  int ans = 0;
  for (int i = 0; i < s.length(); i++) {
    int rem = 0;
    int f = 0;
    for (int j = i; j < s.length(); j++) {
      rem = (rem * 10 + s[j] - '0') % 2019;
      if (dp[j].find(rem) != dp[j].end())
        break;
      dp[j].insert(rem);
      if (!rem)
        f++;
    }
    ans += f * (f + 1) / 2;
  }
  cout << ans << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  reverse(s.begin(), s.end());
  vector<int64_t> amari(s.size() + 1);
  vector<int64_t> matome(2019);

  int64_t temp = 1;  // 10^xのmod2019
  matome.at(0) += 1; // 一番下の位の分
  for (int64_t i = 0; i < s.size(); i++) {
    int64_t num = s[i] - '0';
    if (i == 0) {
      amari.at(i) = num;
    } else {
      amari.at(i) = (amari.at(i - 1) + temp * num) % 2019;
    }
    matome.at(amari.at(i)) += 1;
    temp = (temp * 10) % 2019;
  }

  int64_t ans = 0;
  for (int i = 0; i < 2019; i++) {
    int64_t num = matome.at(i);
    ans += num * (num - 1) / 2;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  vector<vector<pair<int, int>>> dp(200000);
  for (int i = 0; i <= 2020; i++) {
    dp.at(0).push_back(make_pair(0, 0));
  }
  for (int i = 0; i < s.size(); i++) {
    int num = s[i] - '0';
    if (i == 0) {
      dp.at(0).at(num) = make_pair(1, 1);
      continue;
    }
    for (int j = 0; j <= 2019; j++) {
      dp.at(i).push_back(make_pair(0, 0));
      dp.at(i).at(j) = make_pair(dp.at(i - 1).at(j).first, 0);
    }
    for (int j = 0; j <= 2019; j++) {
      int index_new = (j * 10 + num) % 2019;
      dp.at(i).at(index_new).second += dp.at(i - 1).at(j).second;
      dp.at(i).at(index_new).first += dp.at(i - 1).at(j).second;
    }
    dp.at(i).at(num).second += 1;
  }

  cout << dp.at(s.size() - 1).at(0).first << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int csum = 0, ans = 0;
  int N, P;
  string S;
  vector<int> con;

  P = 2019;
  cin >> S;

  N = S.size();

  if (10 % P) {
    con.resize(P);

    con[0] = 1;

    for (int i = S.size() - 1, ten = 1 % P; i >= 0; i--) {
      csum = csum + ten * (S[i] - '0') % P;
      csum %= P;

      ans += con[csum];

      con[csum]++;
      ten *= 10;
      ten %= P;
    }
  } else {
    for (int i = 0; i < S.size(); i++) {
      if ((S[i] - '0') % P == 0) {
        ans += i + 1;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  constexpr int num = 2019;
  string S;
  int ans = 0;
  vector<vector<int>> r;

  cin >> S;

  r.resize(S.size() + 1, vector<int>(num));

  for (int i = 0; i < S.size(); i++) {
    r[i + 1][S[i] - '0']++;

    for (int j = 0; j < num; j++) {
      int x = (j * 10 + S[i] - '0') % num;
      r[i + 1][x] += r[i][j];
    }

    ans += r[i + 1][0];
  }

  cout << ans << endl;
  // cout<<r[S.size()][0]<<endl;

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

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

typedef long long ll;

int main() {
  string s;
  cin >> s;
  vector<ll> x(2019, 0);
  x[0] += 1;
  ll k = 0;
  ll p = 1;
  for (int i = 1; i <= s.size(); i++) {
    k += ((ll)(s[s.size() - i] - '0')) * p % 2019;
    k %= 2019;
    x[k % 2019] += 1;
    p *= 10;
    p %= 2019;
  }
  ll ans = 0;
  for (auto iter = x.begin(); iter != x.end(); iter++) {
    if (*iter > 0) {
      ans += (*iter) * (*iter - 1) / 2;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;

int main() {
  string s;
  cin >> s;
  vector<ll> x(2019, 0);
  x[0] += 1;
  ll k = 0;
  ll p = 1;
  for (int i = 1; i <= s.size(); i++) {
    k += ((ll)(s[s.size() - i] - '0')) * p % 2019;
    k %= 2019;
    x[k % 2019] += 1;
    p *= 10;
  }
  ll ans = 0;
  for (auto iter = x.begin(); iter != x.end(); iter++) {
    if (*iter > 0) {
      ans += (*iter) * (*iter - 1) / 2;
    }
  }
  cout << ans << endl;
}