Problem p01555 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// [l, r)
ll countNum(ll l, ll r, ll div) { return (r - 1) / div - (l - 1) / div; }

// x ?????§????????¨????????????????????¨????????????????±???????
ll calc(ll x) {
  ll p = 1;
  ll ans = 0;
  int cnt = 1;
  while (10 * p < x) {
    ll np = p * 10;
    // [p, np) ??§?????????????????????
    ll fizzbuzz = countNum(p, np, 15);
    ll fizz = countNum(p, np, 3) - fizzbuzz;
    ll buzz = countNum(p, np, 5) - fizzbuzz;
    ll other = (np - p) - (fizz + buzz + fizzbuzz);
    ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
    cnt++;
    p = np;
  }
  // [p, x] ????????????????????°
  ll fizzbuzz = countNum(p, x + 1, 15);
  ll fizz = countNum(p, x + 1, 3) - fizzbuzz;
  ll buzz = countNum(p, x + 1, 5) - fizzbuzz;
  ll other = (x + 1 - p) - (fizz + buzz + fizzbuzz);
  ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
  return ans;
}

const string number = "0123456789";

string trans(ll x) {
  if (x % 15 == 0)
    return "FizzBuzz";
  if (x % 5 == 0)
    return "Buzz";
  if (x % 3 == 0)
    return "Fizz";
  string ret;
  while (x) {
    ret += number[(int)(x % 10)];
    x /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  ll s;
  cin >> s;
  ll low = 0, high = 1ll << 55;
  // calc(x) < s ?????????????????§??? x ????±???????
  while (high - low > 1) {
    const ll med = (low + high) / 2;
    if (calc(med) >= s)
      high = med;
    else
      low = med;
  }
  s -= calc(low) + 1;
  // ?????£??????????????´????????£??????
  string ans;
  while (1) {
    string t = trans(++low);
    if (s > 0) {
      ans += t.substr((int)s);
      s = 0;
    } else {
      int i = 0;
      while (ans.size() < 20 && i < (int)t.size())
        ans += t[i++];
    }
    if (ans.size() == 20)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

// [l, r)
ll countNum(ll l, ll r, ll div) { return (r - 1) / div - (l - 1) / div; }

// x ?????§????????¨????????????????????¨????????????????±???????
ll calc(ll x) {
  ll p = 1;
  ll ans = 0;
  int cnt = 1;
  while (10 * p < x) {
    ll np = p * 10;
    // [p, np) ??§?????????????????????
    ll fizzbuzz = countNum(p, np, 15);
    ll fizz = countNum(p, np, 3) - fizzbuzz;
    ll buzz = countNum(p, np, 5) - fizzbuzz;
    ll other = (np - p) - (fizz + buzz + fizzbuzz);
    ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
    cnt++;
    p = np;
  }
  // [p, x] ????????????????????°
  ll fizzbuzz = countNum(p, x + 1, 15);
  ll fizz = countNum(p, x + 1, 3) - fizzbuzz;
  ll buzz = countNum(p, x + 1, 5) - fizzbuzz;
  ll other = (x + 1 - p) - (fizz + buzz + fizzbuzz);
  ans += fizzbuzz * 8 + fizz * 4 + buzz * 4 + other * cnt;
  return ans;
}

const string number = "0123456789";

string trans(ll x) {
  if (x % 15 == 0)
    return "FizzBuzz";
  if (x % 5 == 0)
    return "Buzz";
  if (x % 3 == 0)
    return "Fizz";
  string ret;
  while (x) {
    ret += number[(int)(x % 10)];
    x /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  ll s;
  cin >> s;
  ll low = 0, high = 1ll << 58;
  // calc(x) < s ?????????????????§??? x ????±???????
  while (high - low > 1) {
    const ll med = (low + high) / 2;
    if (calc(med) >= s)
      high = med;
    else
      low = med;
  }
  s -= calc(low) + 1;
  // ?????£??????????????´????????£??????
  string ans;
  while (1) {
    string t = trans(++low);
    if (s > 0) {
      ans += t.substr((int)s);
      s = 0;
    } else {
      int i = 0;
      while (ans.size() < 20 && i < (int)t.size())
        ans += t[i++];
    }
    if (ans.size() == 20)
      break;
  }
  cout << ans << endl;
  return 0;
}