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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define ll long long

using namespace std;

const int N = 1e5 + 10, M = 2e5 + 10;
int n, m, cnt, head[N], x, y, tot, flag, col[N];
ll a, b, c;
struct edge {
  int to, nxt;
} e[N << 1];

void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}

void dfs(int u, int c) {
  ++tot, col[u] = c;
  for (int i = head[u], v; i; i = e[i].nxt) {
    v = e[i].to;
    if (col[v] == -1)
      dfs(v, c ^ 1);
    else if (col[v] == c)
      flag = 1;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, m) scanf("%d%d", &x, &y), adde(x, y), adde(y, x);
  memset(col, -1, sizeof(col));
  rep(i, 1, n) if (col[i] == -1) {
    tot = 0, flag = 0, dfs(i, 0);
    if (tot == 1)
      a++;
    else if (flag)
      b++;
    else
      c++;
  }
  printf("%lld\n",
         a * (2 * n - 1) - a * (a - 1) + c * c * 2 + b * b + b * c * 2);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = (x); i <= (y); i++)
#define ll long long

using namespace std;

const int N = 1e5 + 10, M = 2e5 + 10;
int n, m, cnt, head[N], x, y, tot, flag, col[N];
ll a, b, c;
struct edge {
  int to, nxt;
} e[M << 1];

void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}

void dfs(int u, int c) {
  ++tot, col[u] = c;
  for (int i = head[u], v; i; i = e[i].nxt) {
    v = e[i].to;
    if (col[v] == -1)
      dfs(v, c ^ 1);
    else if (col[v] == c)
      flag = 1;
  }
}

int main() {
  scanf("%d%d", &n, &m);
  rep(i, 1, m) scanf("%d%d", &x, &y), adde(x, y), adde(y, x);
  memset(col, -1, sizeof(col));
  rep(i, 1, n) if (col[i] == -1) {
    tot = 0, flag = 0, dfs(i, 0);
    if (tot == 1)
      a++;
    else if (flag)
      b++;
    else
      c++;
  }
  printf("%lld\n",
         a * (2 * n - 1) - a * (a - 1) + c * c * 2 + b * b + b * c * 2);
  return 0;
}
Problem p03787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 100000 + 5;
typedef long long ll;
struct node {
  int u, v, nxt;
} edge[N * 2];
int head[N], mcnt;
int du[N];
void add_edge(int u, int v) {
  mcnt++;
  edge[mcnt].u = u;
  edge[mcnt].v = v;
  edge[mcnt].nxt = head[u];
  head[u] = mcnt;
  du[u]++;
}
ll I, P, Q;
bool tag[N];
bool vis[N];
bool flag;
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (!vis[v]) {
      tag[v] = tag[u] ^ 1;
      dfs(v);
    } else if (tag[u] == tag[v])
      flag = false;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    if (du[i] == 0) {
      I++;
      continue;
    }
    flag = true;
    dfs(i);
    if (flag)
      Q++;
    else
      P++;
  }
  ll ans =
      1ll * 2 * I * n - 1ll * I * I + 1ll * P * P + 2ll * P * Q + 2ll * Q * Q;
  printf("%lld\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1000000 + 5;
typedef long long ll;
struct node {
  int u, v, nxt;
} edge[N * 2];
int head[N], mcnt;
int du[N];
void add_edge(int u, int v) {
  mcnt++;
  edge[mcnt].u = u;
  edge[mcnt].v = v;
  edge[mcnt].nxt = head[u];
  head[u] = mcnt;
  du[u]++;
}
ll I, P, Q;
bool tag[N];
bool vis[N];
bool flag;
void dfs(int u) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (!vis[v]) {
      tag[v] = tag[u] ^ 1;
      dfs(v);
    } else if (tag[u] == tag[v])
      flag = false;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i])
      continue;
    if (du[i] == 0) {
      I++;
      continue;
    }
    flag = true;
    dfs(i);
    if (flag)
      Q++;
    else
      P++;
  }
  ll ans =
      1ll * 2 * I * n - 1ll * I * I + 1ll * P * P + 2ll * P * Q + 2ll * Q * Q;
  printf("%lld\n", ans);
}
Problem p03788 (C++)

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 100010;
const int MOD = 1000000007;
const int LOG = 20;

int pnt = 0, n, k;
bool f = 0, mark[N]; // marked B unmarked A
string s;

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  cin >> s;
  for (int i = 0; i < n; i++)
    mark[i] = (s[i] == 'B');
  for (int i = 1; i <= min(k, 10000000 + (k % 2)); i++) {
    if ((mark[pnt] ^ f)) {
      pnt++;
      pnt %= n;
      f ^= 1;
    } else {
      mark[pnt] ^= 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ((mark[pnt] ^ f) ? 'B' : 'A');
    pnt++;
    pnt %= n;
  }

  return 0;
}

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

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200010;
const int MOD = 1000000007;
const int LOG = 20;

