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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, val) for (int i = 0; i < val; i++)

const long long INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

bool has_key(unordered_map<string, ll> &m, string s) {
  if (m.find(s) == m.end()) {
    return false;
  } else {
    return true;
  }
}

bool vector_find(std::vector<ll> vec, ll number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) {
    return true;
  } else {
    return false;
  }
}

void recursive_comb(int *indexes, int s, int rest,
                    std::function<void(int *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

void foreach_comb(int n, int k, std::function<void(int *)> f) {
  int indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

int main() {

  ll N;

  cin >> N;

  ll tmp = 0;
  vector<ll> vec_sum(12, 0);
  for (ll i = 1; i <= 11; i++) {
    tmp += pow(26, i);
    vec_sum.at(i) = tmp;
  }

  string ans = "";

  vector<string> alphabet{"a", "b", "c", "d", "e", "f", "g", "h", "i",
                          "j", "k", "l", "m", "n", "o", "p", "q", "r",
                          "s", "t", "u", "v", "w", "x", "y", "z"};

  vector<ll> index;
  ll div_tmp;

  for (ll i = 1; i <= 11; i++) {

    if (N <= vec_sum.at(i)) {
      N -= vec_sum.at(i - 1);

      for (ll j = i - 1; j >= 0; j--) {
        if (N == 0) {
          ans += "z";
        } else if (j == 0) {
          ans += alphabet.at(N - 1);
        } else if (pow(26, j) < N) {
          div_tmp = (N - 1) / ll(pow(26, j));
          N -= pow(26, j) * div_tmp;
          if (div_tmp == 26) {
            ans += "z";
          } else {
            ans += alphabet.at(div_tmp);
          }
        } else {
          ans += "a";
        }
      }

      break;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, val) for (int i = 0; i < val; i++)

const long long INF = 1LL << 60;
const ll MOD = ll(1e9) + 7;

bool has_key(unordered_map<string, ll> &m, string s) {
  if (m.find(s) == m.end()) {
    return false;
  } else {
    return true;
  }
}

bool vector_find(std::vector<ll> vec, ll number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) {
    return true;
  } else {
    return false;
  }
}

void recursive_comb(int *indexes, int s, int rest,
                    std::function<void(int *)> f) {
  if (rest == 0) {
    f(indexes);
  } else {
    if (s < 0)
      return;
    recursive_comb(indexes, s - 1, rest, f);
    indexes[rest - 1] = s;
    recursive_comb(indexes, s - 1, rest - 1, f);
  }
}

void foreach_comb(int n, int k, std::function<void(int *)> f) {
  int indexes[k];
  recursive_comb(indexes, n - 1, k, f);
}

int main() {

  ll N;

  cin >> N;

  ll tmp = 0;
  vector<ll> vec_sum(12, 0);
  for (ll i = 1; i <= 11; i++) {
    tmp += pow(26, i);
    vec_sum.at(i) = tmp;
  }

  string ans = "";

  vector<string> alphabet{"a", "b", "c", "d", "e", "f", "g", "h", "i",
                          "j", "k", "l", "m", "n", "o", "p", "q", "r",
                          "s", "t", "u", "v", "w", "x", "y", "z"};

  vector<ll> index;
  ll div_tmp;

  for (ll i = 1; i <= 11; i++) {

    if (N <= vec_sum.at(i)) {
      N -= vec_sum.at(i - 1);

      for (ll j = i - 1; j >= 0; j--) {

        if (N == 0) {
          ans += "z";
        } else if (j == 0) {
          ans += alphabet.at(N - 1);
        } else if (pow(26, j) < N) {
          cout << "div_tmp:" << div_tmp << " " << ll(pow(26, j)) << endl;
          N -= pow(26, j) * div_tmp;
          if (div_tmp == 26) {
            ans += "z";
          } else {
            ans += alphabet.at(div_tmp);
          }
        } else {
          ans += "a";
        }
      }

      break;
    }
  }

  cout << ans << endl;

  return 0;
}