Problem p01555 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
        << endl)
#else
#define dump(...)
#endif

#define int ll

int range(int l, int r, int len) {
  int fizz = r / 3 - l / 3;
  int buzz = r / 5 - l / 5;
  int fizzbuzz = r / 15 - l / 15;
  int res = 0;
  res += (r - l - fizz - buzz + fizzbuzz) * len;
  res += (fizz + buzz - 2 * fizzbuzz) * 4;
  res += fizzbuzz * 8;
  return res;
}

int len(int n) {
  int res = 0;
  int cnt = 1;
  int p = 1;
  while (p * 10 <= n) {
    res += range(p - 1, p * 10 - 1, cnt);
    p *= 10;
    cnt += 1;
  }
  res += range(p - 1, n - 1, cnt);
  return res;
}

string solve(int n) {
  int l = 0, r = 1LL << 55;
  while (l + 1 < r) {
    int m = (r + l) / 2;
    if (len(m) < n)
      l = m;
    else
      r = m;
  }
  int rest = n - len(l);
  string s;
  for (int i = l; (int)s.size() < rest + 30; i++) {
    if (i % 15 == 0)
      s += "FizzBuzz";
    else if (i % 3 == 0)
      s += "Fizz";
    else if (i % 5 == 0)
      s += "Buzz";
    else
      s += to_string(i);
  }
  return s.substr(rest - 1, 20);
}

signed main() {
  int n;
  cin >> n;
  cout << solve(n) << endl;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \
        << endl)
#else
#define dump(...)
#endif

#define int ll

int range(int l, int r, int len) {
  int fizz = r / 3 - l / 3;
  int buzz = r / 5 - l / 5;
  int fizzbuzz = r / 15 - l / 15;
  int res = 0;
  res += (r - l - fizz - buzz + fizzbuzz) * len;
  res += (fizz + buzz - 2 * fizzbuzz) * 4;
  res += fizzbuzz * 8;
  return res;
}

int len(int n) {
  int res = 0;
  int cnt = 1;
  int p = 1;
  while (p * 10 <= n) {
    res += range(p - 1, p * 10 - 1, cnt);
    p *= 10;
    cnt += 1;
  }
  res += range(p - 1, n - 1, cnt);
  return res;
}

string solve(int n) {
  int l = 0, r = 1000000000000000000LL;
  while (l + 1 < r) {
    int m = (r + l) / 2;
    if (len(m) < n)
      l = m;
    else
      r = m;
  }
  int rest = n - len(l);
  string s;
  for (int i = l; (int)s.size() < rest + 30; i++) {
    if (i % 15 == 0)
      s += "FizzBuzz";
    else if (i % 3 == 0)
      s += "Fizz";
    else if (i % 5 == 0)
      s += "Buzz";
    else
      s += to_string(i);
  }
  return s.substr(rest - 1, 20);
}

signed main() {
  int n;
  cin >> n;
  cout << solve(n) << endl;
}