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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  ll N;
  cin >> N;

  ll cnt = 1;
  ll acc = 26;
  if (acc > N) {
    cout << (char)('a' + N - 1) << endl;
    return 0;
  }
  while (true) {
    cnt++;
    ll a = pow(26, cnt);
    if (acc + a >= N)
      break;
    acc += a;
  }

  vector<char> name(cnt);

  ll now = acc + 1;
  REP(i, cnt) {
    int t = 0;
    REP(_, 26) {
      ll a = pow(26, (cnt - 1) - i);
      if (now + a - 1 >= N) {
        break;
      }
      t++;
      now += a;
    }
    name[i] = (char)('a' + t);
  }

  for (char c : name) {
    cout << c;
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
// #define int long long

template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

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

  ll N;
  cin >> N;

  ll cnt = 1;
  ll acc = 26;
  if (acc > N) {
    cout << (char)('a' + N - 1) << endl;
    return 0;
  }
  while (true) {
    cnt++;
    int a = pow(26, cnt);
    if (acc + a >= N)
      break;
    acc += a;
  }

  vector<char> name(cnt);

  ll now = acc + 1;
  REP(i, cnt) {
    int t = 0;
    REP(_, 26) {
      int a = pow(26, (cnt - 1) - i);
      if (now + a - 1 >= N) {
        break;
      }
      t++;
      now += a;
    }
    name[i] = (char)('a' + t);
  }

  for (char c : name) {
    cout << c;
  }
  cout << endl;
  return 0;
}