int pnt = 0, n, k;
bool f = 0, mark[N]; // marked B unmarked A
string s;

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  cin >> s;
  for (int i = 0; i < n; i++)
    mark[i] = (s[i] == 'B');
  for (int i = 1; i <= min(k, 10000000 + (k % 2)); i++) {
    if ((mark[pnt] ^ f)) {
      pnt++;
      pnt %= n;
      f ^= 1;
    } else {
      mark[pnt] ^= 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ((mark[pnt] ^ f) ? 'B' : 'A');
    pnt++;
    pnt %= n;
  }

  return 0;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 4e5 + 10;
const int mod = 0;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, k, a[N], tag = 0;
char s[N];
int main() {
  cin >> n >> k;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] - 'A';
  int l = 1, r = n, cnt = 0;
  while (k && cnt < 2 * n) {
    k--;
    cnt++;
    if (a[l] == tag)
      a[l] ^= 1;
    else
      l++, r++, tag ^= 1, a[r] = tag;
  }
  if (k & 1) {
    k--;
    cnt++;
    if (a[l] == tag)
      a[l] ^= 1;
    else
      l++, r++, tag ^= 1, a[r] = tag;
  }
  for (int i = l; i <= r; i++)
    putchar('A' + (a[i] ^ tag));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int inf = 1e9;
const ll Inf = 1e18;
const int N = 8e5 + 10;
const int mod = 0;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; };
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; };
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, k, a[N], tag = 0;
char s[N];
int main() {
  cin >> n >> k;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] - 'A';
  int l = 1, r = n, cnt = 0;
  while (k && cnt < 2 * n) {
    k--;
    cnt++;
    if (a[l] == tag)
      a[l] ^= 1;
    else
      l++, r++, tag ^= 1, a[r] = tag;
  }
  if (k & 1) {
    k--;
    cnt++;
    if (a[l] == tag)
      a[l] ^= 1;
    else
      l++, r++, tag ^= 1, a[r] = tag;
  }
  for (int i = l; i <= r; i++)
    putchar('A' + (a[i] ^ tag));
  return 0;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>

#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)

using namespace std;

typedef long long LL;
typedef double db;

const int N = 200005;

int a[N * 2];
int n, k;
char s[N];

int main() {
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  fo(i, 1, n) a[i] = s[i] == 'B';
  int nt = 0;
  if (k <= n * 2) {
    int he = 1, ta = n;
    fo(i, 1, k) {
      if ((a[he] ^ nt) == 1) {
        nt ^= 1;
        he++, ta++;
        a[ta] = nt;
      } else
        a[he] ^= 1;
    }
    fo(i, he, ta) if ((a[i] ^ nt) == 1) putchar('B');
    else putchar('A');
  } else {
    if (n & 1)
      putchar('A' + !(k & 1));
    fo(i, 1, n / 2) putchar('B'), putchar('A');
  }
  putchar('\n');
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>

#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)

using namespace std;

typedef long long LL;
typedef double db;

const int N = 400005;

int a[N * 2];
int n, k;
char s[N];

int main() {
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  fo(i, 1, n) a[i] = s[i] == 'B';
  int nt = 0;
  if (k <= n * 2) {
    int he = 1, ta = n;
    fo(i, 1, k) {
      if ((a[he] ^ nt) == 1) {
        nt ^= 1;
        he++, ta++;
        a[ta] = nt;
      } else
        a[he] ^= 1;
    }
    fo(i, he, ta) if ((a[i] ^ nt) == 1) putchar('B');
    else putchar('A');
  } else {
    if (n & 1)
      putchar('A' + !(k & 1));
    fo(i, 1, n / 2) putchar('B'), putchar('A');
  }
  putchar('\n');
  return 0;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#define maxn 100010
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
deque<int> d;
int n, tag, K;
char s[maxn];
int a[maxn];
int main() {
  n = read();
  K = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] - 'A';
  if (K > 2 * n)
    K = 2 * n + (K - 2 * n) % 2;
  for (int i = 1; i <= n; i++)
    d.push_back(a[i]);
  while (K--) {
    if (!(d.front() ^ tag)) {
      d.pop_front();
      d.push_front(tag ^ 1);
    } else {
      d.pop_front();
      tag ^= 1;
      d.push_back(tag);
    }
  }
  while (d.size()) {
    int now = (d.front() ^ tag);
    d.pop_front();
    printf("%c", (char)(now + 'A'));
  }
  puts("");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#define maxn 200010
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch - '0' < 0 || ch - '0' > 9) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch - '0' >= 0 && ch - '0' <= 9) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
deque<int> d;
int n, tag, K;
char s[maxn];
int a[maxn];
int main() {
  n = read();
  K = read();
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    a[i] = s[i] - 'A';
  if (K > 2 * n)
    K = 2 * n + (K - 2 * n) % 2;
  for (int i = 1; i <= n; i++)
    d.push_back(a[i]);
  while (K--) {
    if (!(d.front() ^ tag)) {
      d.pop_front();
      d.push_front(tag ^ 1);
    } else {
      d.pop_front();
      tag ^= 1;
      d.push_back(tag);
    }
  }
  while (d.size()) {
    int now = (d.front() ^ tag);
    d.pop_front();
    printf("%c", (char)(now + 'A'));
  }
  puts("");
  return 0;
}
Problem p03788 (C++)

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

int N, K;
char V[400200];

int main() {
  scanf("%d %d %s", &N, &K, V);
  if (K > 2 * N)
    K = 2 * N + (K - 2 * N) % 2;

  int idx = 0, neg = 0;
  while (K--) {
    if (V[idx] == 'A' ^ neg)
      V[idx] = !(V[idx] - 'A') + 'A';
    else {
      neg = !neg;
      V[idx + N] = neg ? 'B' : 'A';
      idx++;
    }
  }

  if (neg) {
    for (int i = idx; i < idx + N; i++)
      V[i] = !(V[i] - 'A') + 'A';
  }
  puts(V + idx);

  return 0;
}


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

int N, K;
char V[600200];

