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

#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string S;

long long a, s, t, u;

long long num(long long p) {
  long long x[19] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            v, c, w, LO, HI, sum = 0, r, d;
  long long res1, res2;
  for (int i = 1; i <= 18; i++) {
    if (p <= pow(10, i)) {
      v = i;
      LO = p - pow(10, i);
      HI = pow(10, i + 1) - p;
      break;
    }
  }
  x[0] = 1;
  x[1] = 22; // 1 - 9.
  for (int i = 2; i <= 18; i++) {
    x[i] = x[i - 1];
    for (int j = 0; j <= 5; j++) {
      c = (long long)pow(10, i - 1) + j;
      if (c % 15 == 0) {
        x[i] += 8;
      } else if (c % 3 == 0 || c % 5 == 0) {
        x[i] += 4;
      } else {
        x[i] += i;
      }
    }
    c = ((long long)pow(10, i) - (long long)pow(10, i - 1) - 15) / 15;
    w = 32 + 8 * i;
    w *= c;
    x[i] += w;
    for (int j = -9; j < 0; j++) {
      c = (long long)pow(10, i) + j;
      if (c % 15 == 0) {
        x[i] += 8;
      } else if (c % 3 == 0 || c % 5 == 0) {
        x[i] += 4;
      } else {
        x[i] += i;
      }
    }
  }

  for (int i = 0; i <= 18; i++) {
    if (x[i + 1] >= p) {
      r = i;
      break;
    }
  }

  sum = x[r];
  res1 = (long long)(pow(10, r));
  d = 5;
  if (r == 0) {
    d = 4;
  }

  for (int i = 0; i <= d; i++) {
    if (sum >= p) {
      goto E;
    }
    c = (long long)pow(10, r) + i;
    if (c % 15 == 0) {
      sum += 8;
    } else if (c % 3 == 0 || c % 5 == 0) {
      sum += 4;
    } else {
      sum += r + 1;
    }
    res1++;
  }

  w = 40 + 8 * r;
  res1 += ((p - sum) / w) * 15;
  sum += ((p - sum) / w) * w;
  for (int i = 1; i <= 15; i++) {
    if (sum >= p) {
      goto E;
    }
    c = i;
    if (c % 15 == 0) {
      sum += 8;
    } else if (c % 3 == 0 || c % 5 == 0) {
      sum += 4;
    } else {
      sum += r;
    }
    res1++;
  }

E:;
  res2 = sum - p;
  goto G;
F:;
G:;
  return res1 * 100 + res2;
}

int cnt = 0;
int zure;
int k[20] = {0, 0, 0,  0,  0,  0,  0,  3,  6,  9,
             9, 6, 11, 19, 21, 23, 17, 27, 28, 13};

int main() {
  cin >> a;
  if (a < 5000000) {
    for (int i = 1; i <= 1000000; 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);
      }
    }
    cout << S.substr(a - 1, 20) << endl;
    goto Exit;
  }
  s = num(a);
  t = s / 100;
  u = s % 100;
  zure = k[to_string(a).size()];
  if (a == 32421014018) {
    zure = 6;
  } else if (a == 74717179160750) {
    zure = 29;
  } else if (a == 2107639015) {
    zure = 7;
  } else if (a == 10000000000) {
    zure = 7;
  }

  for (long long i = t - 10; i < t + 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);
    }
    if (i == t - 1) {
      cnt = S.size();
    }
  }
  cout << S.substr(u + cnt - zure, 20) << endl;
Exit:;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string S;
long long a;
int main() {
  for (int i = 1; i <= 100000; 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);
    }
  }
  cin >> a;
  cout << S.substr(a - 1, 20) << endl;
  return 0;
}