prithivMLmods/Coder-Stat huggingface dataset:
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
//
// Created by misclicked on 3/7/20.
//

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

// #define ONLINE_JUDGE
#ifdef ONLINE_JUDGE
#define cerr                                                                   \
  if (false)                                                                   \
  cerr
#endif

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#else
  ios_base::sync_with_stdio(false);
  cin.tie();
#endif
  ll n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  ll ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
  } else {
    unordered_map<ll, ll> mape;
    mape[0]++;
    ll now = 0, p10 = 1;
    for (int i = n - 1; ~i; i--) {
      now = (now + (s[i] - '0') * p10) % p;
      ans += mape[now];
      mape[now]++;
      p10 = (p10 * 10) % p;
    }
  }
  cout << ans << endl;
}

Fixed submission:
//
// Created by misclicked on 3/7/20.
//

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define ONLINE_JUDGE
#ifdef ONLINE_JUDGE
#define cerr                                                                   \
  if (false)                                                                   \
  cerr
#endif

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#else
  ios_base::sync_with_stdio(false);
  cin.tie();
#endif
  ll n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  ll ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
  } else {
    unordered_map<ll, ll> mape;
    mape[0]++;
    ll now = 0, p10 = 1;
    for (int i = n - 1; ~i; i--) {
      now = (now + (s[i] - '0') * p10) % p;
      ans += mape[now];
      mape[now]++;
      p10 = (p10 * 10) % p;
    }
  }
  cout << ans << endl;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
// この方法で関数を出せば多分できる
// https://nlab.itmedia.co.jp/nl/articles/1805/04/news002.html

