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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int lli;

lli pow10(lli n) {
  lli res = 1LL;
  if (n < 0)
    return 0;
  for (lli i = 0; i < n; i++) {
    res *= 10LL;
  }
  return res;
}

lli len(lli n) {
  lli res = 0;
  while (n > 0) {
    res += (lli)(log10l(n) + 1LL) * (n - pow10((lli)log10l(n)) + 1LL);
    lli tmp = pow10((lli)log10l(n)) - 1LL;
    // if(tmp == n)tmp /= 10;
    //  std::cout << log10l(n) << std::endl;
    //  std::cout << "tmp:" << tmp << std::endl;
    res += (4LL - (lli)(log10l(n) + 1LL)) *
           (n / 3LL + n / 5LL - (tmp / 3LL + tmp / 5LL));
    res += (lli)(log10l(n) + 1LL) * (n / 15LL - tmp / 15LL);
    n = tmp;
  }
  // std::cout << "res:" << res << std::endl;
  return res;
}

lli pos(lli n) {
  lli maxi = 2 * 1e18, mini = 0, med = (maxi + mini) / 2LL;
  lli l;
  while (maxi - mini > 1) {
    // l = len(med);
    l = len(n);
    // std::cout << "l:" << l << std::endl;
    // if(l > n){
    if (l < med) {
      maxi = med;
    } else {
      mini = med;
    }
    med = (maxi + mini) / 2;
  }
  // if(maxi > n)return mini;
  if (maxi > len(n))
    return mini;
  else
    return maxi;
}

int main() {
  lli n;
  std::cin >> n;
  lli maxi = 1.8 * 1e18, mini = 0, med = (maxi + mini) / 2, p;
  while (maxi - mini > 1) {
    if (pos(med) >= n) {
      maxi = med;
    } else {
      mini = med;
    }
    med = (maxi + mini) / 2;
  }
  if (pos(maxi) >= n)
    p = mini;
  else
    p = maxi;
  // std::cout << "pos(3):" << pos(3)  << std::endl;
  //  std::cout << "maxi:" << maxi << std::endl;
  //  std::cout << "mini:" << mini << std::endl;

  lli diff = n - pos(p) - 1LL;
  // std::cout << n << std::endl;
  // std::cout << "pos(" << p << "):" << pos(p) << std::endl;
  // std::cout << "diff:" << diff << std::endl;
  // std::cout << (lli)1e18 << std::endl;
  // std::cout << diff + pos(p) + 1 << std::endl;
  string s;
  for (lli i = p + 1; s.length() < 20 + diff + 1; 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);
  }
  // std::cout << s << std::endl;
  // std::cout << "   diff:" << diff << std::endl;
  // std::cout << "      p:" << p << std::endl;
  // std::cout << "pos(" << p << "):" << pos(p) << std::endl;
  std::cout << s.substr(diff, 20) << std::endl;
  // slow(n);
  // std::cout << len(310) << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;

lli pow10(lli n) {
  lli res = 1;
  if (n < 0)
    return 0;
  for (int i = 0; i < n; i++) {
    res *= 10;
  }
  return res;
}

lli len(lli n) {
  lli res = 0;
  while (n > 0) {
    res += (lli)(log10(n) + 1) * (n - pow10((lli)log10(n)) + 1);
    lli tmp = pow10((lli)log10(n)) - 1;
    res += (4 - (lli)(log10(n) + 1)) * (n / 3 + n / 5 - (tmp / 3 + tmp / 5));
    res += (lli)(log10(n) + 1) * (n / 15 - tmp / 15);
    n = pow10((lli)log10(n)) - 1;
  }
  return res;
}

lli pos(lli n) {
  lli maxi = 1e14, mini = 0, med = (maxi + mini) / 2;
  lli l;
  while (maxi - mini > 1) {
    l = len(n);
    if (l < med) {
      maxi = med;
    } else {
      mini = med;
    }
    med = (maxi + mini) / 2;
  }
  if (maxi > len(n))
    return mini;
  else
    return maxi;
}

// void slow(int n){
//   string s;
//   for (int i = 1; i <= n + 20; 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);
//   }
//   std::cout << "ans:";
//   std::cout << s.substr(n - 1, 20) << std::endl;
// }

int main() {
  lli n;
  std::cin >> n;
  lli maxi = 1e14, mini = 0, med = (maxi + mini) / 2, p;
  while (maxi - mini > 1) {
    // std::cout << pos(med) << std::endl;
    if (pos(med) >= n) {
      maxi = med;
    } else {
      mini = med;
    }
    med = (maxi + mini) / 2;
  }
  if (pos(maxi) >= n)
    p = mini;
  else
    p = maxi;
  // std::cout << "pos(3):" << pos(3)  << std::endl;
  // std::cout << maxi << std::endl;

  int diff = n - pos(p) - 1;
  string s;
  for (lli i = p + 1; s.length() < 20 + diff + 1; 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);
  }
  // std::cout << "   diff:" << diff << std::endl;
  // std::cout << "      p:" << p << std::endl;
  // std::cout << "pos(" << p << "):" << pos(p) << std::endl;
  std::cout << s.substr(diff, 20) << std::endl;
  // slow(n);
  // std::cout << len(310) << std::endl;
  return 0;
}