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

//
#include <bits/stdc++.h>
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define e_b emplace_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) // vectorの中身を見る
{
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
void map_p(map<int, int> &d) {
  cout << "map: ";
  for (auto a : d) {
    cout << "{" << a.first << ":" << a.second << "}";
  }
  cout << endl;
} // mapの中身を見る
void set_p(set<int> &d) {
  cout << "set: ";
  for (int a : d) {
    cout << a << " ";
  }
  cout << endl;
} // setの中身を見る

// void dfs(string cur,int nan){
//     if(nan==sou){
//         bef++;
//         if(bef==n){
//             ans=cur;
//             return;
//         }
//         return;
//     }
//     if(n<bef) return;
//     for(int i=1;i<=26;i++){
//         dfs(cur+char(96+i),nan+1);
//     }
//     return;
// }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  ll sou = 1;
  ll bef = 0;
  ll n;
  string ans = "";
  cin >> n;
  ll now = 26;
  while (now < n) {
    sou++;
    bef = now;
    now = now + ll(pow(26, sou));
  }
  // cout<<sou<<endl;
  vector<int> num;
  bef++;
  // cout<<bef<<endl;
  for (int i = sou - 1; i >= 1; i = i - 1) {
    int sou2 = 1;
    ll end = bef + ll(pow(26, i)) - 1; // ブロックの最後
    while (end < n) {
      bef = end + 1;
      end = bef + ll(pow(26, i)) - 1;
      sou2++;
      // cout<<end<<endl;
    }
    num.emplace_back(sou2);
    // if(sou2!=1) bef-=ll(pow(26,i));
  }
  // cout<<num<<endl;
  // cout<<ans<<endl;
  ll ind = bef - 1;
  // cout<<ind<<endl;
  rep(i, sou - 1) { ans += char(num[i] + 96); }
  for (int i = 1; i <= 26; i++) {
    ind++;
    if (ind == n) {
      ans += char(96 + i);
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

//
#include <bits/stdc++.h>
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define e_b emplace_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ipair;
typedef pair<ll, ll> lpair;
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) // vectorの中身を見る
{
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
void map_p(map<int, int> &d) {
  cout << "map: ";
  for (auto a : d) {
    cout << "{" << a.first << ":" << a.second << "}";
  }
  cout << endl;
} // mapの中身を見る
void set_p(set<int> &d) {
  cout << "set: ";
  for (int a : d) {
    cout << a << " ";
  }
  cout << endl;
} // setの中身を見る

ll sou = 1;
ll bef = 0;
ll n;
string ans = "";
void dfs(string cur, int nan) {
  if (nan == sou) {
    bef++;
    if (bef == n) {
      ans = cur;
      return;
    }
    return;
  }
  if (n < bef)
    return;
  for (int i = 1; i <= 26; i++) {
    dfs(cur + char(96 + i), nan + 1);
  }
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  cin >> n;
  ll now = 26;
  while (now < n) {
    sou++;
    bef = now;
    now = now + ll(pow(26, sou));
  }
  // cout<<sou<<endl;
  dfs("", 0);
  cout << ans << endl;
  return 0;
}