//---------- begin union_find ---------- pub struct DSU { p: Vec, } impl DSU { pub fn new(n: usize) -> DSU { assert!(n < std::i32::MAX as usize); DSU { p: vec![-1; n] } } pub fn init(&mut self) { self.p.iter_mut().for_each(|p| *p = -1); } pub fn root(&self, mut x: usize) -> usize { assert!(x < self.p.len()); while self.p[x] >= 0 { x = self.p[x] as usize; } x } pub fn same(&self, x: usize, y: usize) -> bool { assert!(x < self.p.len() && y < self.p.len()); self.root(x) == self.root(y) } pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> { assert!(x < self.p.len() && y < self.p.len()); let mut x = self.root(x); let mut y = self.root(y); if x == y { return None; } if self.p[x] > self.p[y] { std::mem::swap(&mut x, &mut y); } self.p[x] += self.p[y]; self.p[y] = x as i32; Some((x, y)) } pub fn parent(&self, x: usize) -> Option { assert!(x < self.p.len()); let p = self.p[x]; if p >= 0 { Some(p as usize) } else { None } } pub fn sum(&self, mut x: usize, mut f: F) -> usize where F: FnMut(usize), { while let Some(p) = self.parent(x) { f(x); x = p; } x } pub fn size(&self, x: usize) -> usize { assert!(x < self.p.len()); let r = self.root(x); (-self.p[r]) as usize } } //---------- end union_find ---------- use std::io::*; fn read() -> Vec<(Vec, Vec)> { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::()); let mut next = || it.next().unwrap(); let t = next(); (0..t).map(|_| { let n = next(); let a = (0..n).map(|_| next()).collect(); let b = (0..n).map(|_| next()).collect(); (a, b) }).collect() } fn main() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for (a, b) in read() { let n = a.len(); let mut dsu = DSU::new(n); for k in 2..=n { for j in 2..=(n / k) { dsu.unite(k * (j - 1) - 1, k * j - 1); } } let mut a = a.iter().enumerate().map(|a| (dsu.root(a.0), *a.1)).collect::>(); let mut b = b.iter().enumerate().map(|a| (dsu.root(a.0), *a.1)).collect::>(); a.sort(); b.sort(); if a == b { writeln!(out, "Yes").ok(); } else { writeln!(out, "No").ok(); } } }