/* -*- coding: utf-8 -*- * * 1779.cc: No.1779 Magical Swap - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; /* typedef */ typedef multiset msi; template struct UFT { int links[MAX_N], ranks[MAX_N], sizes[MAX_N]; UFT() {} void init(int n) { for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1; } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ int as[MAX_N + 1], bs[MAX_N + 1]; msi sas[MAX_N + 1], sbs[MAX_N + 1]; UFT uft; /* subroutines */ void mergemsi(msi &s0, msi &s1) { if (s0.size() < s1.size()) swap(s0, s1); for (auto v: s1) s0.insert(v); s1.clear(); } void printmsi(msi &s) { for (auto v: s) printf("%d ", v); } bool eqmsi(msi &s0, msi &s1) { //printmsi(s0), printf("<=> "), printmsi(s1), putchar('\n'); if (s0.size() != s1.size()) return false; msi::iterator sit0 = s0.begin(); msi::iterator sit1 = s1.begin(); for (; sit0 != s0.end(); sit0++, sit1++) if (*sit0 != *sit1) return false; return true; } /* main */ int main() { int tn; scanf("%d", &tn); while (tn--) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", as + i); for (int i = 1; i <= n; i++) scanf("%d", bs + i); for (int i = 1; i <= n; i++) { sas[i].clear(), sas[i].insert(as[i]); sbs[i].clear(), sbs[i].insert(bs[i]); } uft.init(n + 1); for (int u = 2; u * 2 <= n; u++) for (int v = u * 2; v <= n; v += u) if (! uft.same(u, v)) { int p = uft.merge(u, v), q = (p != u) ? u : v; mergemsi(sas[p], sas[q]); mergemsi(sbs[p], sbs[q]); } bool ok = true; for (int i = 1; ok && i <= n; i++) if (uft.root(i) == i) ok = eqmsi(sas[i], sbs[i]); if (ok) puts("Yes"); else puts("No"); } return 0; }