Problem p03098 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end());
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
constexpr double pi = 3.1415926535897932384626433832795028;
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T1, typename T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> inline bool chmin(T1 &a, const T2 &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct Setup {
  static constexpr int PREC = 20;
  Setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

struct Permutation {
  const int n;
  std::vector<int> data;
  Permutation(int n) : n(n), data(n) {
    for (int i = 0; i < n; i++) {
      data[i] = i;
    }
  }
  Permutation(std::vector<int> &src) : n(src.size()), data(src) {}
  bool next() { return std::next_permutation(data.begin(), data.end()); }
  bool prev() { return std::prev_permutation(data.begin(), data.end()); }
  bool operator==(const Permutation &rhs) const { return data == rhs.data; }
  bool operator!=(const Permutation &rhs) const { return data != rhs.data; }
  int operator[](int i) const { return data[i]; }
  Permutation &operator*=(const Permutation &rhs) {
    std::vector<int> tmp(data);
    for (int i = 0; i < n; i++) {
      data[i] = tmp[rhs[i]];
    }
    return *this;
  }
  Permutation &operator/=(const Permutation &rhs) { return *this *= rhs.inv(); }
  Permutation operator*(const Permutation &rhs) const {
    return Permutation(*this) *= rhs;
  }
  Permutation operator/(const Permutation &rhs) const {
    return Permutation(*this) /= rhs;
  }
  Permutation inv() const {
    std::vector<int> ret(n);
    for (int i = 0; i < n; i++) {
      ret[data[i]] = i;
    }
    return ret;
  }
  Permutation pow(long long m) const {
    std::vector<int> ret(n);
    std::vector<bool> used(n);
    for (int i = 0; i < n; i++) {
      if (used[i]) {
        continue;
      }
      std::vector<int> cyc;
      int cur = i;
      do {
        used[cur] = true;
        cyc.push_back(cur);
        cur = data[cur];
      } while (cur != i);
      for (int j = 0; j < cyc.size(); j++) {
        ret[cyc[j]] = cyc[(j + m) % cyc.size()];
      }
    }
    return ret;
  }
  friend std::ostream &operator<<(std::ostream &os, const Permutation &rhs) {
    os << "{";
    for (int i = 0; i < rhs.n; i++) {
      os << rhs[i] << (i + 1 != rhs.n ? ", " : "");
    }
    os << "}";
    return os;
  }
};

int N, K;
signed main() {
  cin >> N >> K;
  vint p(N), q(N);
  rep(i, N) { cin >> p[i], q[i]--; }
  rep(i, N) { cin >> q[i], q[i]--; }
  Permutation P(p), Q(q);
  Permutation O(N);
  int k = (K - 1) % 6;
  if (k == 0) {
    O *= P;
  } else if (k == 1) {
    O *= Q;
  } else if (k == 2) {
    O *= Q / P;
  } else if (k == 3) {
    O *= Q / P / Q;
  } else if (k == 4) {
    O *= Q / P / Q * P / Q;
  } else if (k == 5) {
    O *= Q / P / Q * P * P / Q;
  }
  Permutation S = (Q / P / Q * P).pow((K - 1) / 6);
  Permutation A = S * O / S;
  rep(i, N) { cout << A[i] + 1 << endl; }
}

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

#define int long long
#define double long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end());
#define bit(n) (1LL << (n))
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
constexpr double pi = 3.1415926535897932384626433832795028;
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
template <typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
template <typename T1, typename T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> inline bool chmin(T1 &a, const T2 &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &rhs) {
  os << "(" << rhs.first << ", " << rhs.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
  os << "{";
  for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
    os << *itr << (next(itr) != rhs.end() ? ", " : "");
  }
  os << "}";
  return os;
}
struct Setup {
  static constexpr int PREC = 20;
  Setup() {
    cout << fixed << setprecision(PREC);
    cerr << fixed << setprecision(PREC);
  };
} setup;

struct Permutation {
  const int n;
  std::vector<int> data;
  Permutation(int n) : n(n), data(n) {
    for (int i = 0; i < n; i++) {
      data[i] = i;
    }
  }
  Permutation(std::vector<int> &src) : n(src.size()), data(src) {}
  bool next() { return std::next_permutation(data.begin(), data.end()); }
  bool prev() { return std::prev_permutation(data.begin(), data.end()); }
  bool operator==(const Permutation &rhs) const { return data == rhs.data; }
  bool operator!=(const Permutation &rhs) const { return data != rhs.data; }
  int operator[](int i) const { return data[i]; }
  Permutation &operator*=(const Permutation &rhs) {
    std::vector<int> tmp(data);
    for (int i = 0; i < n; i++) {
      data[i] = tmp[rhs[i]];
    }
    return *this;
  }
  Permutation &operator/=(const Permutation &rhs) { return *this *= rhs.inv(); }
  Permutation operator*(const Permutation &rhs) const {
    return Permutation(*this) *= rhs;
  }
  Permutation operator/(const Permutation &rhs) const {
    return Permutation(*this) /= rhs;
  }
  Permutation inv() const {
    std::vector<int> ret(n);
    for (int i = 0; i < n; i++) {
      ret[data[i]] = i;
    }
    return ret;
  }
  Permutation pow(long long m) const {
    std::vector<int> ret(n);
    std::vector<bool> used(n);
    for (int i = 0; i < n; i++) {
      if (used[i]) {
        continue;
      }
      std::vector<int> cyc;
      int cur = i;
      do {
        used[cur] = true;
        cyc.push_back(cur);
        cur = data[cur];
      } while (cur != i);
      for (int j = 0; j < cyc.size(); j++) {
        ret[cyc[j]] = cyc[(j + m) % cyc.size()];
      }
    }
    return ret;
  }
  friend std::ostream &operator<<(std::ostream &os, const Permutation &rhs) {
    os << "{";
    for (int i = 0; i < rhs.n; i++) {
      os << rhs[i] << (i + 1 != rhs.n ? ", " : "");
    }
    os << "}";
    return os;
  }
};

int N, K;
signed main() {
  cin >> N >> K;
  vint p(N), q(N);
  rep(i, N) { cin >> p[i], p[i]--; }
  rep(i, N) { cin >> q[i], q[i]--; }
  Permutation P(p), Q(q);
  Permutation O(N);
  int k = (K - 1) % 6;
  if (k == 0) {
    O *= P;
  } else if (k == 1) {
    O *= Q;
  } else if (k == 2) {
    O *= Q / P;
  } else if (k == 3) {
    O *= Q / P / Q;
  } else if (k == 4) {
    O *= Q / P / Q * P / Q;
  } else if (k == 5) {
    O *= Q / P / Q * P * P / Q;
  }
  Permutation S = (Q / P / Q * P).pow((K - 1) / 6);
  Permutation A = S * O / S;
  rep(i, N) { cout << A[i] + 1 << endl; }
}