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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(i, s) cout << " " << i;                                                 \\
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int main() {
  ll n;
  ll buff;
  cin >> n;
  ll cnt = 0;
  while (n > 0) {
    buff = n;
    n -= (ll)((cnt + 6) * 8 * 6 * pow(10, cnt)) - 5;
    cnt++;
  }
  if (buff <= cnt * 8 + 35) {
    ll fr = pow(10, cnt) - 9;
    string ss;
    ll cnt3 = 0;
    ll id = 0;
    while (cnt3 < buff + 20) {
      if ((fr + id) % 3 == 0) {
        string hoge = "Fizz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 == 0) {
        string hoge = "Buzz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 != 0 && (fr + id) % 3 != 0) {
        string hoge;
        ll hoge2 = fr + id;
        while (hoge2 != 0) {
          hoge += (char)('0' + (hoge2 % 10));
          cnt3++;
          hoge2 /= 10;
        }
        reverse(hoge.begin(), hoge.end());
        ss += hoge;
      }
      id++;
    }
    cout << ss.substr(buff - 1, 20) << "\\n";
  } else {
    buff -= cnt * 8 + 35;
    ll buff2 = buff % (8 * (cnt + 5));
    ll cnt2 = buff / (8 * (cnt + 5));
    ll fr = (ll)(pow(10, cnt) + 6) + (ll)(cnt2 * 15LL);
    string ss;
    ll cnt3 = 0;
    ll id = 0;
    while (cnt3 < buff2 + 20) {
      if ((fr + id) % 3 == 0) {
        string hoge = "Fizz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 == 0) {
        string hoge = "Buzz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 != 0 && (fr + id) % 3 != 0) {
        string hoge;
        ll hoge2 = fr + id;
        while (hoge2 != 0) {
          hoge += (char)('0' + (hoge2 % 10));
          cnt3++;
          hoge2 /= 10;
        }
        reverse(hoge.begin(), hoge.end());
        ss += hoge;
      }
      id++;
    }
    cout << ss.substr(buff2 - 1, 20) << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(i, s) cout << " " << i;                                                 \\
  cout << endl

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int main() {
  ll n;
  ll buff;
  cin >> n;
  ll cnt = 0;
  while (n > 0) {
    buff = n;
    n -= (cnt + 6) * 8 * 6 * pow(10, cnt) - 5;
    cnt++;
  }
  if (buff <= cnt * 8 + 35) {
    ll fr = pow(10, cnt) + 6 - 15;
    string ss;
    ll cnt3 = 0;
    ll id = 0;
    while (cnt3 < buff + 20) {
      if ((fr + id) % 3 == 0) {
        string hoge = "Fizz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 == 0) {
        string hoge = "Buzz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 != 0 && (fr + id) % 3 != 0) {
        string hoge;
        ll hoge2 = fr + id;
        while (hoge2 != 0) {
          hoge += (char)('0' + (hoge2 % 10));
          cnt3++;
          hoge2 /= 10;
        }
        reverse(hoge.begin(), hoge.end());
        ss += hoge;
      }
      id++;
    }
    cout << ss.substr(buff - 1, 20) << "\\n";
  } else {
    buff -= cnt * 8 + 35;
    ll cnt2 = 0;
    ll buff2 = 0;
    while (buff > 0) {
      buff2 = buff;
      buff -= 8 * (cnt + 5);
      cnt2++;
    }
    ll fr = pow(10, cnt) + 6 + (cnt2 - 1) * 15;
    string ss;
    ll cnt3 = 0;
    ll id = 0;
    while (cnt3 < buff2 + 20) {
      if ((fr + id) % 3 == 0) {
        string hoge = "Fizz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 == 0) {
        string hoge = "Buzz";
        ss += hoge;
        cnt3 += 4;
      }
      if ((fr + id) % 5 != 0 && (fr + id) % 3 != 0) {
        string hoge;
        ll hoge2 = fr + id;
        while (hoge2 != 0) {
          hoge += (char)('0' + (hoge2 % 10));
          cnt3++;
          hoge2 /= 10;
        }
        reverse(hoge.begin(), hoge.end());
        ss += hoge;
      }
      id++;
    }
    cout << ss.substr(buff - 1, 20) << "\\n";
  }
  return 0;
}