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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

// n??????
// Radix r;
// cout << r.to(n, 4) << endl;
class Radix {
private:
  const char *s;
  int a[128];

public:
  Radix(const char *s = "0123456789ABCDEF") : s(s) {
    for (int i = 0; s[i]; i++)
      a[(int)s[i]] = i;
  }
  string to(long long p, int q) {
    if (!p)
      return "0";
    char t[64] = {};
    int i;
    for (i = 62; p; i--) {
      t[i] = s[p % q];
      p /= q;
    }
    return string(t + i + 1);
  }
  string to(const string &t, int p, int q) { return to(to(t, p), q); }
  long long to(const string &t, int p) {
    long long sm = a[(int)t[0]];
    for (int i = 1; i < (int)t.length(); i++)
      sm = sm * p + a[(int)t[i]];
    return sm;
  }
};

signed main() {
  for (int n; cin >> n && n;) {
    Radix r("01235789");
    cout << r.to(n, 8) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

#define int long long
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

signed main() {
  // i?????? S????????? 4or6????????????
  int dp[12][2][2] = {};
  unordered_map<int, int> m;
  for (string S; cin >> S && S != "0";) {
    string prev = "-1";
    m[stoll(prev)] = 0;
    while (prev != S) {
      memset(dp, 0, sizeof(dp));
      dp[0][0][0] = 1;
      rep(i, 0, S.size()) rep(j, 0, 2) rep(k, 0, 2) {
        int lim = j ? 9 : S[i] - '0';
        rep(d, 0, lim + 1) {
          //?????§??????????¢??????????????????????????¢????
          (dp[i + 1][j || d < lim][k || d == 4 || d == 6] += dp[i][j][k]);
        }
      }
      int s = m[stoll(prev)];
      prev = S;
      m[stoll(S)] = dp[S.size()][0][1] + dp[S.size()][1][1];
      S = to_string(stoll(S) + m[stoll(S)] - s);
      // dump(S);
    }
    cout << S << endl;
  }
  return 0;
}