Problem p01555 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
long long count_div(long long a, long long b, long long x) {
  return b / x - a / x;
}
long long calc_len(long long a) {
  long long ans = 0;
  long long i, l;
  for (i = 1, l = 10;; i++, l *= 10) {
    if (a >= l) {
      ans += i * (l - l / 10);
      long long x3 = count_div(l / 10 - 1, l - 1, 3);
      long long x5 = count_div(l / 10 - 1, l - 1, 5);
      long long x15 = count_div(l / 10 - 1, l - 1, 15);
      ans -= (x3 + x5 - x15) * i;
      ans += (x3 + x5) * 4;
    } else {
      ans += i * (a - l / 10 + 1);
      long long x3 = count_div(l / 10 - 1, a, 3);
      long long x5 = count_div(l / 10 - 1, a, 5);
      long long x15 = count_div(l / 10 - 1, a, 15);
      ans -= (x3 + x5 - x15) * i;
      ans += (x3 + x5) * 4;
      break;
    }
  }
  if (a % 15 == 0) {
    ans -= 8;
  } else if (a % 3 == 0 || a % 5 == 0) {
    ans -= 4;
  } else {
    ans -= i;
  }
  return ans;
}
string fizzbuzz(long long x) {
  stringstream ss;
  for (long long i = x; i < x + 20; i++) {
    if (i == 0)
      ss << 0;
    else if (i % 15 == 0) {
      ss << "FizzBuzz";
    } else if (i % 5 == 0) {
      ss << "Buzz";
    } else if (i % 3 == 0) {
      ss << "Fizz";
    } else {
      ss << i;
    }
  }
  return ss.str();
}

string solve(long long s) {
  long long l, r, m;
  l = 0;
  r = 1000000000000000000;

  while (l < r) {
    m = (l + r) / 2;
    long long len = calc_len(m);
    if (len < s)
      l = m + 1;
    else
      r = m;
  }
  l = max(l - 1, (long long)1);
  long long dif = s - calc_len(l);
  /*puts("");
  cout<<s<<endl;
  debug(dif);
  debug(l);*/
  return fizzbuzz(l).substr(dif - 1, 20);
}

int main() {
  long long s;
  cin >> s;
  cout << solve(s) << endl;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M_PI 3.141592
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define toRad 2.0 * M_PI / 360.0
#define inin(x)                                                                \\
  int x;                                                                       \\
  cin >> x;
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " :" << x << endl;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define EPS 1e-12
#define CHECK(i, a)                                                            \\
  FOR(i, a.size()) cout << #a << "[" << i << "] : " << a[i] << endl;
long long count_div(long long a, long long b, long long x) {
  return b / x - a / x;
}
long long calc_len(long long a) {
  long long ans = 0, b = a;
  // if (a > 0)ans--;
  for (long long i = 1, l = 10;; i++, l *= 10) {
    if (b >= l) {
      ans += i * (l - l / 10);
      long long x3 = count_div(l / 10 - 1, l - 1, 3);
      long long x5 = count_div(l / 10 - 1, l - 1, 5);
      long long x15 = count_div(l / 10 - 1, l - 1, 15);
      ans -= (x3 + x5 - x15) * i;
      ans += (x3 + x5) * 4;
    } else {
      ans += i * (b - l / 10 + 1);
      long long x3 = count_div(l / 10 - 1, b, 3);
      long long x5 = count_div(l / 10 - 1, b, 5);
      long long x15 = count_div(l / 10 - 1, b, 15);
      ans -= (x3 + x5 - x15) * i;
      ans += (x3 + x5) * 4;
      break;
    }
  }
  return ans;
}
string fizzbuzz(long long x) {
  stringstream ss;
  for (long long i = x; i < x + 20; i++) {
    if (i == 0)
      ss << 0;
    else if (i % 15 == 0) {
      ss << "FizzBuzz";
    } else if (i % 5 == 0) {
      ss << "Buzz";
    } else if (i % 3 == 0) {
      ss << "Fizz";
    } else {
      ss << i;
    }
  }
  return ss.str();
}
string solve(long long s) {
  long long l, r, m;
  l = 0;
  r = 10000000000;

  while (l < r) {
    m = (l + r) / 2;
    long long len = calc_len(m);
    if (len < s)
      l = m;
    else
      r = m - 1;
  }
  debug(r);
  debug(calc_len(r));
  long long dif = s - calc_len(r);
  debug(dif);
  return fizzbuzz(r).substr(dif, 20);
}

int main() {
  long long s;
  cin >> s;
  cout << solve(s) << endl;
}