Problem p01555 (C++) — buggy submission, verdict: Runtime Error

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int len[30];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int s;
  cin >> s;

  int sum = 0;
  auto check = [&](int m) -> bool {
    sum = 0;
    int i, l;
    for (i = 1, l = 1; i <= m; i *= 10, ++l) {
      // [i, 10*i)
      int a = (10 * i - 1) / 3 - (i - 1) / 3;
      int b = (10 * i - 1) / 5 - (i - 1) / 5;
      int c = (10 * i - 1) / 15 - (i - 1) / 15;
      a -= c, b -= c;
      sum += (a + b + 2 * c) * 4 + (10 * i - i - a - b - c) * l;
    }
    // (m, i) を引く
    if (m < i) {
      int a = (i - 1) / 3 - m / 3;
      int b = (i - 1) / 5 - m / 5;
      int c = (i - 1) / 15 - m / 15;
      a -= c, b -= c;
      sum -= (a + b + 2 * c) * 4 + (i - 1 - m - a - b - c) * (l - 1);
    }
    return sum < s;
  };

  int lb = 0, ub = 1e18;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (check(mid)) {
      lb = mid;
    } else {
      ub = mid;
    }
  }
  // lb+1 の s - sum番目からのFizzBuzz string
  check(lb);
  // cout << lb+1 << " " << s-sum << endl;

  int idx = lb + 1;
  string ans = "";
  if (idx % 15 == 0) {
    string tmp = "FizzBuzz";
    ans += tmp.substr(s - sum - 1);
  } else if (idx % 3 == 0) {
    string tmp = "Fizz";
    ans += tmp.substr(s - sum - 1);
  } else if (idx % 5 == 0) {
    string tmp = "Buzz";
    ans += tmp.substr(s - sum - 1);
  } else {
    string tmp = to_string(idx);
    ans += tmp.substr(s - sum - 1);
  }
  idx++;
  // cout << ans << endl;
  while (ans.size() < 20) {
    if (idx % 15 == 0) {
      string tmp = "FizzBuzz";
      if (ans.size() + 8 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else if (idx % 3 == 0) {
      string tmp = "Fizz";
      if (ans.size() + 4 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else if (idx % 5 == 0) {
      string tmp = "Buzz";
      if (ans.size() + 4 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else {
      string tmp = to_string(idx);
      if (ans.size() + tmp.size() <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    }
    idx++;
  }
  cout << ans.substr(0, 20) << endl;

  return 0;
}


Error: 0

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL << 60);
const int INF = (1LL << 30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a <= x && x < b; }
template <typename T> T ceil(T a, T b) { return a / b + !!(a % b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << '[';
  REP(i, a.size()) {
    out << a[i];
    if (i != a.size() - 1)
      out << ',';
  }
  out << ']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int len[30];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int s;
  cin >> s;

  int sum = 0;
  auto check = [&](int m) -> bool {
    sum = 0;
    int i, l;
    for (i = 1, l = 1; i <= m; i *= 10, ++l) {
      // [i, 10*i)
      int a = (10 * i - 1) / 3 - (i - 1) / 3;
      int b = (10 * i - 1) / 5 - (i - 1) / 5;
      int c = (10 * i - 1) / 15 - (i - 1) / 15;
      a -= c, b -= c;
      sum += (a + b + 2 * c) * 4 + (10 * i - i - a - b - c) * l;
    }
    // (m, i) を引く
    if (m < i) {
      int a = (i - 1) / 3 - m / 3;
      int b = (i - 1) / 5 - m / 5;
      int c = (i - 1) / 15 - m / 15;
      a -= c, b -= c;
      sum -= (a + b + 2 * c) * 4 + (i - 1 - m - a - b - c) * (l - 1);
    }
    return sum < s;
  };

  int lb = 0, ub = 1e16;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (check(mid)) {
      lb = mid;
    } else {
      ub = mid;
    }
  }
  // lb+1 の s - sum番目からのFizzBuzz string
  check(lb);

  int idx = lb + 1;
  string ans = "";
  if (idx % 15 == 0) {
    string tmp = "FizzBuzz";
    ans += tmp.substr(s - sum - 1);
  } else if (idx % 3 == 0) {
    string tmp = "Fizz";
    ans += tmp.substr(s - sum - 1);
  } else if (idx % 5 == 0) {
    string tmp = "Buzz";
    ans += tmp.substr(s - sum - 1);
  } else {
    string tmp = to_string(idx);
    ans += tmp.substr(s - sum - 1);
  }
  idx++;
  while (ans.size() < 20) {
    if (idx % 15 == 0) {
      string tmp = "FizzBuzz";
      if (ans.size() + 8 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else if (idx % 3 == 0) {
      string tmp = "Fizz";
      if (ans.size() + 4 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else if (idx % 5 == 0) {
      string tmp = "Buzz";
      if (ans.size() + 4 <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    } else {
      string tmp = to_string(idx);
      if (ans.size() + tmp.size() <= 20) {
        ans += tmp;
      } else {
        ans += tmp.substr(0, 20 - ans.size());
      }
    }
    idx++;
  }
  cout << ans.substr(0, 20) << endl;

  return 0;
}