Problem p03789 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (int i = int(x); i < int(y); ++i)
#define For(i, x, y) for (int i = int(x); i <= int(y); ++i)
#define FOr(i, x, y) for (int i = int(x); i >= int(y); --i)
#define pi acos(-1)
#define mk make_pair
#define pa pair<ll, ll>
#define lf else if
#define IL inline
#define max(x, y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define Mul(x, y) ((x) = 1LL * (x) * (y) % mod)
#define Add(x, y) ((x) = ((x) + (y)) % mod)
#define Max(x, y) ((x) = ((x) < (y) ? (y) : (x)))
#define Min(x, y) ((x) = ((x) > (y) ? (y) : (x)))
#define E(x) return writeln(x), 0
#define LL (long long)
#define p(x) printf("~%lld~\n", LL(x))
#define pp(x, y) printf("~~%lld %lld~~\n", LL(x), LL(y))
#define ppp(x, y, z) printf("~~~%lld %lld %lld~~~\n", LL(x), LL(y), LL(z))
#define pppp(a, b, c, d)                                                       \
  printf("~~~%lld %lld %lld %lld\n", LL(a), LL(b), LL(c), LL(d))
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
#define GuYue                                                                  \
  puts("\nGuYueNa____________________________________________________________" \
       "____________________")
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
static const int GYN = 2333333;
char SZB[GYN], *SS = SZB, *TT = SZB;
inline char gc() {
  if (SS == TT) {
    TT = (SS = SZB) + fread(SZB, 1, GYN, stdin);
    if (SS == TT)
      return '\n';
  }
  return *SS++;
}
#if NEG
inline ll read() {
  ll x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      g = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) { For(i, s, t) printf("%lld ", a[i]); }
void Print(int *a, int s, int t) { For(i, s, t) printf("%d ", a[i]); }
void Print(char *a, int s, int t) { For(i, s, t) putchar(a[i]); }
void writeln(ll x) {
  write(x);
  puts("");
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const int N = 400105;
ll a[N], n;
char s[N];
bool check(ll x) {
  memset(a, 0, sizeof a);
  For(i, 1, n) a[i] = (s[n - i + 1] - '0') * 9;
  a[1] += x * 9;
  ll sum = 0;
  For(i, 1, n + 100) {
    for (; a[i] > 9; a[i] -= 10, a[i + 1]++)
      ;
    sum += a[i];
  }
  return sum <= x * 9;
}
int main() {
  n = readstr(s + 1);
  ll l = 0, r = n * 9, ans = 0;
  for (; l <= r;) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  writeln(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (int i = int(x); i < int(y); ++i)
#define For(i, x, y) for (int i = int(x); i <= int(y); ++i)
#define FOr(i, x, y) for (int i = int(x); i >= int(y); --i)
#define pi acos(-1)
#define mk make_pair
#define pa pair<ll, ll>
#define lf else if
#define IL inline
#define max(x, y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define Mul(x, y) ((x) = 1LL * (x) * (y) % mod)
#define Add(x, y) ((x) = ((x) + (y)) % mod)
#define Max(x, y) ((x) = ((x) < (y) ? (y) : (x)))
#define Min(x, y) ((x) = ((x) > (y) ? (y) : (x)))
#define E(x) return writeln(x), 0
#define LL (long long)
#define p(x) printf("~%lld~\n", LL(x))
#define pp(x, y) printf("~~%lld %lld~~\n", LL(x), LL(y))
#define ppp(x, y, z) printf("~~~%lld %lld %lld~~~\n", LL(x), LL(y), LL(z))
#define pppp(a, b, c, d)                                                       \
  printf("~~~%lld %lld %lld %lld\n", LL(a), LL(b), LL(c), LL(d))
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
#define GuYue                                                                  \
  puts("\nGuYueNa____________________________________________________________" \
       "____________________")
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
static const int GYN = 2333333;
char SZB[GYN], *SS = SZB, *TT = SZB;
inline char gc() {
  if (SS == TT) {
    TT = (SS = SZB) + fread(SZB, 1, GYN, stdin);
    if (SS == TT)
      return '\n';
  }
  return *SS++;
}
#if NEG
inline ll read() {
  ll x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      g = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) { For(i, s, t) printf("%lld ", a[i]); }
void Print(int *a, int s, int t) { For(i, s, t) printf("%d ", a[i]); }
void Print(char *a, int s, int t) { For(i, s, t) putchar(a[i]); }
void writeln(ll x) {
  write(x);
  puts("");
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const int N = 500105;
ll a[N], n;
char s[N];
bool check(ll x) {
  memset(a, 0, sizeof a);
  For(i, 1, n) a[i] = (s[n - i + 1] - '0') * 9;
  a[1] += x * 9;
  ll sum = 0;
  For(i, 1, n + 100) {
    for (; a[i] > 9; a[i] -= 10, a[i + 1]++)
      ;
    sum += a[i];
  }
  return sum <= x * 9;
}
int main() {
  n = readstr(s + 1);
  ll l = 0, r = n * 9, ans = 0;
  for (; l <= r;) {
    ll mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  writeln(ans);
}