int main() {
  scanf("%d %d %s", &N, &K, V);
  if (K > 2 * N)
    K = 2 * N + (K - 2 * N) % 2;

  int idx = 0, neg = 0;
  while (K--) {
    if (V[idx] == 'A' ^ neg)
      V[idx] = !(V[idx] - 'A') + 'A';
    else {
      neg = !neg;
      V[idx + N] = neg ? 'B' : 'A';
      idx++;
    }
  }

  if (neg) {
    for (int i = idx; i < idx + N; i++)
      V[i] = !(V[i] - 'A') + 'A';
  }
  puts(V + idx);

  return 0;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
deque<int> deq;
int n, k;
string s;
int main() {
  cin >> n >> k >> s;
  rep(i, n) { deq.pb(s[i] - 'A'); }
  int rev = 0;
  rep(i, min(10000000, k)) {
    if ((rev ^ deq[0]) == 0) {
      deq[0] ^= 1;
    } else {
      deq.pop_front();
      rev ^= 1;
      deq.pb(rev);
    }
  }
  if (10000000 >= k) {
    rep(i, n) { printf("%c", ('A' + (rev ^ deq[i]))); }
  } else {
    for (int i = 1; i < n; i++) {
      assert(deq[i] != deq[i - 1]);
    }
    int zan = k - 10000000;
    zan %= 2;
    rep(i, zan) {
      if ((rev ^ deq[0]) == 0) {
        deq[0] ^= 1;
      } else {
        deq.pop_front();
        rev ^= 1;
        deq.pb(rev);
      }
    }
    rep(i, n) { printf("%c", ('A' + (rev ^ deq[i]))); }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
deque<int> deq;
int n, k;
string s;
int main() {
  cin >> n >> k >> s;
  rep(i, n) { deq.pb(s[i] - 'A'); }
  int rev = 0;
  rep(i, min(10000000, k)) {
    if ((rev ^ deq[0]) == 0) {
      deq[0] ^= 1;
    } else {
      deq.pop_front();
      rev ^= 1;
      deq.pb(rev);
    }
  }
  if (10000000 >= k) {
    rep(i, n) { printf("%c", ('A' + (rev ^ deq[i]))); }
  } else {
    for (int i = 1; i < n; i++) {
      // assert(deq[i]!=deq[i-1]);
    }
    int zan = k - 10000000;
    zan %= 2;
    rep(i, zan) {
      if ((rev ^ deq[0]) == 0) {
        deq[0] ^= 1;
      } else {
        deq.pop_front();
        rev ^= 1;
        deq.pb(rev);
      }
    }
    rep(i, n) { printf("%c", ('A' + (rev ^ deq[i]))); }
  }
}
Problem p03788 (C++)

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

const int N = 100010;

char s[N];
int n, k;
std::deque<int> dq;

int main() {
  scanf("%d%d%s", &n, &k, s);
  for (int i = 0; i < n; ++i) {
    dq.push_back(s[i] - 'A');
  }
  if (k > n << 1) {
    k = (n << 1) + (k & 1);
  }
  int flag = 0;
  for (int i = 0; i < k; ++i) {
    int x = dq.front();
    if (x == flag) {
      dq.pop_front();
      dq.push_front(flag ^ 1);
    } else {
      dq.pop_front();
      flag ^= 1;
      dq.push_back(flag);
    }
  }
  while (!dq.empty()) {
    int x = dq.front();
    putchar((x ^ flag) + 'A');
    dq.pop_front();
  }
  return printf("\n"), 0;
}

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

const int N = 200010;

char s[N];
int n, k;
std::deque<int> dq;

int main() {
  scanf("%d%d%s", &n, &k, s);
  for (int i = 0; i < n; ++i) {
    dq.push_back(s[i] - 'A');
  }
  if (k > n << 1) {
    k = (n << 1) + (k & 1);
  }
  int flag = 0;
  for (int i = 0; i < k; ++i) {
    int x = dq.front();
    if (x == flag) {
      dq.pop_front();
      dq.push_front(flag ^ 1);
    } else {
      dq.pop_front();
      flag ^= 1;
      dq.push_back(flag);
    }
  }
  while (!dq.empty()) {
    int x = dq.front();
    putchar((x ^ flag) + 'A');
    dq.pop_front();
  }
  return printf("\n"), 0;
}
Problem p03788 (C++)

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

typedef unsigned long long ULL;
const int N = 3e3 + 50;
const ULL base = 31;

int n, k, tag;
char ch[N];
ULL pw[N], val[2];

tr1::unordered_map<ULL, int> s;
deque<int> q;

inline void print() {
  for (int i = 1; i <= n; i++)
    putchar((q[i - 1] ^ tag) ? 'B' : 'A');
}
inline void rec() {
  if (!(q[0] ^ tag)) {
    q[0] ^= 1;
    return;
  }
  q.pop_front();
  tag ^= 1;
  q.push_back(tag);
}

int main() {
  scanf("%d%d%s", &n, &k, ch + 1);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * base;
  for (int i = 1; i <= n; ++i)
    q.push_back((ch[i] == 'A') ? 0 : 1);

  if (k <= 2 * n) {
    for (int i = 1; i <= k; i++)
      rec();
    print();
  } else {
    for (int i = 0; i < n; i++) {
      val[0] += q[i] * pw[n - i];
      val[1] += (q[i] ^ 1) * pw[n - i];
    }
    s[val[0]] = 0;
    int cir = 0, i;
    for (i = 1;; ++i) {
      if (!(q[0] ^ tag)) {
        val[1] -= pw[n];
        val[0] += pw[n];
        q[0] ^= 1;
      } else {
        val[0] -= (q[0] ^ tag) * pw[n];
        val[1] -= (q[0] ^ tag ^ 1) * pw[n];
        rec();
        swap(val[0], val[1]);
        val[0] *= base;
        val[1] *= base;
        val[1] += base;
      }
      if (s.count(val[0])) {
        cir = i - s[val[0]];
        break;
      } else
        s[val[0]] = i;
    }
    int t = (k - i) % cir;
    while (t--)
      rec();
    print();
  }
}

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

typedef unsigned long long ULL;
const int N = 2e5 + 50;
const ULL base = 31;

int n, k, tag;
char ch[N];
ULL pw[N], val[2];

tr1::unordered_map<ULL, int> s;
deque<int> q;

inline void print() {
  for (int i = 1; i <= n; i++)
    putchar((q[i - 1] ^ tag) ? 'B' : 'A');
}
inline void rec() {
  if (!(q[0] ^ tag)) {
    q[0] ^= 1;
    return;
  }
  q.pop_front();
  tag ^= 1;
  q.push_back(tag);
}

int main() {
  scanf("%d%d%s", &n, &k, ch + 1);
  pw[0] = 1;
  for (int i = 1; i <= n; i++)
    pw[i] = pw[i - 1] * base;
  for (int i = 1; i <= n; ++i)
    q.push_back((ch[i] == 'A') ? 0 : 1);

  if (k <= 2 * n) {
    for (int i = 1; i <= k; i++)
      rec();
    print();
  } else {
    for (int i = 0; i < n; i++) {
      val[0] += q[i] * pw[n - i];
      val[1] += (q[i] ^ 1) * pw[n - i];
    }
    s[val[0]] = 0;
    int cir = 0, i;
    for (i = 1;; ++i) {
      if (!(q[0] ^ tag)) {
        val[1] -= pw[n];
        val[0] += pw[n];
        q[0] ^= 1;
      } else {
        val[0] -= (q[0] ^ tag) * pw[n];
        val[1] -= (q[0] ^ tag ^ 1) * pw[n];
        rec();
        swap(val[0], val[1]);
        val[0] *= base;
        val[1] *= base;
        val[1] += base;
      }
      if (s.count(val[0])) {
        cir = i - s[val[0]];
        break;
      } else
        s[val[0]] = i;
    }
    int t = (k - i) % cir;
    while (t--)
      rec();
    print();
  }
}
Problem p03788 (C++)

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

using namespace std;

using int64 = long long;
bool bit[200001];

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  for (int i = 0; i < N; i++)
    bit[i] = S[i] == 'A';

  int last = N;
  while (K--) {
    while (last >= 2 && bit[last - 1] && !bit[last - 2]) {
      last -= 2;
    }
    if (last == 0)
      break;
    if (last == 1)
      last %= 2;
    if (bit[0]) {
      bit[0] = false;
    } else {
      for (int i = 0; i < last; i++)
        bit[i] = !bit[i + 1];
    }
  }

  for (int i = 0; i < N; i++)
    cout << "BA"[bit[i]];
  cout << endl;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

using int64 = long long;
bool bit[200001];

int main() {
  int N, K;
  cin >> N >> K;
  string S;
  cin >> S;

  for (int i = 0; i < N; i++)
    bit[i] = S[i] == 'A';

  int last = N;
  while (K--) {
    while (last >= 2 && bit[last - 1] && !bit[last - 2]) {
      last -= 2;
    }
    if (last == 0)
      break;
    if (last == 1)
      K %= 2;
    if (bit[0]) {
      bit[0] = false;
    } else {
      for (int i = 0; i < last; i++)
        bit[i] = !bit[i + 1];
    }
  }

  for (int i = 0; i < N; i++)
    cout << "BA"[bit[i]];
  cout << endl;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k, num[N], p = 1, cnt;
char s[N];
signed main() {
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    num[i] = s[i] - 'A';
  int st = num[1];
  while (k--) {
    if (st == 0) {
      st ^= 1;
      num[p] ^= 1;
    } else {
      p++;
      cnt++;
      st = num[p];
      st ^= (cnt & 1);
    }
    if (p > n)
      break;
  }
  if (p <= n) {
    for (int i = p; i <= n; i++)
      putchar('A' + (num[i] ^ (cnt & 1)));
    int t = n - (n - p + 1), c = (cnt - 1) & 1;
    while (t--) {
      putchar('A' + c);
      c ^= 1;
    }
  } else {
    if ((cnt - 1) & 1) {
      for (int i = 1; i <= n; i++)
        putchar('A' + (i & 1));
    } else {
      putchar('A' + (k & 1));
      for (int i = 1; i < n; i++)
        putchar('A' + (i & 1));
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, k, num[N], p = 1, cnt;
char s[N];
signed main() {
  scanf("%d%d", &n, &k);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++)
    num[i] = s[i] - 'A';
  int st = num[1];
  while (k--) {
    if (st == 0) {
      st ^= 1;
      num[p] ^= 1;
    } else {
      p++;
      cnt++;
      st = num[p];
      st ^= (cnt & 1);
    }
    if (p > n)
      break;
  }
  if (p <= n) {
    for (int i = p; i <= n; i++)
      putchar('A' + (num[i] ^ (cnt & 1)));
    int t = n - (n - p + 1), c = (cnt - 1) & 1;
    while (t--) {
      putchar('A' + c);
      c ^= 1;
    }
  } else {
    if ((cnt - 1) & 1) {
      for (int i = 1; i <= n; i++)
        putchar('A' + (i & 1));
    } else {
      putchar('A' + (k & 1));
      for (int i = 1; i < n; i++)
        putchar('A' + (i & 1));
    }
  }
  return 0;
}
Problem p03788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mo 1000000007
#define pi 3.1415926535898
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
long long ksm(long long u, long long v) {
  long long o = 1;
  while (v) {
    if (v & 1)
      o = o * u % mo;
    u = u * u % mo;
    v >>= 1;
  }
  return o;
}
int n, k, i, sum;
bool a[100005], flag;
char s[100005];
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  k = read();
  scanf("%s", s);
  for (i = 0; i < n; i++)
    if (s[i] == 'A')
      a[i] = 0;
    else
      a[i] = 1;
  while (k--) {
    if ((a[sum] ^ flag) == 0)
      a[sum] ^= 1;
    else {
      sum++;
      flag ^= 1;
      sum = (sum >= n ? sum - n : sum);
    }
  }
  for (i = sum; i < n; i++)
    if ((a[i] ^ flag) == 0)
      putchar('A');
    else
      putchar('B');
  for (i = 0; i < sum; i++)
    if ((a[i] ^ flag) == 0)
      putchar('A');
    else
      putchar('B');
  cout << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define mo 1000000007
#define pi 3.1415926535898
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10)
    pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1)
    putchar(' ');
  if (flagg == 2)
    putchar('\n');
  return;
}
long long ksm(long long u, long long v) {
  long long o = 1;
  while (v) {
    if (v & 1)
      o = o * u % mo;
    u = u * u % mo;
    v >>= 1;
  }
  return o;
}
int n, k, i, sum;
bool a[200005], flag;
char s[200005];
int main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  n = read();
  k = read();
  scanf("%s", s);
  for (i = 0; i < n; i++)
    if (s[i] == 'A')
      a[i] = 0;
    else
      a[i] = 1;
  while (k--) {
    if ((a[sum] ^ flag) == 0)
      a[sum] ^= 1;
    else {
      sum++;
      flag ^= 1;
      sum = (sum >= n ? sum - n : sum);
    }
  }
  for (i = sum; i < n; i++)
    if ((a[i] ^ flag) == 0)
      putchar('A');
    else
      putchar('B');
  for (i = 0; i < sum; i++)
    if ((a[i] ^ flag) == 0)
      putchar('A');
    else
      putchar('B');
  cout << endl;
  return 0;
}
Problem p03789 (C++)

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

using namespace std;

typedef long long ll;
typedef pair<int, int> par;

char cad[maxn];
int num[maxn], l, K[maxn], lk, sum[maxn];

bool check(int k) {
  memset(K, 0, sizeof K);
  int tmp = 9 * k;
  lk = 0;
  while (tmp) {
    K[++lk] = tmp % 10;
    tmp /= 10;
  }
  int maxl = max(l, lk), s = 0, a;
  for (int i = 1; i <= maxl; i++) {
    a = (num[i] + K[i] + s);
    sum[i] = a % 10;
    s = a / 10;
  }
  if (s)
    sum[++maxl] = s;
  int acc = accumulate(sum + 1, sum + 1 + maxl, 0);

  return acc <= 9 * k;
}

int main() {
#ifdef home
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif // home
  ios_base::sync_with_stdio(0), cin.tie(0);

  cin >> cad + 1;
  l = strlen(cad + 1);
  reverse(cad + 1, cad + 1 + l);
  for (int i = 1; i <= l; i++)
    num[i] = cad[i] - '0';

  int s = 0, a;
  for (int i = 1; i <= l; i++) {
    a = (num[i] * 9 + s);
    num[i] = a % 10;
    s = a / 10;
  }
  if (s)
    num[++l] = s;

  int ini = 1, fin = l, piv;
  while (ini != fin) {
    piv = (ini + fin) >> 1;
    if (check(piv))
      fin = piv;
    else
      ini = piv + 1;
  }
  cout << ini;
  return 0;
}


Fixed submission:
/// Ernestico
#include <bits/stdc++.h>
#define maxn 500005

using namespace std;

typedef long long ll;
typedef pair<int, int> par;

char cad[maxn];
int num[maxn], l, K[maxn], lk, sum[maxn];

bool check(int k) {
  memset(K, 0, sizeof K);
  int tmp = 9 * k;
  lk = 0;
  while (tmp) {
    K[++lk] = tmp % 10;
    tmp /= 10;
  }
  int maxl = max(l, lk), s = 0, a;
  for (int i = 1; i <= maxl; i++) {
    a = (num[i] + K[i] + s);
    sum[i] = a % 10;
    s = a / 10;
  }
  if (s)
    sum[++maxl] = s;
  int acc = accumulate(sum + 1, sum + 1 + maxl, 0);

  return acc <= 9 * k;
}

int main() {
#ifdef home
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif // home
  ios_base::sync_with_stdio(0), cin.tie(0);

  cin >> cad + 1;
  l = strlen(cad + 1);
  reverse(cad + 1, cad + 1 + l);
  for (int i = 1; i <= l; i++)
    num[i] = cad[i] - '0';

  int s = 0, a;
  for (int i = 1; i <= l; i++) {
    a = (num[i] * 9 + s);
    num[i] = a % 10;
    s = a / 10;
  }
  if (s)
    num[++l] = s;

  int ini = 1, fin = l, piv;
  while (ini != fin) {
    piv = (ini + fin) >> 1;
    if (check(piv))
      fin = piv;
    else
      ini = piv + 1;
  }
  cout << ini;
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void read(long long &x) {
  char ch;
  bool ok;
  for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      ok = 1;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  if (ok)
    x = -x;
}
#define rg register
const int maxn = 5e5 + 10;
char a[maxn];
int ans, n, s[maxn], w[10];
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  for (rg int i = 1; i <= n; i++)
    s[i] = a[i] - '0';
  int k = 0;
  for (rg int i = 1; i <= n; i++) {
    int las = n, d = i;
    if (s[k + 1] >= s[k])
      k = 0;
    for (rg int j = d + 1; j <= n; j++) {
      if (s[j] < s[j - 1]) {
        las = j - 1;
        break;
      }
      if (k >= j && s[j] == s[j - 1]) {
        las = j - 1;
        break;
      }
    }
    if (las == n) {
      ans++;
      break;
    }
    if (s[las] == s[las - 1]) {
      k = las;
      for (rg int j = las; j; j--)
        if (s[j] != s[j - 1]) {
          las = j;
          break;
        }
    }
    ans++, s[n]++;
    int now = n;
    s[i] = 0;
    while (now && s[now] >= 10)
      s[now - 1] += s[now] / 10, s[now] %= 10, now--;
    i = las;
  }
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void read(long long &x) {
  char ch;
  bool ok;
  for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      ok = 1;
  for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
    ;
  if (ok)
    x = -x;
}
#define rg register
const int maxn = 5e5 + 10;
char a[maxn];
int ans, n, s[maxn], w[10];
int main() {
  scanf("%s", a + 1);
  n = strlen(a + 1);
  for (rg int i = 1; i <= n; i++)
    s[i] = a[i] - '0';
  int k = 0;
  for (rg int i = 1; i <= n; i++) {
    int las = n, d = i;
    if (s[k + 1] >= s[k])
      k = 0;
    for (rg int j = d + 1; j <= n; j++) {
      if (s[j] < s[j - 1]) {
        las = j - 1;
        break;
      }
      if (k >= j && s[j] == s[j - 1]) {
        las = j - 1;
        break;
      }
    }
    if (las == n) {
      ans++;
      break;
    }
    if (s[las] == s[las - 1]) {
      k = las;
      for (rg int j = las; j; j--)
        if (s[j] != s[j - 1]) {
          las = j;
          break;
        }
    }
    ans++, s[n]++;
    int now = n;
    s[i] = s[las] = 0;
    while (now && s[now] >= 10)
      s[now - 1] += s[now] / 10, s[now] %= 10, now--;
    i = las;
  }
  printf("%d\n", ans);
}
Problem p03789 (C++)

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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

const int N = 1e6 + 5;
int a[N];
string s;

int main() {
  cin >> s;
  int n = s.size();
  for (int i = 1; i <= n; ++i)
    a[i] = s[n - i] - '0';

  for (int i = 1; i <= n; ++i)
    a[i] *= 9;
  for (int i = 1; i <= n; ++i)
    a[i + 1] += a[i] / 10, a[i] %= 10;
  if (a[n + 1])
    ++n;

  int tot = 0;
  for (int i = 1; i <= n; ++i)
    tot += a[i];
  for (int k = 1, i;; ++k) {
    a[i = 1] += 9;
    tot += 9;
    for (; a[i] >= 10; ++i) {
      a[i + 1] += a[i] / 10;
      a[i] %= 10;
      tot -= 9;
    }
    if (tot <= 9 * k && tot % 9 == 0)
      return printf("%d\n", k);
  }

  return 0;
}


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

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return x * f;
}

const int N = 1e6 + 5;
int a[N];
string s;

int main() {
  cin >> s;
  int n = s.size();
  for (int i = 1; i <= n; ++i)
    a[i] = s[n - i] - '0';

  for (int i = 1; i <= n; ++i)
    a[i] *= 9;
  for (int i = 1; i <= n; ++i)
    a[i + 1] += a[i] / 10, a[i] %= 10;
  if (a[n + 1])
    ++n;

  int tot = 0;
  for (int i = 1; i <= n; ++i)
    tot += a[i];
  for (int k = 1, i;; ++k) {
    a[i = 1] += 9;
    tot += 9;
    for (; a[i] >= 10; ++i) {
      a[i + 1] += a[i] / 10;
      a[i] %= 10;
      tot -= 9;
    }
    if (tot <= 9 * k && tot % 9 == 0)
      return printf("%d\n", k), 0;
  }

  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 5e5 + 15;
using namespace std;

char s[N];
int a[N];

int main() {
  scanf("%s", s + 1);
  a[0] = strlen(s + 1);
  int L = a[0], sum = 0;
  rep(i, 1, a[0]) a[a[0] - i + 1] = s[i] - '0';
  rep(i, 1, a[0]) a[i] *= 9;
  rep(i, 1, a[0]) {
    a[i + 1] += a[i] / 10;
    a[i] %= 10, sum += a[i];
    if (a[a[0] + 1])
      ++a[0];
  }
  rep(k, 1, L) {
    a[1] += 9, sum += 9;
    rep(i, 1, a[0]) {
      a[i + 1] += a[i] / 10, sum += a[i] / 10;
      sum -= a[i], a[i] %= 10, sum += a[i];
      if (a[a[0] + 1])
        ++a[0];
    }
    if (sum <= 9 * k) {
      printf("%d\n", k);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
typedef long long ll;
#define rep(i, a, b) for (int i = a; i <= b; ++i)
const int N = 5e5 + 15;
using namespace std;

char s[N];
int a[N];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAXN 300005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
char s[MAXN];
int n, a[MAXN];
int func(int x) {
  int ans = 0;
  while (x) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}
bool check(int value) {
  int ans = 0, now = value * 9;
  for (int i = n; i >= 1; i--) {
    now += a[i] * 9;
    ans += now % 10;
    now /= 10;
  }
  return ans + func(now) <= value * 9;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = n; i >= 1; i--)
    a[i] = s[i] - '0';
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAXN 500005
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
char s[MAXN];
int n, a[MAXN];
int func(int x) {
  int ans = 0;
  while (x) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}
bool check(int value) {
  int ans = 0, now = value * 9;
  for (int i = n; i >= 1; i--) {
    now += a[i] * 9;
    ans += now % 10;
    now /= 10;
  }
  return ans + func(now) <= value * 9;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = n; i >= 1; i--)
    a[i] = s[i] - '0';
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
char S[1000010];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  scanf("%s", S + 1);
  int n = strlen(S + 1);
  for (int i = 1; i <= n; ++i)
    S[i] -= '0';
  std::reverse(S + 1, S + n + 1);
  n <<= 1;
  for (int i = 1; i <= n; ++i)
    S[i] *= 9;
  for (int i = 1; i <= n; ++i)
    if (S[i] >= 10)
      S[i + 1] += S[i] / 10, S[i] %= 10;
  ll sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += S[i];
  for (int i = 1; i <= n; ++i) {
    S[1] += 9;
    sum += 9;
    for (int j = 1; j <= n; ++j)
      if (S[j] >= 10)
        sum += S[j] % 10 + S[j] / 10 - S[j], S[j + 1] += S[j] / 10, S[j] %= 10;
    if (sum <= 9 * i) {
      printf("%d\n", i);
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
char S[1000010];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  scanf("%s", S + 1);
  int n = strlen(S + 1);
  for (int i = 1; i <= n; ++i)
    S[i] -= '0';
  std::reverse(S + 1, S + n + 1);
  n <<= 1;
  for (int i = 1; i <= n; ++i)
    S[i] *= 9;
  for (int i = 1; i <= n; ++i)
    if (S[i] >= 10)
      S[i + 1] += S[i] / 10, S[i] %= 10;
  ll sum = 0;
  for (int i = 1; i <= n; ++i)
    sum += S[i];
  for (int i = 1; i <= n; ++i) {
    S[1] += 9;
    sum += 9;
    for (int j = 1; j <= n; ++j)
      if (S[j] >= 10)
        sum += S[j] % 10 + S[j] / 10 - S[j], S[j + 1] += S[j] / 10, S[j] %= 10;
      else
        break;
    if (sum <= 9 * i) {
      printf("%d\n", i);
      break;
    }
  }
  return 0;
}
Problem p03789 (C++)

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

const int N = 500005;
const int maxn = N * 5;

int tot, a[maxn + 5];
char str[N];

void mul(int x) {
  int s, g = 0;
  for (int i = maxn; i >= 1; i--) {
    s = a[i] * x + g;
    a[i] = s % 10;
    g = s / 10;
  }
}

void add(int x) {
  a[maxn] += x;
  tot += x;
  for (int i = maxn; i >= 1 && a[i] >= 10; i--) {
    tot -= a[i - 1] + a[i];
    a[i - 1]++;
    a[i] -= 10;
    tot += a[i - 1] + a[i];
  }
}

int main() {
  scanf("%s", str);
  int len = strlen(str);
  for (int i = 0; i < len; i++)
    a[maxn - i] = str[len - i - 1] - '0';
  mul(9);
  for (int i = 1; i <= maxn; i++)
    tot += a[i];
  for (int k = 1;; k++) {
    add(9);
    if (k * 9 == tot) {
      printf("%d", k);
      break;
    }
  }
  return 0;
}

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

const int N = 500005;
const int maxn = N * 5;

int tot, a[maxn + 5];
char str[N];

void mul(int x) {
  int s, g = 0;
  for (int i = maxn; i >= 1; i--) {
    s = a[i] * x + g;
    a[i] = s % 10;
    g = s / 10;
  }
}

void add(int x) {
  a[maxn] += x;
  tot += x;
  for (int i = maxn; i >= 1 && a[i] >= 10; i--) {
    tot -= a[i - 1] + a[i];
    a[i - 1]++;
    a[i] -= 10;
    tot += a[i - 1] + a[i];
  }
}

int main() {
  scanf("%s", str);
  int len = strlen(str);
  for (int i = 0; i < len; i++)
    a[maxn - i] = str[len - i - 1] - '0';
  mul(9);
  for (int i = 1; i <= maxn; i++)
    tot += a[i];
  for (int k = 1;; k++) {
    add(9);
    if (k * 9 >= tot) {
      printf("%d", k);
      break;
    }
  }
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long LL;
const int maxn = 100005;

char str[maxn];
int n[maxn], L, sum;
int main() {
#ifdef Amberframe
  freopen("agc011e.in", "r", stdin);
  freopen("agc011e.out", "w", stdout);
#endif
  scanf("%s", str + 1);
  L = strlen(str + 1);
  for (int i = 1; i <= L; i++)
    n[i] = str[L - i + 1] - '0';
  for (int t = 0, i = 1; i <= L + 10; i++)
    n[i] = n[i] * 9 + t, t = n[i] / 10, n[i] %= 10, sum += n[i];
  for (int k = 0;; k++) {
    if (sum <= 9 * k) {
      printf("%d\n", k);
      return 0;
    } else {
      n[1] += 9;
      sum += 9;
      int p = 1;
      while (n[p] >= 10)
        n[p] -= 10, sum -= 9, n[++p]++;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <vector>

using namespace std;
typedef long long LL;
const int maxn = 600005;

char str[maxn];
int n[maxn], L, sum;
int main() {
#ifdef Amberframe
  freopen("agc011e.in", "r", stdin);
  freopen("agc011e.out", "w", stdout);
#endif
  scanf("%s", str + 1);
  L = strlen(str + 1);
  for (int i = 1; i <= L; i++)
    n[i] = str[L - i + 1] - '0';
  for (int t = 0, i = 1; i <= L + 10; i++)
    n[i] = n[i] * 9 + t, t = n[i] / 10, n[i] %= 10, sum += n[i];
  for (int k = 0;; k++) {
    if (sum <= 9 * k) {
      printf("%d\n", k);
      return 0;
    } else {
      n[1] += 9;
      sum += 9;
      int p = 1;
      while (n[p] >= 10)
        n[p] -= 10, sum -= 9, n[++p]++;
    }
  }
  return 0;
}
Problem p03789 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 200005;

int n, l, r, mid, len;
int a[N];
char s[N];

bool check(int w) {
  int i, sum;
  len = n;
  for (i = 1; i <= n; i++)
    a[i] = (s[i] - '0') * 9;
  a[1] += 9 * w;
  for (i = 1; i <= n; i++) {
    a[i + 1] += a[i] / 10;
    a[i] = a[i] % 10;
  }
  while (a[len + 1]) {
    len++;
    a[len + 1] += a[len] / 10;
    a[len] = a[len] % 10;
  }
  sum = 0;
  for (i = 1; i <= len; i++)
    sum += a[i];
  for (i = 1; i <= len; i++)
    a[i] = 0;
  if (sum <= 9 * w)
    return 1;
  return 0;
}

int main() {
  int i;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  reverse(s + 1, s + 1 + n);
  l = 1;
  r = n;
  while (l < r) {
    mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}

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

const int N = 1000005;

int n, l, r, mid, len;
int a[N];
char s[N];

bool check(int w) {
  int i, sum;
  len = n;
  for (i = 1; i <= n; i++)
    a[i] = (s[i] - '0') * 9;
  a[1] += 9 * w;
  for (i = 1; i <= n; i++) {
    a[i + 1] += a[i] / 10;
    a[i] = a[i] % 10;
  }
  while (a[len + 1]) {
    len++;
    a[len + 1] += a[len] / 10;
    a[len] = a[len] % 10;
  }
  sum = 0;
  for (i = 1; i <= len; i++)
    sum += a[i];
  for (i = 1; i <= len; i++)
    a[i] = 0;
  if (sum <= 9 * w)
    return 1;
  return 0;
}

int main() {
  int i;
  scanf("%s", s + 1);
  n = strlen(s + 1);
  reverse(s + 1, s + 1 + n);
  l = 1;
  r = n;
  while (l < r) {
    mid = l + (r - l) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}
Problem p03789 (C++)

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

int n[500000];
int x[510000];

void input() {
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    n[s.length() - 1 - i] = s[i] - '0';
  }
}

bool check(int k) {
  int i;

  for (i = 0; i < 505000; i++)
    x[i] = 0;
  for (i = 0; i < 505000; i++)
    x[i] = n[i] * 9;
  x[0] += 9 * k;

  for (i = 0; i < 505000; i++) {
    x[i + 1] += x[i] / 10;
    x[i] %= 10;
  }

  int digit_sum = 0;
  for (i = 0; i < 505000; i++) {
    digit_sum += x[i];
  }
  return (digit_sum <= 9 * k);
}

int main() {
  input();

  int st = 0, ed = 500001, mid; // xxxooo, (st, ed]
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if (check(mid))
      ed = mid;
    else
      st = mid;
  }
  cout << ed << endl;

  return 0;
}

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

int n[510000];
int x[510000];

void input() {
  string s;
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    n[s.length() - 1 - i] = s[i] - '0';
  }
}

bool check(int k) {
  int i;

  for (i = 0; i < 505000; i++)
    x[i] = 0;
  for (i = 0; i < 505000; i++)
    x[i] = n[i] * 9;
  x[0] += 9 * k;

  for (i = 0; i < 505000; i++) {
    x[i + 1] += x[i] / 10;
    x[i] %= 10;
  }

  int digit_sum = 0;
  for (i = 0; i < 505000; i++) {
    digit_sum += x[i];
  }
  return (digit_sum <= 9 * k);
}

int main() {
  input();

  int st = 0, ed = 500001, mid; // xxxooo, (st, ed]
  while (ed - st >= 2) {
    mid = (st + ed) / 2;
    if (check(mid))
      ed = mid;
    else
      st = mid;
  }
  cout << ed << endl;

  return 0;
}
Problem p03789 (C++)

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

const int N = 500010;

int len, sum;
char s[N];

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> s, len = std::strlen(s);
  for (int i = 0; i < len; ++i)
    (s[i] -= '0') *= 9;
  std::reverse(s, s + len);
  for (int i = 0; i < len; ++i)
    s[i + 1] += s[i] / 10, s[i] %= 10;
  if (s[len])
    ++len;
  for (int i = 0; i < len; ++i)
    sum += s[i];
  for (int k = 0;;) {
    sum += 10 - s[0], k += 10 - s[0], s[0] = 10;
    for (int i = 0; i < len; ++i)
      if (s[i] == 10)
        ++s[i + 1], s[i] = 0, sum -= 9;
    if (s[len])
      ++len;
    if (sum <= k)
      return std::cout << (k + 8) / 9 << '\n', 0;
  }
  return 0;
}

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

const int N = 500010;

int len, sum;
char s[N];

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> s, len = std::strlen(s);
  for (int i = 0; i < len; ++i)
    (s[i] -= '0') *= 9;
  std::reverse(s, s + len);
  for (int i = 0; i < len; ++i)
    s[i + 1] += s[i] / 10, s[i] %= 10;
  if (s[len])
    ++len;
  for (int i = 0; i < len; ++i)
    sum += s[i];
  for (int k = 0;;) {
    sum += 10 - s[0], k += 10 - s[0], s[0] = 10;
    for (int i = 0; i < len; ++i)
      if (s[i] == 10)
        ++s[i + 1], s[i] = 0, sum -= 9;
      else
        break;
    if (s[len])
      ++len;
    if (sum <= k)
      return std::cout << (k + 8) / 9 << '\n', 0;
  }
  return 0;
}