const int MAX = 100 / 0.08;
int main() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (10 % p == 0) { // pが2 or 5
    ll ans = 0;
    // 2か5で割り切れるというのは、1桁目を見るだけで判断可能なので
    // 1桁目の余りが0だったらOK
    // 通り数は、桁数と同等なのでr+1をnまで足せばOK
    rep(r, n) {
      if ((s[r] - '0') % p == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  // 累積和の配列
  // 各桁の余りの合計
  vector<int> d(n + 1);
  int ten = 1;
  // 1桁目から計算する
  for (int i = n - 1; i >= 0; --i) {
    int a = (s[i] - '0') * ten % p;
    d[i] = (d[i + i] + 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;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
// この方法で関数を出せば多分できる
// https://nlab.itmedia.co.jp/nl/articles/1805/04/news002.html

const int MAX = 100 / 0.08;
int main() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (10 % p == 0) { // pが2 or 5
    ll ans = 0;
    // 2か5で割り切れるというのは、1桁目を見るだけで判断可能なので
    // 1桁目の余りが0だったらOK
    // 通り数は、桁数と同等なのでr+1をnまで足せばOK
    rep(r, n) {
      if ((s[r] - '0') % p == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  // 累積和の配列
  // 各桁の余りの合計
  vector<int> d(n + 1);
  int ten = 1;
  // 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;
}
Problem p02757 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * 	Heart beats fast
 * 		Colors and promises
 * 			How to be brave
 * 				How can I love when I am afraid to fall...
 */
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define F first
#define S second

void solve() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (p == 2 || p == 5) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
    cout << ans;
    return;
  }
  int cnt = 0;
  map<pair<int, int>, int> m;
  f(i, n) {
    int r = 0, pref = 0;
    fp(j, i, n - 1) {
      r = ((10 * r) + (s[j] - '0')) % p;
      if (m[{j, r}] == 1)
        break;
      m[{j, r}] = 1;
      if (r == 0)
        pref++;
    }
    cnt += ((pref * (pref + 1)) / 2);
  }
  cout << cnt;
  return;
}

signed main() {
  FAST;
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*
 * 	Heart beats fast
 * 		Colors and promises
 * 			How to be brave
 * 				How can I love when I am afraid to fall...
 */
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define f(i, n) for (int i = 0; i < n; i++)
#define fp(i, k, n) for (int i = k; i <= n; i++)
#define fr(i, k, n) for (int i = k; i >= n; i--)
#define pb push_back
#define F first
#define S second

void solve() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (p == 2 || p == 5) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
    cout << ans;
    return;
  }
  int cnt = 0, r = 0;
  map<int, int> m;
  m[0] = 1;
  int q = 1;
  fr(i, n - 1, 0) {
    r = ((s[i] - '0') * q + r) % p;
    q *= 10;
    q %= p;
    cnt += m[r];
    m[r]++;
  }
  cout << cnt;
  return;
}

signed main() {
  FAST;
  int T = 1;
  // cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 1e5 + 5;
const int M = 1560 + 5;
const ll OO = 2e18;
const int mod = 1e9 + 7;

int n, p, pre[N];
string s;
ll ans;
map<int, int> m;

int mul(int a, int b) { return (a * b) % p; }

int pw(int b, int k) {

  if (!k)
    return 1;

  int res = pw(b, k >> 1);

  return mul(mul(res, res), (k & 1 ? b : 1));
}

int modInv(int a) { return pw(a, p - 2); }

void init() {}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> p >> s;

  if (p == 2 || p == 5) {

    for (int i = 0; i < n; ++i) {
      if ((s[i] - '0') % p == 0) {
        ans += i + 1;
      }
    }

  } else {

    for (int i = 0; i < n; ++i) {
      pre[i] = ((i ? pre[i - 1] : 0) * 10 + (s[i] - '0')) % p;
    }

    m[0] = 1;

    for (int i = 0; i < n; ++i) {
      ans += m[mul(pre[i], modInv(pw(10, i)))]++;
    }
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
// #pragma GCC optimize ('O3')
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
#define f first
#define s second
#define pb push_back

const int N = 2e5 + 5;
const int M = 1560 + 5;
const ll OO = 2e18;
const int mod = 1e9 + 7;

int n, p, pre[N];
string s;
ll ans;
map<int, int> m;

int mul(int a, int b) { return (a * b) % p; }

int pw(int b, int k) {

  if (!k)
    return 1;

  int res = pw(b, k >> 1);

  return mul(mul(res, res), (k & 1 ? b : 1));
}

int modInv(int a) { return pw(a, p - 2); }

void init() {}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> p >> s;

  if (p == 2 || p == 5) {

    for (int i = 0; i < n; ++i) {
      if ((s[i] - '0') % p == 0) {
        ans += i + 1;
      }
    }

  } else {

    for (int i = 0; i < n; ++i) {
      pre[i] = ((i ? pre[i - 1] : 0) * 10 + (s[i] - '0')) % p;
    }

    m[0] = 1;

    for (int i = 0; i < n; ++i) {
      ans += m[mul(pre[i], modInv(pw(10, i)))]++;
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
#define c2i(c) ((int)c - '0')
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int char2int(char c) { return int(c - '0'); }
char s[200'009];
int f[10'009];
int main() {
  int n, p;
  scanf("%d%d%s", &n, &mod, s);
  if (p == 2 || p == 5) {
    ll ans = 0;
    rrep(i, n) if (c2i(s[i]) % p == 0) ans += i + 1;
    cout << ans << endl;
    return 0;
  }
  ll rem = 0;
  ll pow = 1;
  ll ans = 0;
  REP(i, 1, p) f[i] = 0;
  f[0] = 1;
  rrep(i, n) {
    rem = (rem + c2i(s[i]) * pow) % p;
    ans += f[rem];
    f[rem]++;
    pow = pow * 10 % p;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
#define c2i(c) ((int)c - '0')
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int char2int(char c) { return int(c - '0'); }
char s[200'009];
int f[10'009];
int main() {
  int n, p;
  scanf("%d%d%s", &n, &p, s);
  if (p == 2 || p == 5) {
    ll ans = 0;
    rrep(i, n) if (c2i(s[i]) % p == 0) ans += i + 1;
    cout << ans << endl;
    return 0;
  }
  ll rem = 0;
  ll pow = 1;
  ll ans = 0;
  REP(i, 1, p) f[i] = 0;
  f[0] = 1;
  rrep(i, n) {
    rem = (rem + c2i(s[i]) * pow) % p;
    ans += f[rem];
    f[rem]++;
    pow = pow * 10 % p;
  }
  cout << ans << endl;
  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using ll = long long int;
using namespace std;

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

  int n, p, i, j;
  string s;
  cin >> n >> p >> s;

  if (p < 10) {
    while (true)
      ;
  }

  vector<int> M(p);

  M[0] = 1;
  int sum = 0;
  int power = 1;
  ll ans = 0;
  for (i = n - 1; i >= 0; --i) {
    sum += ((s[i] - '0') * power) % p;
    sum %= p;
    ans += M[sum];
    M[sum]++;
    power *= 10;
    power %= p;
  }

  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using ll = long long int;
using namespace std;

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

  int n, p, i, j;
  string s;
  cin >> n >> p >> s;

  if (p == 2) {
    ll ans = 0;
    for (i = 0; i < n; ++i) {
      if ((s[i] - '0') % 2 == 0) {
        ans += (i + 1);
      }
    }
    cout << ans;
    return 0;
  }

  if (p == 5) {
    ll ans = 0;
    for (i = 0; i < n; ++i) {
      if ((s[i] - '0') % 5 == 0) {
        ans += (i + 1);
      }
    }
    cout << ans;
    return 0;
  }

  vector<int> M(p);

  M[0] = 1;
  int sum = 0;
  int power = 1;
  ll ans = 0;
  for (i = n - 1; i >= 0; --i) {
    sum += ((s[i] - '0') * power) % p;
    sum %= p;
    ans += M[sum];
    M[sum]++;
    power *= 10;
    power %= p;
  }

  cout << ans;

  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int N, P;
  cin >> N >> P;

  string S;
  cin >> S;

  // Pが2か5の場合
  if (P == 2 || P == 5) {
    long long answer = 0;
    for (int i = 0; i < N; i++) {
      if ((S[i] - '0') % P == 0) {
        answer += i + 1;
      }
    }
    cout << answer << endl;
    return 0;
  }

  // Pが2か5以外の場合
  vector<int> d(N + 1);
  int count = 1;
  for (int i = N - 1; 0 <= i; i--) {
    int a = (S[i] - '0') * count % P;
    d[i] = (d[i + 1] + a) % P;
    count *= 10;
    count %= P;
  }

  vector<int> sum(P);
  long long answer = 0;
  for (int i = 0; i < N + 1; i--) {
    answer += sum[d[i]];
    sum[d[i]]++;
  }

  cout << answer << endl;

  return 0;
}


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

int main() {

  int N, P;
  cin >> N >> P;

  string S;
  cin >> S;

  // Pが2か5の場合
  if (P == 2 || P == 5) {
    long long answer = 0;
    for (int i = 0; i < N; i++) {
      if ((S[i] - '0') % P == 0) {
        answer += i + 1;
      }
    }
    cout << answer << endl;
    return 0;
  }

  // Pが2か5以外の場合
  vector<int> d(N + 1);
  int count = 1;
  for (int i = N - 1; 0 <= i; i--) {
    int a = (S[i] - '0') * count % P;
    d[i] = (d[i + 1] + a) % P;
    count *= 10;
    count %= P;
  }

  vector<int> sum(P);
  long long answer = 0;
  for (int i = 0; i < N + 1; i++) {
    answer += sum[d[i]];
    sum[d[i]]++;
  }

  cout << answer << endl;

  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

struct int_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

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

struct pair_hash {
  size_t operator()(pair<int, int> x) const {
    return int_hash{}(x.first) ^ (int_hash{}(x.second) << 16);
  }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const ll INF = numeric_limits<ll>::max();
const int inf = 1e7;
const int MX = 100001; // check the limits, dummy

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

  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  ll ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0, cur = 0; i < n; i++)
      if ((s[i] - '0') % p == 0)
        ans += (i + 1);
  } else {
    unordered_map<int, int> mp{{0, 1}};
    for (int i = n - 1, cur = 0, b = 1; i >= 0; i++) {
      cur = ((b * (s[i] - '0')) + cur) % p;
      ans += mp[cur];
      mp[cur]++;
      b = (b * 10) % p;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

struct int_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

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

struct pair_hash {
  size_t operator()(pair<int, int> x) const {
    return int_hash{}(x.first) ^ (int_hash{}(x.second) << 16);
  }
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const ll INF = numeric_limits<ll>::max();
const int inf = 1e7;
const int MX = 100001; // check the limits, dummy

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

  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  ll ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0, cur = 0; i < n; i++)
      if ((s[i] - '0') % p == 0)
        ans += (i + 1);
  } else {
    unordered_map<int, int> mp{{0, 1}};
    for (int i = n - 1, cur = 0, b = 1; i >= 0; i--) {
      cur = ((b * (s[i] - '0')) + cur) % p;
      ans += mp[cur];
      mp[cur]++;
      b = (b * 10) % p;
    }
  }
  cout << ans << endl;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

int main() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (10 % p == 0) {
    ll ans = 0;
    rep(r, n) {
      if ((s[r] - '0') % p == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  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;
  }

  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;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;

int main() {
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  if (10 % p == 0) {
    ll ans = 0;
    rep(r, n) {
      if ((s[r] - '0') % p == 0) {
        ans += r + 1;
      }
    }
    cout << ans << endl;
    return 0;
  }

  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;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> 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;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n, p;
  cin >> n;
  cin >> p;
  string s;
  cin >> s;
  LL ans = 0;
  if (p == 2 || p == 5) {
    int dp[n + 1][p];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
      dp[i + 1][(s[i] - '0') % p]++;
      for (int k = 0; k < p; k++) {
        int nx = (p + 10 * k + s[i] - '0') % p;
        dp[i + 1][nx] += dp[i][k];
      }
      ans += dp[i + 1][0];
    }
  } else {
    map<LL, LL> ma;
    int now = 0;
    ma[0]++;
    vector<LL> t(n, 1);
    for (int i = 0; i < n; i++) {
      t[i + 1] = t[i] * 10 % p;
    }
    for (int i = n - 1; i >= 0; i--) {
      now = (now + t[n - 1 - i] * (s[i] - '0')) % p;
      ans += ma[now]++;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<LL, LL> 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;
}
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
const LL mod = 1000000007;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};

int main() {
  int n, p;
  cin >> n;
  cin >> p;
  string s;
  cin >> s;
  LL ans = 0;
  if (p == 2 || p == 5) {
    int dp[n + 1][p];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
      dp[i + 1][(s[i] - '0') % p]++;
      for (int k = 0; k < p; k++) {
        int nx = (p + 10 * k + s[i] - '0') % p;
        dp[i + 1][nx] += dp[i][k];
      }
      ans += dp[i + 1][0];
    }
  } else {
    map<LL, LL> ma;
    int now = 0;
    ma[0]++;
    vector<LL> t(n + 1, 1);
    for (int i = 0; i < n; i++) {
      t[i + 1] = t[i] * 10 % p;
    }
    for (int i = n - 1; i >= 0; i--) {
      now = (now + t[n - 1 - i] * (s[i] - '0')) % p;
      ans += ma[now]++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02757 (Python)

Buggy submission (status: Time Limit Exceeded):
N, P = map(int, input().split())
S = [int(_) for _ in list(input())]
if P == 2 or P == 5:
    ans = 0
    for n in range(N):
        if S[n] % P == 0:
            ans += n + 1
    print(ans)
else:
    S = [(S[n] * 10 ** (N - n - 1)) % P for n in range(N)]
    S.reverse()
    T = [0]
    for s in S:
        T.append((T[-1] + s) % P)
    lst = {i: 0 for i in range(P)}
    ans = 0
    for t in T:
        ans += lst[t]
        lst[t] += 1
    print(ans)


Fixed submission:
N, P = map(int, input().split())
S = [int(_) for _ in list(input())]
if P == 2 or P == 5:
    ans = 0
    for n in range(N):
        if S[n] % P == 0:
            ans += n + 1
    print(ans)
else:
    S = [(S[n] * pow(10, N - n - 1, P)) % P for n in range(N)]
    S.reverse()
    T = [0]
    for s in S:
        T.append((T[-1] + s) % P)
    lst = {i: 0 for i in range(P)}
    ans = 0
    for t in T:
        ans += lst[t]
        lst[t] += 1
    print(ans)
Problem p02757 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter

n, p = map(int, input().split())
s = input()

if p == 2 or p == 5:
    ans = 0
    for i, e in enumerate(s, 1):
        if int(e) % p == 0:
            ans += i

    print(ans)

else:
    li = [0]
    for i, e in enumerate(s[::-1], 1):
        li.append((li[-1] + int(e) * 10**i) % p)

    c = Counter(li)
    ans = 0
    for v in c.values():
        ans += v * (v - 1) // 2

    print(ans)


Fixed submission:
from collections import Counter

n, p = map(int, input().split())
s = input()

if p == 2 or p == 5:
    ans = 0
    for i, e in enumerate(s, 1):
        if int(e) % p == 0:
            ans += i

    print(ans)

else:
    li = [0]
    for i, e in enumerate(s[::-1], 1):
        li.append((li[-1] + int(e) * pow(10, i, p)) % p)

    c = Counter(li)
    ans = 0
    for v in c.values():
        ans += v * (v - 1) // 2

    print(ans)
Problem p02757 (Python)

Buggy submission (status: Time Limit Exceeded):
n, p = map(int, input().split())
s = input()

if p == 2 or p == 5:
    ans = 0
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
else:
    s2 = s[::-1]
    rems = [0]
    tmp = 0
    tmp2 = 1
    for i in range(n):
        tmp += int(s2[i]) * tmp2
        tmp %= p
        tmp2 *= 10
        rems.append(tmp)

    from collections import Counter

    counter = Counter(rems)
    ans = 0
    for key in counter.keys():
        v = counter[key]
        ans += v * (v - 1) // 2
print(ans)


Fixed submission:
n, p = map(int, input().split())
s = input()

if p == 2 or p == 5:
    ans = 0
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i + 1
else:
    s2 = s[::-1]
    rems = [0]
    tmp = 0
    tmp2 = 1
    for i in range(n):
        tmp += int(s2[i]) * tmp2
        tmp %= p
        tmp2 *= 10
        tmp2 %= p
        rems.append(tmp)

    from collections import Counter

    counter = Counter(rems)
    ans = 0
    for key in counter.keys():
        v = counter[key]
        ans += v * (v - 1) // 2
print(ans)
Problem p02757 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, p;
  string s;
  cin >> n >> p >> s;
  int ans = 0;
  unordered_map<int, int> m;
  if (p <= 1000) {
    for (int i = 0; i < n; ++i) {
      // Just s[i]
      int use = (s[i] - '0') % p;
      if (use == 0) {
        ++ans;
      }
      // New map
      unordered_map<int, int> nm;
      for (auto it : m) {
        int r1 = (10 * it.first + use) % p;
        if (r1 == 0) {
          ans += it.second;
        }
        nm[r1] += it.second;
      }

      ++nm[use];
      m = nm;
    }
    cout << ans;
  } else {
    vector<int> pre(n);
    int ten = 1;
    for (int i = n - 1; i >= 0; --i) {
      pre[i] = (ten * (s[i] - '0')) % p;
      ten = (ten * 10) % p;
    }
    for (int i = 1; i < n; ++i)
      pre[i] = (pre[i] + pre[i - 1]) % p;
    m[0] = 1;
    for (int i = 0; i < n; ++i) {
      ans += m[pre[i]];
      ++m[pre[i]];
    }
    cout << ans;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, p;
  string s;
  cin >> n >> p >> s;
  int ans = 0;
  unordered_map<int, int> m;
  if (p <= 100) {
    for (int i = 0; i < n; ++i) {
      // Just s[i]
      int use = (s[i] - '0') % p;
      if (use == 0) {
        ++ans;
      }
      // New map
      unordered_map<int, int> nm;
      for (auto it : m) {
        int r1 = (10 * it.first + use) % p;
        if (r1 == 0) {
          ans += it.second;
        }
        nm[r1] += it.second;
      }

      ++nm[use];
      m = nm;
    }
    cout << ans;
  } else {
    vector<int> pre(n);
    int ten = 1;
    for (int i = n - 1; i >= 0; --i) {
      pre[i] = (ten * (s[i] - '0')) % p;
      ten = (ten * 10) % p;
    }
    for (int i = 1; i < n; ++i)
      pre[i] = (pre[i] + pre[i - 1]) % p;
    m[0] = 1;
    for (int i = 0; i < n; ++i) {
      ans += m[pre[i]];
      ++m[pre[i]];
    }
    cout << ans;
  }
  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define FORE(x, xs) for (const auto &x : xs)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x, y) x = min(x, y)
#define CHMAX(x, y) x = max(x, y)

const int MAX = 1010;

int N, P;
string S;

ll sub() {
  ll ans = 0;
  REP(i, N) {
    if ((S[i] - '0') % P == 0)
      ans += i + 1;
  }
  return ans;
}

ll solve() {
  if (P == 2 || P == 5)
    return sub();

  vi count(MAX);
  ll x = 0, k = 1;
  REPR(i, N) {
    x += k * (S[i] - '0');
    k = k * 10 % P;
    x %= P;
    count[x]++;
  }
  ll ans = count[0];
  REP(i, MAX) { ans += count[i] * (count[i] - 1) / 2; }
  return ans;
}

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

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = n - 1; i >= m; --i)
#define FORE(x, xs) for (const auto &x : xs)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x, y) x = min(x, y)
#define CHMAX(x, y) x = max(x, y)

const int MAX = 10010;

int N, P;
string S;

ll sub() {
  ll ans = 0;
  REP(i, N) {
    if ((S[i] - '0') % P == 0)
      ans += i + 1;
  }
  return ans;
}

ll solve() {
  if (P == 2 || P == 5)
    return sub();

  vi count(MAX);
  ll x = 0, k = 1;
  REPR(i, N) {
    x += k * (S[i] - '0');
    k = k * 10 % P;
    x %= P;
    count[x]++;
  }
  ll ans = count[0];
  REP(i, MAX) { ans += count[i] * (count[i] - 1) / 2; }
  return ans;
}

int main() {
  cin >> N >> P >> S;
  cout << solve() << endl;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p, a[N], ps[N];

int mul(int aa, int bb) { return int(1LL * aa * bb % p); }
int fexp(int b, int e = p - 2) {
  int ans = 1;
  while (e) {
    if (e & 1)
      ans = mul(ans, b);
    b = mul(b, b);
    e /= 2;
  }
  return ans;
}

int cnt[N];

int main() {
  scanf("%d %d", &n, &p);
  for (int i = 1; i <= n; i++)
    scanf("%1d", a + i);

  if (__gcd(10, p) == 1) {
    ll ans = 0;
    cnt[0] = 1;
    int pw = 1;
    for (int i = n; i >= 1; i--) {
      ps[i] = (a[i] * pw + ps[i + 1]) % p;
      ans += cnt[ps[i]];
      cnt[ps[i]]++;
      pw = 10 * pw % p;
    }

    printf("%lld\n", ans);
    return 0;
  }
  assert(0);
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p, a[N], ps[N];

int mul(int aa, int bb) { return int(1LL * aa * bb % p); }
int fexp(int b, int e = p - 2) {
  int ans = 1;
  while (e) {
    if (e & 1)
      ans = mul(ans, b);
    b = mul(b, b);
    e /= 2;
  }
  return ans;
}

int cnt[N];

int main() {
  scanf("%d %d", &n, &p);
  for (int i = 1; i <= n; i++)
    scanf("%1d", a + i);

  if (__gcd(10, p) == 1) {
    ll ans = 0;
    cnt[0] = 1;
    int pw = 1;
    for (int i = n; i >= 1; i--) {
      ps[i] = (a[i] * pw + ps[i + 1]) % p;
      ans += cnt[ps[i]];
      cnt[ps[i]]++;
      pw = 10 * pw % p;
    }

    printf("%lld\n", ans);
    return 0;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] % p == 0) {
      ans += i;
    }
  }
  printf("%lld\n", ans);
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define per(i, a, n) for (int i = a; i <= n; i++)
#define ls l + mid >> 1
#define rs mid + 1 + r >> 1
using namespace std;
typedef long long ll;
int vp[10010];
char s[10010];
int main() {
  int n, p;
  cin >> n >> p;
  ll ans = 0;
  scanf("%s", s + 1);
  ll kk = 1, an = 0;
  vp[0] = 1;
  if (p == 2 || p == 5) {
    for (int i = n; i >= 1; i--) {
      if ((s[i] - '0') % p == 0)
        ans += i;
    }
    cout << ans << endl;
  } else {
    for (int i = n; i >= 1; i--) {
      an = an + ((s[i] - '0') * kk) % p;
      an %= p;
      ans += vp[an];
      vp[an]++;
      kk = kk * 10 % p;
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define per(i, a, n) for (int i = a; i <= n; i++)
#define ls l + mid >> 1
#define rs mid + 1 + r >> 1
using namespace std;
typedef long long ll;
int vp[10010];
char s[200010];
int main() {
  int n, p;
  cin >> n >> p;
  ll ans = 0;
  scanf("%s", s + 1);
  ll kk = 1, an = 0;
  vp[0] = 1;
  if (p == 2 || p == 5) {
    for (int i = n; i >= 1; i--) {
      if ((s[i] - '0') % p == 0)
        ans += i;
    }
    cout << ans << endl;
  } else {
    for (int i = n; i >= 1; i--) {
      an = an + ((s[i] - '0') * kk) % p;
      an %= p;
      ans += vp[an];
      vp[an]++;
      kk = kk * 10 % p;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, p;
char s[100010];
map<long, long> mp;
int main() {
  cin >> n >> p;
  cin >> s;
  long long ans = 0;
  if (10 % p == 0) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
  } else {
    // 从低位往上搞
    long long res = 0, t = 1;
    for (int i = n - 1; i >= 0; i--) {
      res = res + (s[i] - '0') * t;
      res %= p;
      t = t * 10 % p;
      if (res == 0)
        ans++;
      ans += mp[res];
      mp[res]++;
    }
  }
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, p;
char s[200010];
map<long, long> mp;
int main() {
  cin >> n >> p;
  cin >> s;
  long long ans = 0;
  if (10 % p == 0) {
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    }
  } else {
    // 从低位往上搞
    long long res = 0, t = 1;
    for (int i = n - 1; i >= 0; i--) {
      res = res + (s[i] - '0') * t;
      res %= p;
      t = t * 10 % p;
      if (res == 0)
        ans++;
      ans += mp[res];
      mp[res]++;
    }
  }
  cout << ans << '\n';
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// P と 10が互いに素でない場合,
// つまりP=2 または P=5の場合、フェルマーの小定理や、
// その他逆元を高速に演算する手法が使えなさそうなので注意
// https://twitter.com/satanic0258/status/1236285593677520896
int N;
ll P;
const int MAX_N = 200;
string S;
ll C[MAX_N];

void solve_2() {
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    int c = S[i] - '0';
    if (c % 2 == 0)
      res += N - i;
  }
  cout << res << endl;
  return;
}

void solve_5() {
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    int c = S[i] - '0';
    if (c % 5 == 0)
      res += N - i;
  }
  cout << res << endl;
  return;
}

int main() {
  cin >> N >> P;
  cin >> S;

  reverse(S.begin(), S.end());
  if (P == 2) {
    solve_2();
    return 0;
  }
  if (P == 5) {
    solve_5();
    return 0;
  }

  ll base = 1;
  map<ll, ll> Map;
  Map[0]++; // 何も数を選択していないときは0になるため
  for (int i = 0; i < N; ++i) {
    C[i] = (ll)(S[i] - '0') * base % P;
    base *= 10;
    base %= P;
  }
  for (int i = 1; i < N; ++i) {
    C[i] += C[i - 1];
    C[i] %= P;
  }
  for (int i = 0; i < N; ++i) {
    Map[C[i]]++;
  }
  ll res = 0;
  for (auto &p : Map) {
    ll num = p.second;
    res += num * (num - 1) / 2;
  }
  cout << res << endl;
  return 0;
}

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

// P と 10が互いに素でない場合,
// つまりP=2 または P=5の場合、フェルマーの小定理や、
// その他逆元を高速に演算する手法が使えなさそうなので注意
// https://twitter.com/satanic0258/status/1236285593677520896
int N;
ll P;
const int MAX_N = 200100;
string S;
ll C[MAX_N];

void solve_2() {
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    int c = S[i] - '0';
    if (c % 2 == 0)
      res += N - i;
  }
  cout << res << endl;
  return;
}

void solve_5() {
  ll res = 0;
  for (int i = 0; i < N; ++i) {
    int c = S[i] - '0';
    if (c % 5 == 0)
      res += N - i;
  }
  cout << res << endl;
  return;
}

int main() {
  cin >> N >> P;
  cin >> S;

  reverse(S.begin(), S.end());
  if (P == 2) {
    solve_2();
    return 0;
  }
  if (P == 5) {
    solve_5();
    return 0;
  }

  ll base = 1;
  map<ll, ll> Map;
  Map[0]++; // 何も数を選択していないときは0になるため
  for (int i = 0; i < N; ++i) {
    C[i] = (ll)(S[i] - '0') * base % P;
    base *= 10;
    base %= P;
  }
  for (int i = 1; i < N; ++i) {
    C[i] += C[i - 1];
    C[i] %= P;
  }
  for (int i = 0; i < N; ++i) {
    Map[C[i]]++;
  }
  ll res = 0;
  for (auto &p : Map) {
    ll num = p.second;
    res += num * (num - 1) / 2;
  }
  cout << res << endl;
  return 0;
}
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int n;
pair<int, int> p[1000001];
int t[100001 * 4];
int far[100001];
int dp[100001];
void update(int x, int st, int ed, int pos, int ind) {
  if (st == ed) {
    t[x] = ind;
    return;
  }
  int mid = (st + ed) / 2;
  if (pos <= mid)
    update(x * 2, st, mid, pos, ind);
  else
    update(x * 2 + 1, mid + 1, ed, pos, ind);
  t[x] = max(t[x * 2], t[x * 2 + 1]);
}
int query(int x, int st, int ed, int l, int r) {
  if (st >= l && ed <= r)
    return t[x];
  if (st > r || ed < l)
    return 0;
  int mid = (st + ed) / 2;
  return max(query(x * 2, st, mid, l, r), query(x * 2 + 1, mid + 1, ed, l, r));
}

signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &p[i].first, &p[i].second);
  sort(p + 1, p + n + 1);
  for (int i = n; i >= 1; i--) {
    int l = i - 1, r = n + 1;
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      if (p[mid].first <= p[i].first + p[i].second - 1)
        l = mid;
      else
        r = mid;
    }
    far[i] = max(i, query(1, 1, n, i + 1, l));
    update(1, 1, n, i, far[i]);
  }
  dp[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1] + dp[far[i] + 1];
    dp[i] %= mod;
  }
  printf("%d", dp[1]);
  return 0;
}
/*
4 2
2212
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int n;
pair<int, int> p[200001];
int t[200001 * 4];
int far[200001];
int dp[200001];
void update(int x, int st, int ed, int pos, int ind) {
  if (st == ed) {
    t[x] = ind;
    return;
  }
  int mid = (st + ed) / 2;
  if (pos <= mid)
    update(x * 2, st, mid, pos, ind);
  else
    update(x * 2 + 1, mid + 1, ed, pos, ind);
  t[x] = max(t[x * 2], t[x * 2 + 1]);
}
int query(int x, int st, int ed, int l, int r) {
  if (st >= l && ed <= r)
    return t[x];
  if (st > r || ed < l)
    return 0;
  int mid = (st + ed) / 2;
  return max(query(x * 2, st, mid, l, r), query(x * 2 + 1, mid + 1, ed, l, r));
}

signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &p[i].first, &p[i].second);
  sort(p + 1, p + n + 1);
  for (int i = n; i >= 1; i--) {
    int l = i - 1, r = n + 1;
    while (l + 1 < r) {
      int mid = (l + r) / 2;
      if (p[mid].first <= p[i].first + p[i].second - 1)
        l = mid;
      else
        r = mid;
    }
    far[i] = max(i, query(1, 1, n, i + 1, l));
    update(1, 1, n, i, far[i]);
  }
  dp[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1] + dp[far[i] + 1];
    dp[i] %= mod;
  }
  printf("%d", dp[1]);
  return 0;
}
/*
4 2
2212
*/
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>

using namespace std;

typedef long long ll;
const ll mod = 998244353;

int t[500000];

int Max(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return max(Max(v * 2, tl, tm, l, min(r, tm)),
             Max(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void Update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr)
    t[v] = new_val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      Update(v * 2, tl, tm, pos, new_val);
    else
      Update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  vector<int> skip(n);
  for (int i = 0; i < n; ++i) {
    auto it = lower_bound(a.begin(), a.end(),
                          make_pair<ll, ll>(a[i].first + a[i].second, -1));
    skip[i] = it - a.begin() - 1;
  }

  Update(1, 0, n, 2, 2);
  for (int i = n - 1; i >= 0; --i) {
    skip[i] = max(skip[i], Max(1, 0, n, i, skip[i]));
    Update(1, 0, n, i, skip[i]);
  }

  vector<ll> ans(n + 1);
  ans[n] = 1;
  for (int i = n - 1; i >= 0; --i)
    ans[i] = (ans[i + 1] + ans[skip[i] + 1]) % mod;
  cout << ans[0] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <string>

using namespace std;

typedef long long ll;
const ll mod = 998244353;

int t[200 * 1001 * 4];

int Max(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  int tm = (tl + tr) / 2;
  return max(Max(v * 2, tl, tm, l, min(r, tm)),
             Max(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void Update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr)
    t[v] = new_val;
  else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      Update(v * 2, tl, tm, pos, new_val);
    else
      Update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

int main() {
  int n;
  cin >> n;
  vector<pair<ll, ll>> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i].first >> a[i].second;
  sort(a.begin(), a.end());
  vector<int> skip(n);
  for (int i = 0; i < n; ++i) {
    auto it = lower_bound(a.begin(), a.end(),
                          make_pair<ll, ll>(a[i].first + a[i].second, -1));
    skip[i] = it - a.begin() - 1;
  }

  Update(1, 0, n, 2, 2);
  for (int i = n - 1; i >= 0; --i) {
    skip[i] = max(skip[i], Max(1, 0, n, i, skip[i]));
    Update(1, 0, n, i, skip[i]);
  }

  vector<ll> ans(n + 1);
  ans[n] = 1;
  for (int i = n - 1; i >= 0; --i)
    ans[i] = (ans[i + 1] + ans[skip[i] + 1]) % mod;
  cout << ans[0] << endl;
  return 0;
}
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

#define mod 998244353LL
#define N (20 + 5)

typedef long long ll;
struct robot {
  int l, d;
  friend bool operator<(robot a, robot b) {
    return a.l < b.l || (a.l == b.l && a.d > b.d);
  }
} a[N];
int p[N], fir[N], nxt[N];
ll f[N];

void dfs(int x) {
  f[x] = 1LL;
  for (int i = fir[x]; i; i = nxt[i]) {
    dfs(i);
    f[x] *= f[i] + 1;
    f[x] %= mod;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].d);
  std::sort(a + 1, a + n + 1);

  for (int i = 1; i <= n; i++) {
    if (i > 1 && a[i].l == a[i - 1].l)
      continue;
    int tp = i - 1;
    while (tp && a[tp].l + a[tp].d <= a[i].l)
      tp = p[tp];
    p[i] = tp;
    nxt[i] = fir[tp];
    fir[tp] = i;
  }

  dfs(0);
  printf("%lld\n", f[0]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

#define mod 998244353LL
#define N (200000 + 5)

typedef long long ll;
struct robot {
  int l, d;
  friend bool operator<(robot a, robot b) {
    return a.l < b.l || (a.l == b.l && a.d > b.d);
  }
} a[N];
int p[N], fir[N], nxt[N];
ll f[N];

void dfs(int x) {
  f[x] = 1LL;
  for (int i = fir[x]; i; i = nxt[i]) {
    dfs(i);
    f[x] *= f[i] + 1;
    f[x] %= mod;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].l, &a[i].d);
  std::sort(a + 1, a + n + 1);

  for (int i = 1; i <= n; i++) {
    if (i > 1 && a[i].l == a[i - 1].l)
      continue;
    int tp = i - 1;
    while (tp && a[tp].l + a[tp].d <= a[i].l)
      tp = p[tp];
    p[i] = tp;
    nxt[i] = fir[tp];
    fir[tp] = i;
  }

  dfs(0);
  printf("%lld\n", f[0]);

  return 0;
}
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
/*
        DATE:		2020-03-11 11:08:25
        NAME:
        PROBLEM:	ABC158F
*/
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5;

int x[MAXN + 7];
int d[MAXN + 7];
int xd[MAXN + 7];
int dp[MAXN + 7];

const int MOD = 998244353;

int add(long long x, long long y) { return ((x % MOD) + (y % MOD)) % MOD; }

int main() {
  int n;
  cin >> n;

  {
    vector<pair<int, int>> v(n);

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &v[i].first, &v[i].second);
    }

    sort(v.begin(), v.end());

    for (int i = 0; i < n; i++) {
      x[i + 1] = v[i].first;
      d[i + 1] = v[i].second;
    }
  }
  {
    vector<pair<int, int>> v;

    for (int i = n; i >= 1; i--) {
      xd[i] = x[i] + d[i] - 1;
      while (v.size() > 0 and v.back().first <= xd[i]) {
        xd[i] = max(xd[i], v.back().second);
        v.pop_back();
      }
      v.push_back({x[i], xd[i]});
    }
  }

  dp[n + 1] = 1;

  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1];
    int j = upper_bound(x + i + 1, x + n + 1, xd[i]) - x;
    dp[i] = add(dp[i], dp[j]);
  }

  cout << dp[1] << "\n";

  return 0;
}

Fixed submission:
/*
        DATE:		2020-03-11 11:08:25
        NAME:
        PROBLEM:	ABC158F
*/
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5;

int x[MAXN + 7];
int d[MAXN + 7];
int xd[MAXN + 7];
int dp[MAXN + 7];

const int MOD = 998244353;

int add(long long x, long long y) { return ((x % MOD) + (y % MOD)) % MOD; }

int main() {
  int n;
  cin >> n;

  {
    vector<pair<int, int>> v(n);

    for (int i = 0; i < n; i++) {
      scanf("%d %d", &v[i].first, &v[i].second);
    }

    sort(v.begin(), v.end());

    for (int i = 0; i < n; i++) {
      x[i + 1] = v[i].first;
      d[i + 1] = v[i].second;
    }
  }
  {
    vector<pair<int, int>> v;

    for (int i = n; i >= 1; i--) {
      xd[i] = x[i] + d[i] - 1;
      while (v.size() > 0 and v.back().first <= xd[i]) {
        xd[i] = max(xd[i], v.back().second);
        v.pop_back();
      }
      v.push_back({x[i], xd[i]});
    }
  }

  dp[n + 1] = 1;

  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1];
    int j = upper_bound(x + i + 1, x + n + 1, xd[i]) - x;
    dp[i] = add(dp[i], dp[j]);
  }

  cout << dp[1] << "\n";

  return 0;
}
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
#define F first
#define S second
#define int long long
#define mod 1000000007
#define mmod 998244353
#define all(v) v.begin(), v.end()
using namespace std;
int mypow(int a, int b, int c) {
  if (b == 0)
    return 1;
  if (b % 2)
    return mypow(a, b - 1, c) * a % c;
  int z = mypow(a, b / 2, c);
  return z * z % c;
}
P p[200000];
int n, x[200000], z = -1, vz = 0, ans = 1;
void mutu(int a) {
  if (a)
    x[a] += x[a - 1];
  x[a] %= mmod;
  for (int i = z + 1; i < n && p[a].first + p[a].second > p[i].first;
       i = z + 1) {
    z++;
    mutu(i);
  }
  x[z + 1] += x[a];
}
signed main() {
  cin >> n;
  f(i, n) { cin >> p[i].first >> p[i].second; }
  sort(p, p + n);
  for (; z < n - 1;) {
    if (z + 1)
      x[z + 1] = x[z] * -1 + 1;
    else
      x[z + 1] = 1;
    z++;
    mutu(z);
    int sum = 1;
    for (; vz <= z; vz++)
      sum += x[vz];
    ans *= sum % mmod;
    ans %= 998244353;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define f(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>
#define F first
#define S second
#define int long long
#define mod 1000000007
#define mmod 998244353
#define all(v) v.begin(), v.end()
using namespace std;
int mypow(int a, int b, int c) {
  if (b == 0)
    return 1;
  if (b % 2)
    return mypow(a, b - 1, c) * a % c;
  int z = mypow(a, b / 2, c);
  return z * z % c;
}
P p[200110];
int n, x[200011], z = -1, vz = 0, ans = 1;
void mutu(int a) {
  if (a)
    x[a] += x[a - 1];
  x[a] %= mmod;
  for (int i = z + 1; i < n && p[a].first + p[a].second > p[i].first;
       i = z + 1) {
    z++;
    mutu(i);
  }
  x[z + 1] += x[a];
}
signed main() {
  cin >> n;
  f(i, n) { cin >> p[i].first >> p[i].second; }
  sort(p, p + n);
  for (; z < n - 1;) {
    if (z + 1)
      x[z + 1] = x[z] * -1 + 1;
    else
      x[z + 1] = 1;
    z++;
    mutu(z);
    int sum = 1;
    for (; vz <= z; vz++)
      sum += x[vz];
    ans *= sum % mmod;
    ans %= 998244353;
  }
  cout << ans << endl;
}
Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
pair<int, int> arr[100005];
int dp[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d %d", &arr[i].first, &arr[i].second);
  sort(arr, arr + n);
  dp[n] = 1;
  stack<pair<int, int>> s;
  s.push({2e9, n});
  for (int i = n - 1; i >= 0; i--) {
    while (arr[i].first + arr[i].second > s.top().first)
      s.pop();
    dp[i] = (dp[i + 1] + dp[s.top().second]) % mod;
    s.push({arr[i].first, i});
  }
  printf("%d", dp[0]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
pair<int, int> arr[200005];
int dp[200005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d %d", &arr[i].first, &arr[i].second);
  sort(arr, arr + n);
  dp[n] = 1;
  stack<pair<int, int>> s;
  s.push({2e9, n});
  for (int i = n - 1; i >= 0; i--) {
    while (arr[i].first + arr[i].second > s.top().first)
      s.pop();
    dp[i] = (dp[i + 1] + dp[s.top().second]) % mod;
    s.push({arr[i].first, i});
  }
  printf("%d", dp[0]);
}