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

#include <bits/stdc++.h>
using namespace std;

int po3(int e) {
  int ret = 1;
  for (int i = 0; i < e; i++) {
    ret *= 3;
  }

  return ret;
}

int conjug(int x) {
  int mult = 1;
  int ret = 0;

  while (x) {
    int t = x % 3;
    if (t == 1)
      ret += mult * 2;
    if (t == 2)
      ret += mult;

    x /= 3;
    mult *= 3;
  }

  return ret;
}

struct EndingSet {
  int ending;
  vector<int> nums;
};

int main() {
  int n;
  cin >> n;

  string T;
  cin >> T;

  int e = po3((n + 1) / 2);
  int b = po3(n) / e;

  EndingSet a[e];
  int con[b * e];
  for (int i = 0; i < b * e; i++) {
    con[i] = conjug(i);
  }
  for (int i = 0; i < e; i++) {
    a[i].ending = i;
    for (int j = 0; j < b; j++) {
      a[i].nums.push_back(i + j * e);
    }
  }

  int beg[b], ids[b * e], ans[b * e];
  for (int i = 0; i < b; i++) {
    beg[i] = i;
  }

  for (int i = 0; i < b * e; i++) {
    ids[i] = i / e;
  }

  for (int i = 0; i < T.size(); i++) {
    if (T[i] == 'S') {
      for (int j = 0; j < e; j++) {
        a[j].ending = con[a[j].ending];
      }

      for (int j = 0; j < b; j++) {
        beg[j] = con[beg[j]];
      }
    }

    if (T[i] == 'R') {
      int pos;
      for (int j = 0; j < e; j++) {
        if (a[j].ending == e - 1)
          pos = j;
      }

      int inv[b];
      for (int i = 0; i < b; i++) {
        inv[beg[i]] = i;
      }

      for (int x : a[pos].nums) {
        if (beg[ids[x]] < b - 1)
          ids[x] = inv[beg[ids[x]] + 1];
        else
          ids[x] = inv[0];
      }

      for (int j = 0; j < e; j++) {
        if (a[j].ending < e - 1)
          a[j].ending++;
        else
          a[j].ending = 0;
      }
    }
  }

  int inv[b * e];

  for (int j = 0; j < e; j++) {
    int x = a[j].ending;

    for (int y : a[j].nums) {
      ans[x + beg[ids[y]] * e] = y;
      inv[y] = x + beg[ids[y]] * e;
    }
  }

  for (int j = 0; j < b * e; j++) {
    cout << inv[j] << " ";
  }

  cout << endl;

  /*
  for(int i = 0; i<e; i++){
      int x = a[i].ending;

      for(int y: a[i].nums){
          ans[y] = x;
      }
  }

  for(int i = 0; i<b*e; i++){
      ans[i] += beg[ids[i]] * e;
  }

  for(int i = 0; i<b*e; i++){
      cout << ans[i] << " ";
  }
  */
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int po3(int e) {
  int ret = 1;
  for (int i = 0; i < e; i++) {
    ret *= 3;
  }

  return ret;
}

int conjug(int x) {
  int mult = 1;
  int ret = 0;

  while (x) {
    int t = x % 3;
    if (t == 1)
      ret += mult * 2;
    if (t == 2)
      ret += mult;

    x /= 3;
    mult *= 3;
  }

  return ret;
}

struct EndingSet {
  int ending;
  vector<int> nums;
};

int main() {
  int n;
  cin >> n;

  string T;
  cin >> T;

  int e = po3((n + 1) / 2);
  int b = po3(n) / e;

  EndingSet a[e];
  for (int i = 0; i < e; i++) {
    a[i].ending = i;
    for (int j = 0; j < b; j++) {
      a[i].nums.push_back(i + j * e);
    }
  }

  int beg[b], ids[b * e], ans[b * e];
  for (int i = 0; i < b; i++) {
    beg[i] = i;
  }

  for (int i = 0; i < b * e; i++) {
    ids[i] = i / e;
  }

  for (int i = 0; i < T.size(); i++) {
    if (T[i] == 'S') {
      for (int j = 0; j < e; j++) {
        a[j].ending = conjug(a[j].ending);
      }

      for (int j = 0; j < b; j++) {
        beg[j] = conjug(beg[j]);
      }
    }

    if (T[i] == 'R') {
      int pos;
      for (int j = 0; j < e; j++) {
        if (a[j].ending == e - 1)
          pos = j;
      }

      int inv[b];
      for (int i = 0; i < b; i++) {
        inv[beg[i]] = i;
      }

      for (int x : a[pos].nums) {
        if (beg[ids[x]] < b - 1)
          ids[x] = inv[beg[ids[x]] + 1];
        else
          ids[x] = inv[0];
      }

      for (int j = 0; j < e; j++) {
        if (a[j].ending < e - 1)
          a[j].ending++;
        else
          a[j].ending = 0;
      }
    }
  }

  int inv[b * e];

  for (int j = 0; j < e; j++) {
    int x = a[j].ending;

    for (int y : a[j].nums) {
      ans[x + beg[ids[y]] * e] = y;
      inv[y] = x + beg[ids[y]] * e;
    }
  }

  for (int j = 0; j < b * e; j++) {
    cout << inv[j] << " ";
  }

  cout << endl;

  /*
  for(int i = 0; i<e; i++){
      int x = a[i].ending;

      for(int y: a[i].nums){
          ans[y] = x;
      }
  }

  for(int i = 0; i<b*e; i++){
      ans[i] += beg[ids[i]] * e;
  }

  for(int i = 0; i<b*e; i++){
      cout << ans[i] << " ";
  }
  */
}