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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll INF = 1000000000000;
const ll mod = 1000000007;
ll test[12345678], res[12345678], aa[1234][1234], bb[1234][1234];
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
char maze[1234][1234];
signed main() {
  ll n, m, k, sum = 0, cnt = 1, ans = 0;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  test[0] = 1;
  for (int h = 0; h < s.size(); h++) {
    sum = ((ll)(s[h] - '0') * cnt + sum) % 2019;
    test[sum]++; // cout << sum << endl;
    cnt *= 10;
    cnt %= 2019;
  }
  for (int h = 0; h < 2019; h++) {
    ans += test[h] * (test[h] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int test[12345678], cost[12345678];
signed main() {
  int n, ans = 0;
  string s;
  cin >> s;
  test[0] = ((s[0] - '0') * 100) + ((s[1] - '0') * 10) + (s[2] - '0');
  test[1] = ((s[1] - '0') * 10) + (s[2] - '0');
  test[2] = (s[2] - '0');
  for (int h = 3; h < s.size(); h++) {
    for (int i = 0; i <= h; i++) {
      test[i] = test[i] * 10 + (s[h] - '0');
      if (test[i] != 0) {
        if ((test[i] * 10000) % 2019 == 0) {
          ans++;
        }
      }
      test[i] %= 2019;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <limits>
#include <string>

using namespace std;

int main() {
  string S;
  cin >> S;
  int N = S.length();
  int T[N + 1] = {};
  long long m[2019] = {};
  m[0] = 1;
  T[N] = 0;
  int ten = 1;
  for (int i = N - 1; i >= 0; --i) {
    int ai = (int)(S[i] - '0');
    // cout << ai << endl;
    // cout << i << " " << S[i] << endl;
    if (ai > 9 || ai < 0) {
      cout << "ai range error" << endl;
      return -1;
    }
    T[i] = T[i + 1] + ten * ai;
    T[i] %= 2019;
    ten *= 10;
    ten %= 2019;
    // cout << T[i+1] << " " << ai << " " << T[i] << endl;
    m[T[i]]++;
  }
  int ans = 0;
  for (int k = 0; k < 2019; ++k) {
    ans += m[k] * (m[k] - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}

// int main(){
//     string S;
//     cin >> S;
//     //cout << S << endl;
//     int keta = S.length();
//     //cout << keta <<endl;
//     int i = 0;
//     int j = 0;
//     int count = 0;
//     for(j=keta-1; j>=0; --j){
//         i = j-3;
//         while(i>=0){
//             long long tmp;
//             string tmpp;
//             //cout << "i" << i <<"j" << j << endl;
//             int len = j - i + 1;
//             tmpp = S.substr(i, len);
//             //cout << tmpp << "tmpp" << endl;
//             tmp = stoll(tmpp);
//             //cout << tmp << "tmp" << endl;
//             if(tmp%2019==0){
//                 count++;
//                 i = i-4;
//             }
//             else{
//                 i--;
//             }
//
//         }
//     }
//     cout << count << endl;
//
//     return 0;
// }

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <limits>
#include <string>

using namespace std;

int main() {
  string S;
  cin >> S;
  // cout << S << endl;
  int keta = S.length();
  // cout << keta <<endl;
  int i = 0;
  int j = 0;
  int count = 0;
  for (j = keta - 1; j >= 0; --j) {
    i = j - 3;
    while (i >= 0) {
      long long tmp;
      string tmpp;
      // cout << "i" << i <<"j" << j << endl;
      int len = j - i + 1;
      tmpp = S.substr(i, len);
      // cout << tmpp << "tmpp" << endl;
      tmp = stoll(tmpp);
      // cout << tmp << "tmp" << endl;
      if (tmp % 2019 == 0) {
        count++;
        i = i - 4;
      } else {
        i--;
      }
    }
  }
  cout << count << endl;

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

#include <bits/stdc++.h>
#define endl '\\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

string s;
void read() { cin >> s; }

int dp[2019], nxt_dp[2019];
void solve() {
  int sz = s.size();
  dp[s[0] - '0']++;
  long long ans = dp[0];
  for (int i = 2; i <= sz; i++) {
    memset(nxt_dp, 0, sizeof(nxt_dp));
    for (int j = 0; j < 2019; j++)
      nxt_dp[(j * 10 + s[i - 1] - '0') % 2019] += dp[j];

    nxt_dp[s[i - 1] - '0']++;
    for (int j = 0; j < 2019; j++)
      dp[j] = nxt_dp[j];

    ans += dp[0];
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  read();
  solve();
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

string s;
void read() { cin >> s; }

int base[200003];
int dp[200003][2019];
void solve() {
  int sz = s.size();
  dp[1][s[0] - '0']++;
  for (int i = 2; i <= sz; i++) {
    for (int j = 0; j < 2019; j++)
      dp[i][(j * 10 + s[i - 1] - '0') % 2019] += dp[i - 1][j];

    dp[i][s[i - 1] - '0']++;
  }

  long long ans = 0;
  for (int i = 1; i <= sz; i++) {
    ans += dp[i][0];
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  read();
  solve();
}
Problem p02702 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;

static const long long MOD = 1000000007;

int ans;
string s;
int dp[2][2020];

int main(void) {
  cin >> s;
  int n = sz(s);

  dp[0][s[0] - '0']++;
  for (int i = 1; i < n; i++) {
    int now = i % 2;
    int prev = (i - 1) % 2;
    int d = s[i] - '0';
    dp[now][d]++;

    for (int j = 0; j < 2019; j++) {
      int r = (j * 10 + d) % 2019;
      dp[now][r] += dp[prev][j];
    }
    ans += dp[now][0];
    for (int j = 0; j < 2019; j++)
      dp[prev][j] = 0;
  }

  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;

static const long long MOD = 1000000007;

int ans;
string s;
int dp[200005][2020];

int main(void) {
  cin >> s;
  int n = sz(s);

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

    for (int j = 0; j < 2019; j++) {
      int r = (j * 10 + d) % 2019;
      dp[i][r] += dp[i - 1][j];
    }
    ans += dp[i][0];
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;
#define rep(i, n) for (ll i = 0; i < n; i++)

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int keep[2019] = {};
  // 余りを累積和すればいける
  keep[0] = 1;
  int a[n + 1];
  a[0] = 0; // その桁が追加されるといくつ余りが増えるか
  ll md = 2019;
  int ans = 0;
  int j = 1;
  for (int i = 0; i < n; i++) { // 下から何桁取るか考える
    ll t = s[n - i - 1] - '0', tt = s[n - i - 1] - '0';
    t = t * j % md;
    a[i + 1] = (t + a[i]) % md;
    // 数え上げ
    int l = a[i + 1];
    keep[l]++;
    // cout<<keep[l]<<" "<<keep0[l]<<"\\n";

    if (tt != 0)
      ans += keep[l] - 1;
    // cout<<a[i+1]<<"\\n";
    j = (j * 10) % md;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
const double pi = 3.14159265358979323846;
#define rep(i, n) for (ll i = 0; i < n; i++)

int main() {
  string s;
  cin >> s;
  int n = s.size();
  int keep[2019] = {};
  // 余りを累積和すればいける
  keep[0] = 1;
  int a[n + 1];
  a[0] = 0; // その桁が追加されるといくつ余りが増えるか
  ll md = 2019;
  int ans = 0;
  for (int i = 0; i < n; i++) { // 下から何桁取るか考える
    ll t = s[n - i - 1] - '0', tt = s[n - i - 1] - '0';
    int j = i;
    while (j) {
      if (j > 13) {
        t *= 1e13;
        j -= 13;
      } else {
        t *= 10;
        j--;
      }
      t %= md;
    }
    a[i + 1] = (t + a[i]) % md;
    // 数え上げ
    int l = a[i + 1];
    keep[l]++;
    // cout<<keep[l]<<" "<<keep0[l]<<"\\n";

    if (tt != 0)
      ans += keep[l] - 1;
    // cout<<a[i+1]<<"\\n";
  }

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int an[200005];
int bn[3000];
int main() {
  string s;
  cin >> s;
  int r = 1;
  int ans = 0;
  const int mod = 2019;
  for (int i = s.size() - 1; i >= 0; i--) {
    int u = s[i] - '0';
    int p = (r * u % mod + an[i + 1]) % mod;
    an[i] = p;
    bn[p]++;
    r = r * 10 % mod;
  }

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

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int an[3000];
int bn[3000];
int main() {
  string s;
  cin >> s;
  int r = 1;
  int ans = 0;
  const int mod = 2019;
  for (int i = s.size() - 1; i >= 0; i--) {
    int u = s[i] - '0';
    int p = (r * u % mod + an[i + 1]) % 2019;
    an[i] = p;
    bn[p]++;
    r = r * 10 % mod;
  }

  for (int i = 0; i < 2019; i++) {
    ans += bn[i] * (bn[i] - 1) / 2;
  }
  cout << ans + bn[0];
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))

#if 1
// 後から解く
// No.4
int main() {
  // 2019 = 3 * 673
  CHAR *S = new char[200000 + 1];
  // string S;		//stringに200000文字ぶち込むとエラーが出るらしい。
  cin >> S;
  int length = strlen(S);

  map<int, int> mod2019; // value, count
  mod2019.insert(
      pair<int, int>(0, 1)); // 最後の桁まで使って2019で割れる場合のペア。

  LLONG ten = 1;
  LLONG number = 0;
  for (int i = 0; i < length; ++i) {
    number = (S[length - 1 - i] - '0') * ten + number;
    // cout << number << " % 2019 = " << number % 2019 << endl;

    int mod = number % 2019;
    if (mod2019.find(mod) == mod2019.end()) {
      // 追加
      mod2019.insert(pair<int, int>(mod, 1));
    } else {
      mod2019[mod] += 1; // カウント追加
    }

    ten *= 10;
    ten %= 2019;
  }

  LLONG count = 0;
  for (pair<int, int> mod : mod2019) {
    count += mod.second * (mod.second - 1) / 2;
  }

  cout << count;

  return 0;
}
#endif

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))

#if 1
// No.4
int main() {
  // 2019 = 3 * 673
  string S;
  cin >> S;
#if 0
	//馬鹿正直に。いつものパターンを考えると、下x桁だけを見る判定方法がある気がするが。。。
	int count = 0;
	for (int i = 0; i < S.length(); ++i) {
		LLONG number = S[i] - '0';
		for (int j = i + 1; j < S.length(); j++) {
			number = number * 10 + S[j] - '0';
			if (number % 2019 == 0) {
				count += 1;
				cout << i << "," << j << ", " << number << endl;
			}
		}
	}
#else
  // そんな馬鹿な
  int count = 0;
  for (int i = 0; i < S.length() - 4; ++i) {
    LLONG number = S[i] - '0';
    number = number * 10 + S[i + 1] - '0';
    number = number * 10 + S[i + 2] - '0';
    number = number * 10 + S[i + 3] - '0';
    if (number % 2019 == 0) {
      count += 1;
      // cout << i << "," << ", " << number << endl;
    }
    number = number * 10 + S[i + 4] - '0';

    if (number % 2019 == 0) {
      count += 1;
      // cout << i << "," << ", " << number << endl;
    }
  }
  if (S.length() >= 4) {
    int i = S.length() - 4;
    LLONG number = S[i] - '0';
    number = number * 10 + S[i + 1] - '0';
    number = number * 10 + S[i + 2] - '0';
    number = number * 10 + S[i + 3] - '0';
    if (number % 2019 == 0) {
      count += 1;
      // cout << i << "," << ", " << number << endl;
    }
  }
#endif
  cout << count;

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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  string S;
  ll N, i, j;
  cin >> S;
  N = S.size();
  ll pre[2019], now[2019];
  for (j = 0; j < 2019; j++) {
    pre[j] = 0;
    now[j] = 0;
  }
  ll ans = 0;
  ll t;
  for (i = 1; i <= N; i++) {
    t = S[i - 1] - '0';
    for (j = 0; j < 2019; j++) {
      now[(j * 10 + t) % 2019] = pre[j];
    }
    now[t] += 1;
    ans += now[0];
    for (j = 0; j < 2019; j++) {
      pre[j] = now[j];
    }
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
  string S;
  ll N, i, j;
  cin >> S;
  N = S.size();
  ll dp[N + 1][2019];
  for (i = 0; i <= N; i++) {
    for (j = 0; j < 2019; j++) {
      dp[i][j] = 0;
    }
  }
  ll ans = 0;
  ll t;
  for (i = 1; i <= N; i++) {
    t = S[i - 1] - '0';
    for (j = 0; j < 2019; j++) {
      dp[i][(j * 10 + t) % 2019] += dp[i - 1][j];
    }
    dp[i][t] += 1;
    ans += dp[i][0];
  }
  cout << ans;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string S;
  cin >> S;
  static const long long N = S.size();
  static const long long mod = 2019;
  vector<long long> num(N); // 各桁の数字(左から0番目, 1番目, ...)
  for (long long i = 0; i < N; ++i) {
    num[i] = S[i] - '0';
  }
  vector<long long> t(N + 1, 0);
  vector<long long> cnt(mod, 0);
  cnt[0] = 1;
  long long dig = 1;
  for (long long i = N - 1; i >= 0; --i) {
    t[i] = t[i + 1] + num[i] * dig;
    t[i] = (t[i] + mod) % mod;
    ++cnt[t[i]];
    dig *= 10;
    dig = (dig + mod) % mod;
  }
  long long ans = 0;
  for (long long c : cnt) {
    ans += c * (c - 1LL) / 2LL;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  string S;
  cin >> S;
  static const long long N = S.size();
  static const long long mod = 2019;
  long long left = 0;
  long long ans = 0;
  while (left < N) {
    if (left + 4 > N - 1)
      break;
    bool find = false;
    for (long long right = left + 4; right < N; ++right) {
      string sub = S.substr(left, right - left + 1);
      long long num = stoll(sub);
      long long r = num % mod;
      if (r == 0) {
        ++ans;
        find = true;
        left = right;
        break;
      }
    }
    if (find)
      continue;
    else
      ++left;
  }
  cout << ans << endl;
  return 0;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define deb(x) cout << #x << ' ' << x << '\\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \\
  int tt;                                                                      \\
  scn(tt);                                                                     \\
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 2019;
const int N = 1e6 + 2;

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

  int n = (int)s.size();

  int pwr[n];

  pwr[0] = 1;

  rep(i, 1, n) pwr[i] = (pwr[i - 1] * 10) % mod;

  vector<int> fa(mod, 0);

  fa[0] = 1;

  ll ans = 0;

  int cur = n - 1, sum = 0;

  rep(i, 0, n) {
    sum = (sum + pwr[cur] * (s[i] - '0')) % mod;

    cur--;

    ans += fa[sum];

    fa[sum]++;
  }

  lpri(ans);

  return 0;
}

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


Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define deb(x) cout << #x << ' ' << x << '\\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \\
  int tt;                                                                      \\
  scn(tt);                                                                     \\
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int N = 1e6 + 2;

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

  int n = s.size();

  vector<int> pwr(n);

  pwr[0] = 1;

  rep(i, 1, n) pwr[i] = (pwr[i - 1] * 10) % mod;

  vector<int> fa(mod, 0);

  fa[0] = 1;

  ll ans = 0;

  int cur = n - 1, sum = 0;

  rep(i, 0, n) {
    sum = (sum + pwr[cur] * (s[i] - '0')) % mod;

    cur--;

    ans += fa[sum];

    fa[sum]++;
  }

  lpri(ans);

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

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 1000000000000
#define MOD 1000000007
using ll = long long;

int main() {
  string S;
  cin >> S;
  int P = 2019, N = S.size();
  vector<ll> dp(N + 1, 0);
  ll ten = 1;
  for (int i = N - 1; i >= 0; i--) {
    ll tmp = ten * ((int)(S.at(i) - '0')) + dp.at(i + 1);
    tmp %= P;
    ten *= 10;
    ten %= P;
    dp.at(i) = tmp;
    // cout<<dp.at(i)<<endl;
  }
  sort(begin(dp), end(dp));
  map<ll, ll> m;
  for (auto i : dp) {
    if (m.count(i))
      m[i]++;
    else
      m[i] = 1;
  }
  // for(auto p: m)cout<<p.first<<" "<<p.second<<endl;
  ll res = 0;
  for (auto p : m) {
    ll cnt = p.second;
    res += (cnt - 1) * cnt / 2;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 1000000000000
#define MOD 1000000007
using ll = long long;

int main() {
  string S;
  cin >> S;
  ll L = S.size();
  ll res = 0;
  for (int s = 0; s < L; s++) { // 開始インデックス
    int i = s;
    ll num = 0;
    int flag = 0;
    while (1) {
      flag = 0;
      for (int l = 4; i + l <= L; l++) {
        ll tmp = 0;
        rep(j, l) { tmp += ((int)S.at(i + j) - '0') * pow(10.0, l - j - 1); }
        // cout<<tmp<<endl;
        if (tmp % 2019 == 0) {
          num++;
          // cout<<tmp<<endl;
          i += l;
          flag = 1;
          break;
        }
      }
      if (flag)
        continue;
      else
        break;
    }
    // cout<<num<<endl;
    res += (1 + num) * num / 2;
  }
  cout << res << endl;
}
Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int m = s.size();
  vector<int> cnt(2019);
  long long ans = 0;
  int x = 1;
  int tot = 0;

  for (int i = 0; i < m; i++) {
    cnt[tot]++;
    tot += (s[i] - '0') * x;
    tot %= 2019;
    ans += cnt[tot];
    x = x * 10 % 2019;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    int x = 1;
    int tot = 0;
    for (int j = i; j < s.size(); j++) {
      tot += (s[j] - '0') * x;
      tot = tot % 2019;
      if (tot == 0)
        ans++;
      x *= 10;
      x = x % 2019;
    }
  }
  cout << ans;
}
Problem p02702 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <chrono>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  reverse(s.begin(), s.end());
  vector<int> cnt(2019);
  int ans = 0, total = 0, x = 1;

  for (int i = 0; i < n; i++) {
    cnt[total]++;
    total += (s[i] - '0') * x;
    total %= 2019;
    ans += cnt[total];
    x = x * 10 % 2019;
  }

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

Error: 0

Fixed submission:

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

using namespace std;

int main() {
  string s;
  cin >> s;
  vector<string> tmp(s.size());
  vector<long long> remain(2019, 0);

  tmp[s.size() - 1] = s[s.size() - 1];

  for (int i = s.size() - 2; i >= 0; i--) {
    tmp[i] = s[i] + tmp[i + 1];
  }

  int cur = 0;
  vector<int> c;
  for (int i = 0; i < s.size(); i++) {
    cur = stoll(tmp[i]) % 2019;
    // cout << "cur: " << cur << endl;
    remain[cur]++;
    if (remain[cur] == 1 && cur != 0)
      c.push_back(cur);
  }

  int ans = 0;

  for (int i = 0; i < c.size(); i++) {
    ans += remain[c[i]] * (remain[c[i]] - 1) / 2;
  }

  ans += remain[0];

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