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

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

const int maxn = 500010;
int n, a[maxn];
char s[maxn];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = s[n - i + 1] - '0';
  }
  int sum = 0;
  auto mul = [&]() {
    int t = 0;
    for (int i = 1; i <= n; i++) {
      t += a[i] * 9;
      a[i] = t % 10, sum += a[i], t /= 10;
    }
    if (t)
      a[++n] = t, sum += t;
  };
  mul();
  auto inc = [&]() {
    int t = 9;
    for (int i = 1; i <= n; i++) {
      t += a[i], sum -= a[i];
      a[i] = t % 10, sum += a[i], t /= 10;
    }
    if (t)
      a[++n] = t, sum += t;
  };
  for (int i = 1;; i++) {
    inc();
    if (!(sum % 9) && i * 9 >= sum)
      printf("%d\n", i), exit(0);
  }
  return 0;
}

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

const int maxn = 500010;
int n, a[maxn];
char s[maxn];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = s[n - i + 1] - '0';
  }
  int sum = 0;
  auto mul = [&]() {
    int t = 0;
    for (int i = 1; i <= n; i++) {
      t += a[i] * 9;
      a[i] = t % 10, sum += a[i], t /= 10;
    }
    if (t)
      a[++n] = t, sum += t;
  };
  mul();
  auto inc = [&]() {
    int t = 9;
    for (int i = 1; i <= n; i++) {
      t += a[i], sum -= a[i];
      a[i] = t % 10, sum += a[i], t /= 10;
      if (!t)
        break;
    }
    if (t)
      a[++n] = t, sum += t;
  };
  for (int i = 1;; i++) {
    inc();
    if (!(sum % 9) && i * 9 >= sum)
      printf("%d\n", i), exit(0);
  }
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

