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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(_v)                                                             \\
  for (auto _x : _v) {                                                         \\
    cout << _x << " ";                                                         \\
  }                                                                            \\
  cout << endl
#define printVS(_vs)                                                           \\
  for (auto _x : _vs) {                                                        \\
    cout << _x << endl;                                                        \\
  }
#define printVV(_vv)                                                           \\
  for (auto _v : _vv) {                                                        \\
    for (auto _x : _v) {                                                       \\
      cout << _x << " ";                                                       \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(_p) cout << _p.first << " " << _p.second << endl
#define printVP(_vp)                                                           \\
  for (auto _p : _vp)                                                          \\
    printP(_p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

// [1, x]???????????????3??§???5??§???????????????????????´??°????????°
int f(int x) {
  int n3 = x / 3;
  int n5 = x / 5;
  int n15 = x / 15;
  return x - n3 - n5 + n15;
}

// ??°x?????§???????????¨????????????????????????
int calc(int x) {
  int ret = 0;
  ret += 4 * (x / 3 + x / 5);

  int a = 0;
  int b = 9;
  rep(i, 100) {
    bool finish = false;
    if (x <= b) {
      b = x;
      finish = true;
    }
    // [a, b]???????????????3??§???5??§???????????????????????´??°????????°
    int m = f(b) - f(a);
    ret += (i + 1) * m;
    if (finish) {
      return ret;
    }
    a = b + 1;
    b = 10 * a - 1;
  }
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  cin >> n;

  int l = 0, r = 1e18;
  while (r - l > 1) {
    int m = (l + r) / 2;
    (calc(m) < n ? l : r) = m;
  }

  string s;
  rep2(x, l + 1, l + 100) {
    if (x % 3 == 0)
      s += "Fizz";
    if (x % 5 == 0)
      s += "Buzz";
    if (x % 3 != 0 && x % 5 != 0)
      s += to_string(x);
  }
  cout << s.substr(n - calc(l) - 1, 20) << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(_v)                                                             \\
  for (auto _x : _v) {                                                         \\
    cout << _x << " ";                                                         \\
  }                                                                            \\
  cout << endl
#define printVS(_vs)                                                           \\
  for (auto _x : _vs) {                                                        \\
    cout << _x << endl;                                                        \\
  }
#define printVV(_vv)                                                           \\
  for (auto _v : _vv) {                                                        \\
    for (auto _x : _v) {                                                       \\
      cout << _x << " ";                                                       \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(_p) cout << _p.first << " " << _p.second << endl
#define printVP(_vp)                                                           \\
  for (auto _p : _vp)                                                          \\
    printP(_p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

// [1, x]???????????????3??§???5??§???????????????????????´??°????????°
int f(int x) {
  int n3 = x / 3;
  int n5 = x / 5;
  int n15 = x / 15;
  return x - n3 - n5 + n15;
}

// ??°x?????§???????????¨????????????????????????
int calc(int x) {
  int ret = 0;
  ret += 4 * (x / 3 + x / 5);

  int a = 0;
  int b = 9;
  rep(i, 100) {
    bool finish = false;
    if (x <= b) {
      b = x;
      finish = true;
    }
    // [a, b]???????????????3??§???5??§???????????????????????´??°????????°
    int m = f(b) - f(a);
    ret += (i + 1) * m;
    if (finish) {
      return ret;
    }
    a = b + 1;
    b = 10 * a - 1;
  }
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  cin >> n;

  int l = 0, r = 1e15;
  while (r - l > 1) {
    int m = (l + r) / 2;
    (calc(m) < n ? l : r) = m;
  }
  cout << l << " " << calc(l) << endl;
  string s;
  rep2(x, l + 1, l + 100) {
    if (x % 3 == 0)
      s += "Fizz";
    if (x % 5 == 0)
      s += "Buzz";
    if (x % 3 != 0 && x % 5 != 0)
      s += to_string(x);
  }
  cout << s.substr(n - calc(l) - 1, 20) << endl;
}