Problem p01555 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll sum[20];

ll len(ll x) {
  if (x == 0)
    return 0;
  ll ret = 0;
  int keta = 0;
  ll t = x;
  ll ex = 1;
  while (t) {
    keta++;
    t /= 10;
    ex *= 10;
  }
  ex /= 10;
  ret += sum[keta - 1];
  // cout<<"keta "<<keta<<endl;
  ll fiz = x / 3 - (ex - 1) / 3;
  ll buz = x / 5 - (ex - 1) / 5;
  ll fizbuz = x / 15 - (ex - 1) / 15;
  ll mod = x - ex + 1 - fiz - buz + fizbuz;
  // cout<<"len "<<x<<" "<<fiz<<" "<<buz<<" "<<mod<<endl;
  ret += fiz * 4 + buz * 4 + mod * keta;
  return ret;
}

char idx(ll l, int x) {
  if (l % 15 == 0) {
    return "FizzBuzz"[x];
  }
  if (l % 3 == 0) {
    return "Fizz"[x];
  }
  if (l % 5 == 0) {
    return "Buzz"[x];
  }
  stringstream ss;
  ss << l;
  string t;
  ss >> t;
  return t[x];
}

char func(ll x) {
  ll l = 0;
  ll r = 1e12;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    // cout<<l<<" "<<r<<" "<<mid<<" "<<len(mid)<<endl;
    if (len(mid) < x) {
      l = mid;
    } else {
      r = mid;
    }
  }
  x -= len(l);
  // cout<<"func "<<r<<" "<<x-1<<endl;
  return idx(r, x - 1);
}

int main() {
  ll tmp = 10;
  sum[1] = 21;
  for (int i = 2; i < 20; i++) {
    tmp *= 10;
    sum[i] += sum[i - 1];
    sum[i] += tmp / 10 * 9 * i;
    sum[i] += ((tmp - 1) / 3 - (tmp / 10 - 1) / 3) * (4 - i);
    sum[i] += ((tmp - 1) / 5 - (tmp / 10 - 1) / 5) * (4 - i);
    sum[i] += ((tmp - 1) / 15 - (tmp / 10 - 1) / 15) * (i);
    // cout<<sum[i]<<endl;
  }
  ll s;
  cin >> s;
  for (int i = 0; i < 20; i++) {
    // cout<<"i "<<i<<endl;
    cout << func(s + i);
    // cout<<s+i<<" "<<func(s+i)<<endl;
  }
  // for(int i=1; i<10000; i++){
  // 	ll l = len(i) - len(i-1);
  // 	ll a;
  // 	if(i%15==0) a = 8;
  // 	else if(i%3==0) a = 4;
  // 	else if(i%5==0) a = 4;
  // 	else{
  // 		stringstream ss;
  // 		ss<<i;
  // 		string t;
  // 		ss>>t;
  // 		a = t.size();
  // 	}
  // 	if(a != l){
  // 		cout<<i<<" "<<l<<" "<<a<<endl;
  // 		cout<<len(i-1)<<" "<<len(i)<<endl;
  // 		return 0;
  // 	}
  // }
  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll sum[20];

ll len(ll x) {
  if (x == 0)
    return 0;
  ll ret = 0;
  int keta = 0;
  ll t = x;
  ll ex = 1;
  while (t) {
    keta++;
    t /= 10;
    ex *= 10;
  }
  ex /= 10;
  ret += sum[keta - 1];
  // cout<<"keta "<<keta<<endl;
  ll fiz = x / 3 - (ex - 1) / 3;
  ll buz = x / 5 - (ex - 1) / 5;
  ll fizbuz = x / 15 - (ex - 1) / 15;
  ll mod = x - ex + 1 - fiz - buz + fizbuz;
  // cout<<"len "<<x<<" "<<fiz<<" "<<buz<<" "<<mod<<endl;
  ret += fiz * 4 + buz * 4 + mod * keta;
  return ret;
}

char idx(ll l, int x) {
  if (l % 15 == 0) {
    return "FizzBuzz"[x];
  }
  if (l % 3 == 0) {
    return "Fizz"[x];
  }
  if (l % 5 == 0) {
    return "Buzz"[x];
  }
  stringstream ss;
  ss << l;
  string t;
  ss >> t;
  return t[x];
}

char func(ll x) {
  ll l = 0;
  ll r = 1e17;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    // cout<<l<<" "<<r<<" "<<mid<<" "<<len(mid)<<endl;
    if (len(mid) < x) {
      l = mid;
    } else {
      r = mid;
    }
  }
  x -= len(l);
  // cout<<"func "<<r<<" "<<x-1<<endl;
  return idx(r, x - 1);
}

int main() {
  ll tmp = 10;
  sum[1] = 21;
  for (int i = 2; i < 20; i++) {
    tmp *= 10;
    sum[i] += sum[i - 1];
    sum[i] += tmp / 10 * 9 * i;
    sum[i] += ((tmp - 1) / 3 - (tmp / 10 - 1) / 3) * (4 - i);
    sum[i] += ((tmp - 1) / 5 - (tmp / 10 - 1) / 5) * (4 - i);
    sum[i] += ((tmp - 1) / 15 - (tmp / 10 - 1) / 15) * (i);
    // cout<<sum[i]<<endl;
  }
  ll s;
  cin >> s;
  for (int i = 0; i < 20; i++) {
    // cout<<"i "<<i<<endl;
    cout << func(s + i);
    // cout<<s+i<<" "<<func(s+i)<<endl;
  }
  // for(int i=1; i<10000; i++){
  // 	ll l = len(i) - len(i-1);
  // 	ll a;
  // 	if(i%15==0) a = 8;
  // 	else if(i%3==0) a = 4;
  // 	else if(i%5==0) a = 4;
  // 	else{
  // 		stringstream ss;
  // 		ss<<i;
  // 		string t;
  // 		ss>>t;
  // 		a = t.size();
  // 	}
  // 	if(a != l){
  // 		cout<<i<<" "<<l<<" "<<a<<endl;
  // 		cout<<len(i-1)<<" "<<len(i)<<endl;
  // 		return 0;
  // 	}
  // }
  cout << endl;
}