string func(string &s) {
  int n = s.size();
  int up = 0;
  string ret = "";
  for (int i = n - 1; i >= 0; --i) {
    up += 9 * (s[i] - '0');
    ret += (up % 10) + '0';
    up /= 10;
  }
  if (up)
    ret += up + '0';
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;
  cin >> s;
  s = func(s);
  int n = s.size();
  vector<int> ret(n + 5);
  rep(i, n) ret[i] = s[n - i - 1] - '0';
  int sum = 0;
  rep(i, n) sum += ret[i];
  int ans = 0;
  while (1) {
    int i = 0;
    while (ret[i] == 9) {
      sum -= 9;
      ret[i] = 0;
      ++i;
    }
    sum += 1;
    ret[i]++;
    ++ans;
    if (sum == ans)
      break;
  }
  cout << (ans + 8) / 9 << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

string func(string &s) {
  int n = s.size();
  int up = 0;
  string ret = "";
  for (int i = n - 1; i >= 0; --i) {
    up += 9 * (s[i] - '0');
    ret += (up % 10) + '0';
    up /= 10;
  }
  if (up)
    ret += up + '0';
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s;
  cin >> s;
  s = func(s);
  int n = s.size();
  vector<int> ret(n + 5);
  rep(i, n) ret[i] = s[n - i - 1] - '0';
  int sum = 0;
  rep(i, n) sum += ret[i];
  int ans = 0;
  while (1) {
    int i = 0;
    while (ret[i] == 9) {
      sum -= 9;
      ret[i] = 0;
      ++i;
    }
    sum += 1;
    ret[i]++;
    ++ans;
    if (sum <= ans)
      break;
  }
  cout << (ans + 8) / 9 << endl;

  return 0;
}
Problem p03789 (C++)

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

using namespace std;

const int maxn = 1e6 + 6;
char input[maxn];
int Num[maxn], tmp[maxn], mx, mx_tmp, Tot;

void Add() {
  if (mx_tmp > mx)
    swap(Num, tmp), swap(mx, mx_tmp);
  for (int i = 0; i <= mx_tmp; i++) {
    Num[i] += tmp[i], Tot += tmp[i];
    while (Num[i] >= 10)
      Num[i + 1]++, Num[i] -= 10, Tot -= 9;
  }
  for (int i = mx_tmp + 1; i <= mx; i++)
    while (Num[i] >= 10)
      Num[i + 1]++, Num[i] -= 10, Tot -= 9;
  while (Num[mx + 1])
    mx++;
}

int main() {
  scanf("%s", input), mx = strlen(input);
  reverse(input, input + mx), mx--;
  for (int i = 0; i <= mx; i++)
    Tot += Num[i] = input[i] - '0';
  memcpy(tmp, Num, sizeof(tmp)), mx_tmp = mx;
  for (int i = 1; i <= 8; i++)
    Add();
  memset(tmp, 0, sizeof(tmp)), tmp[mx_tmp = 0] = 9;
  for (int i = 0; i <= 528180; i++) {
    if (Tot <= i * 9)
      return printf("%d\n", i), 0;
    Add();
  }
  printf("-1");
  return 0;
}


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

using namespace std;

const int maxn = 1e6 + 6;
char input[maxn];
int Num[maxn], tmp[maxn], mx, mx_tmp, Tot;

void Add() {
  if (mx_tmp > mx)
    swap(Num, tmp), swap(mx, mx_tmp);
  for (int i = 0; i <= mx_tmp; i++) {
    Num[i] += tmp[i], Tot += tmp[i];
    while (Num[i] >= 10)
      Num[i + 1]++, Num[i] -= 10, Tot -= 9;
  }
  for (int i = mx_tmp + 1; i <= mx && Num[i] >= 10; i++)
    while (Num[i] >= 10)
      Num[i + 1]++, Num[i] -= 10, Tot -= 9;
  while (Num[mx + 1])
    mx++;
}

int main() {
  scanf("%s", input), mx = strlen(input);
  reverse(input, input + mx), mx--;
  for (int i = 0; i <= mx; i++)
    Tot += Num[i] = input[i] - '0';
  memcpy(tmp, Num, sizeof(tmp)), mx_tmp = mx;
  for (int i = 1; i <= 8; i++)
    Add();
  memset(tmp, 0, sizeof(tmp)), tmp[mx_tmp = 0] = 9;
  for (int i = 0; i <= 528180; i++) {
    if (Tot <= i * 9)
      return printf("%d\n", i), 0;
    Add();
  }
  printf("-1");
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

string str;
int num[216000];
int wp[216000];
bool able(int x) {
  int last = x * 9;
  for (int i = 0; i < str.size(); i++)
    wp[i] = num[i];
  for (int i = 0; i < str.size(); i++) {
    int num = wp[i];
    if (last < num)
      return false;
    int borrow = (last - num) / 10;
    last = num + borrow * 10;
    // if(x == 1)cout << last << " " << num << endl;
    for (int j = i + 1; borrow && j < str.size() + 2; j++) {
      int dig = borrow % 10;
      if (dig <= wp[j]) {
        wp[j] -= dig;
        borrow /= 10;
        continue;
      } else {
        wp[j] = wp[j] + 10 - dig;
        borrow /= 10;
        borrow++;
      }
      if (j == str.size() + 1)
        return true;
    }
  }
  return true;
}

int main() {
  cin >> str;
  for (int i = 0; i < str.size(); i++) {
    num[str.size() - i - 1] = str[i] - '0';
  }
  int bottom = 0, top = 1080000;
  while (top - bottom > 1) {
    int mid = (top + bottom) / 2;
    if (able(mid))
      top = mid;
    else
      bottom = mid;
  }
  cout << top << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

string str;
int num[1080000];
int wp[1080000];
bool able(int x) {
  int last = x * 9;
  for (int i = 0; i < str.size(); i++)
    wp[i] = num[i];
  for (int i = 0; i < str.size(); i++) {
    int num = wp[i];
    if (last < num)
      return false;
    int borrow = (last - num) / 10;
    last = num + borrow * 10;
    // if(x == 1)cout << last << " " << num << endl;
    for (int j = i + 1; borrow && j < str.size() + 2; j++) {
      int dig = borrow % 10;
      if (dig <= wp[j]) {
        wp[j] -= dig;
        borrow /= 10;
        continue;
      } else {
        wp[j] = wp[j] + 10 - dig;
        borrow /= 10;
        borrow++;
      }
      if (j == str.size() + 1)
        return true;
    }
  }
  return true;
}

int main() {
  cin >> str;
  for (int i = 0; i < str.size(); i++) {
    num[str.size() - i - 1] = str[i] - '0';
  }
  int bottom = 0, top = 1080000;
  while (top - bottom > 1) {
    int mid = (top + bottom) / 2;
    if (able(mid))
      top = mid;
    else
      bottom = mid;
  }
  cout << top << endl;
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define RI register int
const int N = 500005;
char s[N];
int a[N << 1], n, sum;
int main() {
  int x = 0;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (RI i = 1; i <= n; ++i)
    a[i] = (s[n - i + 1] - '0') * 9;
  for (RI i = 1; i <= n; ++i)
    a[i] += x, x = a[i] / 10, a[i] %= 10;
  while (x)
    a[++n] = x % 10, x /= 10;
  for (RI i = 1; i <= n; ++i)
    sum += a[i];
  for (RI i = 1, j;; ++i) {
    a[1] += 9, sum += 9, j = 1;
    while (a[j] > 9) {
      sum -= a[j + 1], a[j + 1] += a[j] / 10, sum += a[j + 1];
      sum -= a[j], a[j] %= 10, sum += a[j];
      ++j;
      if (j > n)
        n = j;
    }
    if (sum == 9 * i) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define RI register int
const int N = 500005;
char s[N];
int a[N << 1], n, sum;
int main() {
  int x = 0;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (RI i = 1; i <= n; ++i)
    a[i] = (s[n - i + 1] - '0') * 9;
  for (RI i = 1; i <= n; ++i)
    a[i] += x, x = a[i] / 10, a[i] %= 10;
  while (x)
    a[++n] = x % 10, x /= 10;
  for (RI i = 1; i <= n; ++i)
    sum += a[i];
  for (RI i = 1, j;; ++i) {
    a[1] += 9, sum += 9, j = 1;
    while (a[j] > 9) {
      sum -= a[j + 1], a[j + 1] += a[j] / 10, sum += a[j + 1];
      sum -= a[j], a[j] %= 10, sum += a[j];
      ++j;
      if (j > n)
        n = j;
    }
    if (sum <= 9 * i) {
      printf("%d\n", i);
      return 0;
    }
  }
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
char s[N];
int a[N], len, sum, ans;

int main() {
  scanf("%s", s + 1), len = strlen(s + 1);
  for (int i = 1; i <= len; i++)
    a[i] = (s[len - i + 1] - '0') * 9;
  for (int i = 1; i <= len; i++) {
    a[i + 1] += a[i] / 10, a[i] %= 10, sum += a[i];
    if (i == len && a[len + 1])
      ++len;
  }
  for (;;) {
    //		printf(">>> %d %d\n", ans, sum);
    //		for(int i = 1; i <= len; i++) printf("%d ", a[i]); puts("");
    if (sum == ans * 9) {
      printf("%d\n", ans);
      return 0;
    }
    ++ans;
    sum -= a[1], a[1] += 9;
    for (int i = 1; i <= len; i++) {
      if (a[i] < 10) {
        sum += a[i];
        break;
      }
      sum -= a[i + 1], a[i + 1] += a[i] / 10, a[i] %= 10, sum += a[i];
      if (i == len && a[len + 1])
        ++len;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
char s[N];
int a[N], len, sum, ans;

int main() {
  scanf("%s", s + 1), len = strlen(s + 1);
  for (int i = 1; i <= len; i++)
    a[i] = (s[len - i + 1] - '0') * 9;
  for (int i = 1; i <= len; i++) {
    a[i + 1] += a[i] / 10, a[i] %= 10, sum += a[i];
    if (i == len && a[len + 1])
      ++len;
  }
  for (;;) {
    if (sum % 9 == 0 && sum <= ans * 9) {
      printf("%d\n", ans);
      return 0;
    }
    ++ans;
    sum -= a[1], a[1] += 9;
    for (int i = 1; i <= len; i++) {
      if (a[i] < 10) {
        sum += a[i];
        break;
      }
      sum -= a[i + 1], a[i + 1] += a[i] / 10, a[i] %= 10, sum += a[i];
      if (i == len && a[len + 1])
        ++len;
    }
  }
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 5e2 + 5;
char c[N];
int n, i, a[N], x;
int main() {
  *c = '0';
  scanf("%s", c + 1);
  for (i = 1, n = strlen(c + 1); i <= n; ++i)
    a[i] = c[i] - c[i - 1];
  for (i = n; i; --i)
    if (a[i] < 0)
      --a[i - 1], a[i] += 10, ++a[n];
  for (; a[n] > 10;)
    for (i = n; a[i] >= 10 && a[n]; --i)
      ++a[i - 1], a[i] -= 10, a[n]--;
  for (i = n - 1; i; --i)
    if (a[i] == 10 && a[n])
      a[i] = 0, ++a[i - 1], --a[n];
  for (x = 0, i = 1; i <= n; ++i)
    x += a[i];
  printf("%d\n", (x + 8) / 9);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int N = 5e5 + 5;
char c[N];
int n, i, a[N], x;
int main() {
  *c = '0';
  scanf("%s", c + 1);
  for (i = 1, n = strlen(c + 1); i <= n; ++i)
    a[i] = c[i] - c[i - 1];
  for (i = n; i; --i)
    if (a[i] < 0)
      --a[i - 1], a[i] += 10, ++a[n];
  for (; a[n] > 10;)
    for (i = n; a[i] >= 10 && a[n]; --i)
      ++a[i - 1], a[i] -= 10, a[n]--;
  for (i = n - 1; i; --i)
    if (a[i] == 10 && a[n])
      a[i] = 0, ++a[i - 1], --a[n];
  for (x = 0, i = 1; i <= n; ++i)
    x += a[i];
  printf("%d\n", (x + 8) / 9);
  return 0;
}
Problem p03789 (C++)

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

int num[500010], n, sum;
char s[500010];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum = 0;
  for (int i = 1; i <= n; i++)
    num[i] = s[n - i + 1] - '0';
  for (int i = 1; i <= n; i++)
    num[i] *= 9;
  for (int i = 1; i <= n; i++)
    num[i + 1] += num[i] / 10, num[i] %= 10;
  while (num[n + 1])
    n++;
  for (int i = 1; i <= n; i++)
    sum += num[i];
  int ans = 0;
  for (int i = 1;; i++) {
    num[1]++;
    sum++;
    int hh = 1;
    while (num[hh] > 9)
      num[hh + 1] += num[hh] / 10, num[hh] %= 10, hh++, sum -= 9;
    if (sum == i) {
      ans = i;
      break;
    }
  }
  printf("%d\n", ans / 9 + (bool)(ans % 9));
  return 0;
}

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

int num[500010], n, sum;
char s[500010];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum = 0;
  for (int i = 1; i <= n; i++)
    num[i] = s[n - i + 1] - '0';
  for (int i = 1; i <= n; i++)
    num[i] *= 9;
  for (int i = 1; i <= n; i++)
    num[i + 1] += num[i] / 10, num[i] %= 10;
  while (num[n + 1])
    n++;
  for (int i = 1; i <= n; i++)
    sum += num[i];
  int ans = 0;
  for (int i = 1;; i++) {
    num[1]++;
    sum++;
    int hh = 1;
    while (num[hh] > 9)
      num[hh + 1] += num[hh] / 10, num[hh] %= 10, hh++, sum -= 9;
    if (sum <= i) {
      ans = i;
      break;
    }
  }
  printf("%d\n", ans / 9 + (bool)(ans % 9));
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<int> v;
  int t = 0;
  for (int i = 0; i < s.size(); i++) {
    v.push_back(((s[i] - '0') * 9 + t) % 10);
    t = ((s[i] - '0') * 9 + t) / 10;
  }
  if (t > 0)
    v.push_back(t);
  reverse(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += v[i];
  for (int p = 0;; p++) {
    if (sum == 0) {
      printf("%d\n", (p + 8) / 9);
      break;
    }
    int cnt = 0;
    for (;;) {
      if (v.empty()) {
        v.push_back(1);
        sum++;
        break;
      }
      if (v[v.size() - 1] == 9) {
        v.pop_back();
        sum -= 9;
        cnt++;
      } else {
        v[v.size() - 1]++;
        sum++;
        break;
      }
    }
    for (int i = 0; i < cnt; i++)
      v.push_back(0);
    sum--;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<int> v;
  int t = 0;
  for (int i = 0; i < s.size(); i++) {
    v.push_back(((s[i] - '0') * 9 + t) % 10);
    t = ((s[i] - '0') * 9 + t) / 10;
  }
  if (t > 0)
    v.push_back(t);
  reverse(v.begin(), v.end());
  int sum = 0;
  for (int i = 0; i < v.size(); i++)
    sum += v[i];
  for (int p = 0;; p++) {
    if (sum <= 0) {
      printf("%d\n", (p + 8) / 9);
      break;
    }
    int cnt = 0;
    for (;;) {
      if (v.empty()) {
        v.push_back(1);
        sum++;
        break;
      }
      if (v[v.size() - 1] == 9) {
        v.pop_back();
        sum -= 9;
        cnt++;
      } else {
        v[v.size() - 1]++;
        sum++;
        break;
      }
    }
    for (int i = 0; i < cnt; i++)
      v.push_back(0);
    sum--;
  }
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;
int N[1000005];
char Str[500005];
int nine[1000005], sum, limit;
void Read() {
  cin.getline(Str, 500005);
  int len = strlen(Str);
  for (int i = len - 1; i >= 0; i--) {
    N[++N[0]] = Str[i] - '0';
  }
  limit = N[0];
}

void Add() {
  int t = 0, i;
  for (i = 1; i <= N[0] || i <= nine[0] || t; i++) {
    sum -= N[i];
    N[i] = (N[i] + nine[i] + t);
    t = N[i] / 10;
    N[i] %= 10;
    sum += N[i];
  }
  N[0] = i - 1;
}
void mul() {
  int i, t = 0;
  for (i = 1; i <= N[0] || t; i++, t /= 10)
    N[i] = (t += N[i] * 9) % 10;
  N[0] = i - 1;
  for (int i = 1; i <= N[0]; i++)
    sum += N[i];
}
void Solve() {
  mul();
  nine[0] = 1;
  nine[1] = 9;
  for (int k = 1; k <= limit; k++) {
    Add();
    if (sum <= 9 * k) {
      cout << k << "\n";
      return;
    }
  }
}
int main() {
  Read();
  Solve();
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int N[1000005];
char Str[500005];
int nine[1000005], sum, limit;
void Read() {
  cin.getline(Str, 500005);
  int len = strlen(Str);
  for (int i = len - 1; i >= 0; i--) {
    N[++N[0]] = Str[i] - '0';
  }
  limit = N[0];
}

void Add() {
  int t = 0, i;
  for (i = 1; i <= N[0] || i <= nine[0] || t; i++) {
    if (nine[i] == 0 && t == 0)
      break;
    sum -= N[i];
    N[i] = (N[i] + nine[i] + t);
    t = N[i] / 10;
    N[i] %= 10;
    sum += N[i];
  }
  N[0] = i - 1;
}
void mul() {
  int i, t = 0;
  for (i = 1; i <= N[0] || t; i++, t /= 10)
    N[i] = (t += N[i] * 9) % 10;
  N[0] = i - 1;
  for (int i = 1; i <= N[0]; i++)
    sum += N[i];
}
void Solve() {
  mul();
  nine[0] = 1;
  nine[1] = 9;
  for (int k = 1; k <= limit; k++) {
    Add();
    if (sum <= 9 * k) {
      cout << k << "\n";
      return;
    }
  }
}
int main() {
  Read();
  Solve();
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Runtime Error):
// Heaplax
// 别让自己后悔
#include <bits/stdc++.h>
#define LL long long
#define LOG(x) cerr << #x << " = " << x << endl
#define add_edge(u, v) nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
char ch;
bool fs;
void re(int &x) {
  while (ch = getchar(), ch < 33)
    ;
  if (ch == '-')
    fs = 1, x = 0;
  else
    fs = 0, x = ch - 48;
  while (ch = getchar(), ch > 33)
    x = x * 10 + ch - 48;
  if (fs)
    x = -x;
}
using namespace std;
struct node {
  int len, sum;
  int s[55555];
  void in() {
    while (ch = getchar(), ch > '!') {
      ++len;
      s[len] = ch - 48;
    }
    reverse(s + 1, s + len + 1);
  }
  void calc(int x) {
    for (int i = 1; i <= len; ++i)
      s[i] *= x;
    for (int i = 1; i <= len; ++i) {
      s[i + 1] += s[i] / 10;
      s[i] %= 10;
      if (i == len && s[i + 1])
        ++len;
    }
    sum = 0;
    for (int i = 1; i <= len; ++i)
      sum += s[i];
  }
  void add(int x) {
    for (int i = 1; i <= len; ++i) {
      sum -= s[i];
      s[i] += x % 10;
      x /= 10;
      if (s[i] >= 10)
        s[i] -= 10, ++x;
      sum += s[i];
      if (!x)
        return;
      if (i == len)
        ++len;
    }
  }
} a;
int main() {
  a.in();
  a.calc(9);
  for (int i = 1;; ++i) {
    a.add(9);
    if (a.sum <= i * 9) {
      printf("%d\n", i);
      return 0;
    }
  }
}
/*

*/


Fixed submission:
// Heaplax
// 别让自己后悔
#include <bits/stdc++.h>
#define LL long long
#define LOG(x) cerr << #x << " = " << x << endl
#define add_edge(u, v) nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v
#define open(x) freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
char ch;
bool fs;
void re(int &x) {
  while (ch = getchar(), ch < 33)
    ;
  if (ch == '-')
    fs = 1, x = 0;
  else
    fs = 0, x = ch - 48;
  while (ch = getchar(), ch > 33)
    x = x * 10 + ch - 48;
  if (fs)
    x = -x;
}
using namespace std;
struct node {
  int len, sum;
  int s[555555];
  void in() {
    while (ch = getchar(), ch > '!') {
      ++len;
      s[len] = ch - 48;
    }
    reverse(s + 1, s + len + 1);
  }
  void calc(int x) {
    for (int i = 1; i <= len; ++i)
      s[i] *= x;
    for (int i = 1; i <= len; ++i) {
      s[i + 1] += s[i] / 10;
      s[i] %= 10;
      if (i == len && s[i + 1])
        ++len;
    }
    sum = 0;
    for (int i = 1; i <= len; ++i)
      sum += s[i];
  }
  void add(int x) {
    for (int i = 1; i <= len; ++i) {
      sum -= s[i];
      s[i] += x % 10;
      x /= 10;
      if (s[i] >= 10)
        s[i] -= 10, ++x;
      sum += s[i];
      if (!x)
        return;
      if (i == len)
        ++len;
    }
  }
} a;
int main() {
  a.in();
  a.calc(9);
  for (int i = 1;; ++i) {
    a.add(9);
    if (a.sum <= i * 9) {
      printf("%d\n", i);
      return 0;
    }
  }
}
/*

*/
Problem p03789 (C++)

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

#define rep(i, l, r) for (__typeof(l) i = (l), _##i##_ = (r); i < _##i##_; ++i)
#define rof(i, l, r)                                                           \
  for (__typeof(l) i = (l)-1, _##i##_ = (r); i >= _##i##_; --i)
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(), x.end()
template <typename T> bool chkMax(T &x, const T &y) {
  return x < y ? (x = y), 1 : 0;
}
template <typename T> bool chkMin(T &x, const T &y) {
  return x > y ? (x = y), 1 : 0;
}
typedef long long ll;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define DEBUG debug("passing %d in %s\n", __LINE__, __FUNCTION__)

const int MAXN = 50000 + 5;

char Str[MAXN];
int N;

bool check(int x) {
  static int A[MAXN];
  int len = N - 1, sum = 0;
  rep(i, 0, N) { A[i] = (Str[i] - '0') * 9; }
  A[0] += 9 * x;
  for (int i = 0; i <= len; ++i) {
    if (i == len && A[i] > 9) {
      A[++len] = 0;
    }
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
    sum += A[i];
  }
  return sum <= 9 * x;
}

int main() {
  scanf("%s", Str);
  N = strlen(Str);
  std::reverse(Str, Str + N);
  int l = 1, r = N;
  int ans = 0;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (check(m)) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}

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

#define rep(i, l, r) for (__typeof(l) i = (l), _##i##_ = (r); i < _##i##_; ++i)
#define rof(i, l, r)                                                           \
  for (__typeof(l) i = (l)-1, _##i##_ = (r); i >= _##i##_; --i)
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(), x.end()
template <typename T> bool chkMax(T &x, const T &y) {
  return x < y ? (x = y), 1 : 0;
}
template <typename T> bool chkMin(T &x, const T &y) {
  return x > y ? (x = y), 1 : 0;
}
typedef long long ll;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define DEBUG debug("passing %d in %s\n", __LINE__, __FUNCTION__)

const int MAXN = 500000 + 5;

char Str[MAXN];
int N;

bool check(int x) {
  static int A[MAXN];
  int len = N - 1, sum = 0;
  rep(i, 0, N) { A[i] = (Str[i] - '0') * 9; }
  A[0] += 9 * x;
  for (int i = 0; i <= len; ++i) {
    if (i == len && A[i] > 9) {
      A[++len] = 0;
    }
    A[i + 1] += A[i] / 10;
    A[i] %= 10;
    sum += A[i];
  }
  return sum <= 9 * x;
}

int main() {
  scanf("%s", Str);
  N = strlen(Str);
  std::reverse(Str, Str + N);
  int l = 1, r = N;
  int ans = 0;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (check(m)) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MaxNL = 500000;

char s[MaxNL + 2];

struct bigNum {
  int l, w;
  int a[MaxNL + 3];

  inline void read() {
    scanf("%s", s + 1);
    l = strlen(s + 1);
    reverse(s + 1, s + l + 1);

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

    int t = w = 0;
    for (int i = 1; i <= l; ++i) {
      t += a[i] * 9;
      w += (a[i] = t % 10);
      if ((t /= 10) > 0 && i == l)
        ++l;
    }
  }

  inline void operator+=(const int &v) {
    int t = v;
    for (int i = 1; i <= l; ++i) {
      w -= a[i], t += a[i];
      w += (a[i] = t % 10);
      if ((t /= 10) > 0 && i == l)
        ++l;
    }
  }
};
bigNum cur;

int main() {
  cur.read();

  for (int k = 1;; ++k) {
    cur += 9;
    if (cur.w <= 9 * k) {
      cout << k << endl;
      break;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MaxNL = 500000;

char s[MaxNL + 2];

struct bigNum {
  int l, w;
  int a[MaxNL + 3];

  inline void read() {
    scanf("%s", s + 1);
    l = strlen(s + 1);
    reverse(s + 1, s + l + 1);

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

    int t = w = 0;
    for (int i = 1; i <= l; ++i) {
      t += a[i] * 9;
      w += (a[i] = t % 10);
      if ((t /= 10) > 0 && i == l)
        ++l;
    }
  }

  inline void operator+=(const int &v) {
    int t = v;
    for (int i = 1; t && i <= l; ++i) {
      w -= a[i], t += a[i];
      w += (a[i] = t % 10);
      if ((t /= 10) > 0 && i == l)
        ++l;
    }
  }
};
bigNum cur;

int main() {
  cur.read();

  for (int k = 1;; ++k) {
    cur += 9;
    if (cur.w <= 9 * k) {
      cout << k << endl;
      break;
    }
  }

  return 0;
}
Problem p03791 (C++)

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

using namespace std;
#define For(i, l, r) for (int i = l; i <= r; ++i)
#define Cor(i, l, r) for (int i = l; i >= r; --i)

const int MD = 1000000007;

int n, A[111];
int main() {
  int ans = 1;
  cin >> n;
  For(i, 1, n) scanf("%d", &A[i]);
  A[1] = 1;
  int cnt = 1;
  For(i, 2, n) {
    A[i] = min(A[i], A[i - 1] + 2);
    ans = (long long)ans * (cnt + 1) % MD;
    if (A[i] > A[i - 1] + 1)
      ++cnt;
    else
      A[i] = A[i - 1];
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;
#define For(i, l, r) for (int i = l; i <= r; ++i)
#define Cor(i, l, r) for (int i = l; i >= r; --i)

const int MD = 1000000007;

int n, A[111111];
int main() {
  int ans = 1;
  cin >> n;
  For(i, 1, n) scanf("%d", &A[i]);
  A[1] = 1;
  int cnt = 1;
  For(i, 2, n) {
    A[i] = min(A[i], A[i - 1] + 2);
    ans = (long long)ans * (cnt + 1) % MD;
    if (A[i] > A[i - 1] + 1)
      ++cnt;
    else
      A[i] = A[i - 1];
  }
  cout << ans << endl;
  return 0;
}
Problem p03791 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (ut)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
inline void IN(ut &x) { cin >> x; }
inline void INA(ut n, ut x[]) { REP(i, n) cin >> x[i]; }
inline void INE(ut m, VI edges[]) {
  ut a, b;
  REP(i, m) {
    cin >> a >> b;
    edges[a].PB(b);
    edges[b].PB(a);
  }
}
inline void INEC(ut m, Vpr edges[]) {
  ut a, b, c;
  REP(i, m) {
    cin >> a >> b >> c;
    edges[a].PB(pr(c, b));
    edges[b].PB(pr(c, a));
  }
}
const int SIZE = 1234;
const int INF = 10 + 1e9;
const LL p = 7 + 1e9;
ut N, A[SIZE];

int main() {
  IN(N);
  INA(N, A);
  A[0] = 1;
  FOR(i, 1, N) { A[i] = min(A[i - 1] + 2, A[i]); }
  LL ans = 1;
  LL need = 0;
  FOR(i, 1, N) {
    need = max(0LL, i * 2 - A[i - 1]) / 2;
    ans *= i + 1 - need;
    ans %= p;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (ut)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
inline void IN(ut &x) { cin >> x; }
inline void INA(ut n, ut x[]) { REP(i, n) cin >> x[i]; }
inline void INE(ut m, VI edges[]) {
  ut a, b;
  REP(i, m) {
    cin >> a >> b;
    edges[a].PB(b);
    edges[b].PB(a);
  }
}
inline void INEC(ut m, Vpr edges[]) {
  ut a, b, c;
  REP(i, m) {
    cin >> a >> b >> c;
    edges[a].PB(pr(c, b));
    edges[b].PB(pr(c, a));
  }
}
const int SIZE = 5 + 5 * 1e5;
const int INF = 10 + 1e9;
const LL p = 7 + 1e9;
ut N, A[SIZE];

int main() {
  IN(N);
  INA(N, A);
  A[0] = 1;
  FOR(i, 1, N) { A[i] = min(A[i - 1] + 2, A[i]); }
  LL ans = 1;
  LL need = 0;
  FOR(i, 1, N) {
    need = max(0LL, i * 2 - A[i - 1]) / 2;
    ans *= i + 1 - need;
    ans %= p;
  }
  cout << ans << endl;
}
Problem p03791 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define P 1000000007
#define ll long long
using namespace std;
ll n, a[10005];
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + n + 1);
  ll Ans = 1, sum = 0;
  for (ll i = 1; i <= n; i++) {
    //	cout<<Ans<<' '<<sum<<endl;
    if (sum * 2 + 1 > a[i] + 1)
      Ans = Ans * sum % P;
    else if (sum * 2 + 1 == a[i] + 1)
      Ans = Ans * (sum + 1) % P;
    else
      sum++;
    // cout<<Ans<<' '<<sum<<endl;
  }
  while (sum)
    Ans = Ans * (sum--) % P;
  printf("%lld\n", Ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define P 1000000007
#define ll long long
using namespace std;
ll n, a[100005];
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + n + 1);
  ll Ans = 1, sum = 0;
  for (ll i = 1; i <= n; i++) {
    //	cout<<Ans<<' '<<sum<<endl;
    if (sum * 2 + 1 > a[i] + 1)
      Ans = Ans * sum % P;
    else if (sum * 2 + 1 == a[i] + 1)
      Ans = Ans * (sum + 1) % P;
    else
      sum++;
    // cout<<Ans<<' '<<sum<<endl;
  }
  while (sum)
    Ans = Ans * (sum--) % P;
  printf("%lld\n", Ans);
}
Problem p03792 (C++)

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

const int INF = (1 << 28);
int N;
char A[555][555];
bool F[555];

int calc(int R) {
  int cnt = 0;
  int ec = 0;
  int pc = 1 - F[R];
  for (int j = 0; j < N; j++) {
    ec++;
    if (A[R][j] == '.') {
      cnt++;
    } else {
      bool cf = true;
      for (int i = 0; i < N; i++)
        if (A[i][j] == '.')
          cf = false;
      if (cf)
        ec--;
    }
  }
  // cout << R << " : " << cnt << " + " << ec << " + " << pc << endl;
  return cnt + ec + pc;
}

bool check() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      if (A[i][j] == '.')
        return false;
  }
  return true;
}

void view() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout << A[i][j] << " ";
    cout << endl;
  }
}

int solve() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      if (A[i][j] == '#')
        F[j] = true;
  int res = INF;
  for (int i = 0; i < N; i++) {
    res = min(res, calc(i));
  }
  return res;
}

int main() {
  cin >> N;
  assert(N <= 3);
  bool rf = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (A[i][j] == '#')
        rf = false;
    }
  }
  if (rf) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << solve() << endl;
  }
}


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

const int INF = (1 << 28);
int N;
char A[555][555];
bool F[555];

int calc(int R) {
  int cnt = 0;
  int ec = 0;
  int pc = 1 - F[R];
  for (int j = 0; j < N; j++) {
    ec++;
    if (A[R][j] == '.') {
      cnt++;
    } else {
      bool cf = true;
      for (int i = 0; i < N; i++)
        if (A[i][j] == '.')
          cf = false;
      if (cf)
        ec--;
    }
  }
  // cout << R << " : " << cnt << " + " << ec << " + " << pc << endl;
  return cnt + ec + pc;
}

bool check() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      if (A[i][j] == '.')
        return false;
  }
  return true;
}

void view() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout << A[i][j] << " ";
    cout << endl;
  }
}

int solve() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      if (A[i][j] == '#')
        F[j] = true;
  int res = INF;
  for (int i = 0; i < N; i++) {
    res = min(res, calc(i));
  }
  return res;
}

int main() {
  cin >> N;

  bool rf = true;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A[i][j];
      if (A[i][j] == '#')
        rf = false;
    }
  }
  if (rf) {
    cout << -1 << endl;
    return 0;
  } else {
    cout << solve() << endl;
  }
}
Problem p03793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define DEBUG(x) cout << #x << " = " << x << endl
#define pb push_back
using namespace std;
typedef long long ll;

const int MAXN = 100005;
int d[MAXN][27];
int b[MAXN][30];

int main() {
  // freopen("input.txt", "r", stdin);
  string s;
  cin >> s;
  int sz = s.size();

  for (int i = 0; i < 27; i++)
    d[sz][i] = sz + 1;
  for (int i = 0; i < 27; i++)
    d[sz + 1][i] = sz + 1;
  for (int l = sz - 1; l >= 0; l--) {
    int c = s[l] - 'a';
    d[l][c] = l + 1;
    for (int i = c + 1; i < 27; i++) {
      d[l][i] = d[d[l][i - 1]][i - 1];
    }

    for (int i = 0; i < c; i++) {
      d[l][i] = d[d[l][26]][i];
    }
  }

  for (int i = 0; i <= sz + 1; i++) {
    b[i][0] = d[i][26];
  }

  for (int i = 1; i < 20; i++) {
    for (int j = 0; j <= sz + 1; j++) {
      b[j][i] = b[b[j][i - 1]][i - 1];
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    int k = 19;
    while (1) {
      while (k >= 0 && b[l][k] > r)
        k--;
      if (k < 0) {
        cout << "No" << endl;
        break;
      }
      if (b[l][k] == r) {
        cout << "Yes" << endl;
        break;
      }
      l = b[l][k];
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define DEBUG(x) cout << #x << " = " << x << endl
#define pb push_back
using namespace std;
typedef long long ll;

const int MAXN = 500005;
int d[MAXN][27];
int b[MAXN][30];

int main() {
  // freopen("input.txt", "r", stdin);
  string s;
  cin >> s;
  int sz = s.size();

  for (int i = 0; i < 27; i++)
    d[sz][i] = sz + 1;
  for (int i = 0; i < 27; i++)
    d[sz + 1][i] = sz + 1;
  for (int l = sz - 1; l >= 0; l--) {
    int c = s[l] - 'a';
    d[l][c] = l + 1;
    for (int i = c + 1; i < 27; i++) {
      d[l][i] = d[d[l][i - 1]][i - 1];
    }

    for (int i = 0; i < c; i++) {
      d[l][i] = d[d[l][26]][i];
    }
  }

  for (int i = 0; i <= sz + 1; i++) {
    b[i][0] = d[i][26];
  }

  for (int i = 1; i < 20; i++) {
    for (int j = 0; j <= sz + 1; j++) {
      b[j][i] = b[b[j][i - 1]][i - 1];
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    int k = 19;
    while (1) {
      while (k >= 0 && b[l][k] > r)
        k--;
      if (k < 0) {
        cout << "No" << endl;
        break;
      }
      if (b[l][k] == r) {
        cout << "Yes" << endl;
        break;
      }
      l = b[l][k];
    }
  }

  return 0;
}
Problem p03793 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#define MN 600001
using namespace std;

int read_p, read_ca;
inline int read() {
  read_p = 0;
  read_ca = getchar();
  while (read_ca < '0' || read_ca > '9')
    read_ca = getchar();
  while (read_ca >= '0' && read_ca <= '9')
    read_p = read_p * 10 + read_ca - 48, read_ca = getchar();
  return read_p;
}
char s[MN];
int n, m, ne[MN][207], NE[MN][20], l, r;
inline int min(int a, int b) { return a == 0 ? b : b == 0 ? a : a < b ? a : b; }
int main() {
  register int i, j;
  scanf("%s", s);

  for (n = 0; s[n]; n++)
    ;

  for (i = n - 1; i >= 0; i--) {
    ne[i][s[i] -= 'a'] = i + 1;
    for (j = s[i]; j < 26; j++)
      ne[i][j + 1] = ne[ne[i][j]][j];
    for (j = 0; j < s[i]; j++)
      ne[i][j] = min(ne[ne[i][j == 0 ? 26 : j - 1]][j == 0 ? 0 : j - 1],
                     ne[ne[i][26]][j]);
  }

  for (i = n - 1; i >= 0; i--)
    if (NE[i][0] = ne[i][26])
      for (j = 1; NE[i][j] = NE[NE[i][j - 1]][j - 1]; j++)
        ;

  for (m = read(); m--;) {
    l = read() - 1;
    r = read();
    for (i = 19; i >= 0; i--)
      if (NE[l][i] && NE[l][i] <= r)
        l = NE[l][i];
    puts(l == r ? "Yes" : "No");
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#define MN 600001
using namespace std;

int read_p, read_ca;
inline int read() {
  read_p = 0;
  read_ca = getchar();
  while (read_ca < '0' || read_ca > '9')
    read_ca = getchar();
  while (read_ca >= '0' && read_ca <= '9')
    read_p = read_p * 10 + read_ca - 48, read_ca = getchar();
  return read_p;
}
char s[MN];
int n, m, ne[MN][27], NE[MN][20], l, r;
inline int min(int a, int b) { return a == 0 ? b : b == 0 ? a : a < b ? a : b; }
int main() {
  register int i, j;
  scanf("%s", s);

  for (n = 0; s[n]; n++)
    ;

  for (i = n - 1; i >= 0; i--) {
    ne[i][s[i] -= 'a'] = i + 1;
    for (j = s[i]; j < 26; j++)
      ne[i][j + 1] = ne[ne[i][j]][j];
    for (j = 0; j < s[i]; j++)
      ne[i][j] = min(ne[ne[i][j == 0 ? 26 : j - 1]][j == 0 ? 0 : j - 1],
                     ne[ne[i][26]][j]);
  }

  for (i = n - 1; i >= 0; i--)
    if (NE[i][0] = ne[i][26])
      for (j = 1; NE[i][j] = NE[NE[i][j - 1]][j - 1]; j++)
        ;

  for (m = read(); m--;) {
    l = read() - 1;
    r = read();
    for (i = 19; i >= 0; i--)
      if (NE[l][i] && NE[l][i] <= r)
        l = NE[l][i];
    puts(l == r ? "Yes" : "No");
  }
}
Problem p03793 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 1000000007
#define N 505
#define M 1000003
#define LOG 19
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n;
int go[LOG][N], nxt[N][30];
char s[N];

void solve(int l, int r) {

  for (int i = LOG - 1; i >= 0; i--) {

    if (go[i][l] <= r + 1)
      l = go[i][l];
  }

  if (l == r + 1)
    printf("Yes\n");
  else
    printf("No\n");
}

void pre_cal() {

  for (int i = 0; i < 27; i++)
    nxt[n + 1][i] = nxt[n + 2][i] = n + 2;

  for (int i = n; i >= 1; i--) {

    int cc = s[i] - 'a';

    nxt[i][cc] = i + 1;

    for (int c = cc + 1; c < 27; c++)
      nxt[i][c] = nxt[nxt[i][c - 1]][c - 1];
    for (int c = cc - 1; c >= 0; c--)
      nxt[i][c] = nxt[nxt[i][26]][c];
  }

  for (int i = 1; i <= n + 2; i++)
    go[0][i] = nxt[i][26];

  for (int j = 1; j < LOG; j++) {

    for (int i = 1; i <= n + 2; i++)
      go[j][i] = go[j - 1][go[j - 1][i]];
  }
}

int main() {

  scanf("%s", s + 1);

  n = strlen(s + 1);

  pre_cal();

  int q;

  scanf("%d", &q);

  while (q--) {

    int l, r;

    scanf("%d %d", &l, &r);

    solve(l, r);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 1000000007
#define N 500005
#define M 1000003
#define LOG 19
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n;
int go[LOG][N], nxt[N][30];
char s[N];

void solve(int l, int r) {

  for (int i = LOG - 1; i >= 0; i--) {

    if (go[i][l] <= r + 1)
      l = go[i][l];
  }

  if (l == r + 1)
    printf("Yes\n");
  else
    printf("No\n");
}

void pre_cal() {

  for (int i = 0; i < 27; i++)
    nxt[n + 1][i] = nxt[n + 2][i] = n + 2;

  for (int i = n; i >= 1; i--) {

    int cc = s[i] - 'a';

    nxt[i][cc] = i + 1;

    for (int c = cc + 1; c < 27; c++)
      nxt[i][c] = nxt[nxt[i][c - 1]][c - 1];
    for (int c = cc - 1; c >= 0; c--)
      nxt[i][c] = nxt[nxt[i][26]][c];
  }

  for (int i = 1; i <= n + 2; i++)
    go[0][i] = nxt[i][26];

  for (int j = 1; j < LOG; j++) {

    for (int i = 1; i <= n + 2; i++)
      go[j][i] = go[j - 1][go[j - 1][i]];
  }
}

int main() {

  scanf("%s", s + 1);

  n = strlen(s + 1);

  pre_cal();

  int q;

  scanf("%d", &q);

  while (q--) {

    int l, r;

    scanf("%d %d", &l, &r);

    solve(l, r);
  }
}
Problem p03793 (C++)

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

int n, a[N], f[N][26], g[N][19];
char s[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int i, j;
  for (i = 1; i <= n; i++)
    a[i] = s[i] - 'a';
  for (i = 0; i <= 25; i++)
    f[n + 1][i] = f[n + 2][i] = n + 2;
  for (i = 0; i <= 18; i++)
    g[n + 1][i] = g[n + 2][i] = n + 2;
  for (i = n; i; i--) {
    f[i][a[i]] = i + 1;
    for (j = a[i] + 1; j < 26; j++)
      f[i][j] = f[f[i][j - 1]][j - 1];
    g[i][0] = f[f[i][25]][25];
    for (j = 1; j <= 18; j++)
      g[i][j] = g[g[i][j - 1]][j - 1];
    for (j = 0; j < a[i]; j++)
      f[i][j] = f[g[i][0]][j];
  }
  int cas, l, r;
  scanf("%d", &cas);
  while (cas--) {
    scanf("%d%d", &l, &r);
    r++;
    for (i = 18; i >= 0; i--)
      if (g[l][i] <= r)
        l = g[l][i];
    puts((l == r) ? "Yes" : "No");
  }
  return 0;
}


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

int n, a[N], f[N][26], g[N][19];
char s[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int i, j;
  for (i = 1; i <= n; i++)
    a[i] = s[i] - 'a';
  for (i = 0; i <= 25; i++)
    f[n + 1][i] = f[n + 2][i] = n + 2;
  for (i = 0; i <= 18; i++)
    g[n + 1][i] = g[n + 2][i] = n + 2;
  for (i = n; i; i--) {
    f[i][a[i]] = i + 1;
    for (j = a[i] + 1; j < 26; j++)
      f[i][j] = f[f[i][j - 1]][j - 1];
    g[i][0] = f[f[i][25]][25];
    for (j = 1; j <= 18; j++)
      g[i][j] = g[g[i][j - 1]][j - 1];
    for (j = 0; j < a[i]; j++)
      f[i][j] = f[g[i][0]][j];
  }
  int cas, l, r;
  scanf("%d", &cas);
  while (cas--) {
    scanf("%d%d", &l, &r);
    r++;
    for (i = 18; i >= 0; i--)
      if (g[l][i] <= r)
        l = g[l][i];
    puts((l == r) ? "Yes" : "No");
  }
  return 0;
}
Problem p03795 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
if N > 15:
    discount = N // 15
print(N * 800 - discount * 200)


Fixed submission:
N = int(input())
discount = 0
if N > 15:
    discount = N // 15
print(N * 800 - discount * 200)
Problem p03795 (Python)

Buggy submission (status: Runtime Error):
i = int(input())
d = i * 800
b = i // 15

if 0 < b:
    c = d - (200 * b)
    print(c)
else:
    print(c)


Fixed submission:
i = int(input())
d = i * 800
b = i // 15

if 0 <= b:
    c = d - (200 * b)
    print(c)
else:
    print(c)
Problem p03795 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define int_inf 1000000000         // 1e9
#define ll_inf 1000000000000000000 // 1e18
using namespace std;
template <typename T> std::string to_string(const T &t) {
  std::ostringstream os;
  os << t;
  return os.str();
}

int main() {
  int n;
  cin >> n;
  int x = n, y = 0;
  while (n) {
    y += n / 15;
    n -= (n / 15);
  }
  cout << x * 800 - y * 200 << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define int_inf 1000000000         // 1e9
#define ll_inf 1000000000000000000 // 1e18
using namespace std;
template <typename T> std::string to_string(const T &t) {
  std::ostringstream os;
  os << t;
  return os.str();
}

int main() {
  int n;
  cin >> n;
  int x = n, y = 0;
  y += n / 15;
  cout << x * 800 - y * 200 << endl;
  return 0;
}
Problem p03795 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int N, a;
  scanf("%d", &N);

  a = N / (N % 15);

  printf("%d", 800 * N - 200 * a);

  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int N, a;
  scanf("%d", &N);

  a = (N - N % 15) / 15;

  printf("%d", 800 * N - 200 * a);

  return 0;
}
Problem p03795 (C++)

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

int main() {
  int x = 0;
  int y = 0;
  int N;
  std::cin >> N;

  x = 800 * N;
  int z;
  z = N / 15;
  y = z * 200;

  return (x - y);
}

Fixed submission:
#include <iostream>

int main() {
  int x = 0;
  int y = 0;
  int N;
  std::cin >> N;

  x = 800 * N;
  int z;
  z = N / 15;
  y = z * 200;

  std::cout << (x - y) << std::endl;
  return 0;
}
Problem p03796 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define sc(x) scanf("%d", &x);

ll factorial(int x, int mod) {
  if (x == 2)
    return 2;
  return x * factorial(x - 1, mod) % mod;
}

int main() {
  int mod = 1e9 + 7;
  int n;
  sc(n) cout << factorial(n, mod) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define sc(x) scanf("%d", &x);

ll factorial(int x, int mod) {
  if (x == 1)
    return 1;
  return x * factorial(x - 1, mod) % mod;
}

int main() {
  int mod = 1e9 + 7;
  int n;
  sc(n) cout << factorial(n, mod) << endl;
  return 0;
}
Problem p03796 (Python)

Buggy submission (status: Runtime Error):
import math

n = int(input())
print(math.factorial(n) / (pow(10, 9) + 7))


Fixed submission:
import math

n = int(input())
b = pow(10, 9) + 7
print(math.factorial(n) % b)
Problem p03796 (Python)

Buggy submission (status: Time Limit Exceeded):
N = range(1, int(input()) + 1)
prod = 1
for i in N:
    prod *= i
print(prod % (10**9 + 7))


Fixed submission:
num = 1
for i in range(1, int(input()) + 1):
    num = (num * i) % (10**9 + 7)
print(num)
Problem p03796 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {
  long long N;
  cin >> N;
  long long pow = 1;

  for (int i = 1; i < N + 1; ++i) {
    pow = pow * i % 1000000007;
  }

  cout << pow % 1000000007;

  while (true) {
    ;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  long long N;
  cin >> N;
  long long pow = 1;

  for (int i = 1; i < N + 1; ++i) {
    pow = pow * i % 1000000007;
  }

  cout << pow % 1000000007;
}
Problem p03796 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
res = 1
for i in range(1, n + 1):
    res *= i
res = res % (10**9 + 7)

print(res)


Fixed submission:
n = int(input())
res = 1
for i in range(0, n):
    res *= i + 1
    if res > 10**9 + 7:
        res = res % (10**9 + 7)
print(res)
Problem p03796 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())

i = 1
power = 1
while i <= n:
    power *= i
    i += 1
print(power % (10**9 + 7))


Fixed submission:
n = int(input())

i = 1
power = 1
while i <= n:
    if power >= 10**9 + 7:
        power %= 10**9 + 7
    power *= i
    i += 1
print(power % (10**9 + 7))
Problem p03796 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
ans = 1
for i in range(N):
    ans *= i + 1
print(ans % (10**9 + 7))


Fixed submission:
N = int(input())
ans = 1
for i in range(1, N + 1):
    ans = (i * ans) % (10**9 + 7)
print(ans)