結果
問題 | No.1779 Magical Swap |
ユーザー | mkawa2 |
提出日時 | 2021-12-08 01:08:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 270 ms / 2,000 ms |
コード長 | 3,255 bytes |
コンパイル時間 | 171 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 127,104 KB |
最終ジャッジ日時 | 2024-07-16 07:38:38 |
合計ジャッジ時間 | 3,683 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 60 ms
63,232 KB |
testcase_01 | AC | 56 ms
63,488 KB |
testcase_02 | AC | 165 ms
78,828 KB |
testcase_03 | AC | 89 ms
77,808 KB |
testcase_04 | AC | 90 ms
90,240 KB |
testcase_05 | AC | 72 ms
82,272 KB |
testcase_06 | AC | 73 ms
84,736 KB |
testcase_07 | AC | 90 ms
89,856 KB |
testcase_08 | AC | 58 ms
65,152 KB |
testcase_09 | AC | 144 ms
85,248 KB |
testcase_10 | AC | 270 ms
119,680 KB |
testcase_11 | AC | 183 ms
98,688 KB |
testcase_12 | AC | 131 ms
82,328 KB |
testcase_13 | AC | 216 ms
110,976 KB |
testcase_14 | AC | 258 ms
127,104 KB |
testcase_15 | AC | 185 ms
79,488 KB |
testcase_16 | AC | 97 ms
92,800 KB |
testcase_17 | AC | 208 ms
78,336 KB |
testcase_18 | AC | 55 ms
63,488 KB |
ソースコード
import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = 18446744073709551615 inf = 4294967295 md = 10**9+7 # md = 998244353 class Sieve: def __init__(self, n): self.plist = [2] # n以下の素数のリスト min_prime_factor = [2, 0]*(n//2+1) for x in range(3, n+1, 2): if min_prime_factor[x] == 0: min_prime_factor[x] = x self.plist.append(x) if x**2 > n: continue for y in range(x**2, n+1, 2*x): if min_prime_factor[y] == 0: min_prime_factor[y] = x self.min_prime_factor = min_prime_factor def isprime(self, x): return self.min_prime_factor[x] == x # これが素因数分解(prime factorization) def pfct(self, x): pp, ee = [], [] while x > 1: mpf = self.min_prime_factor[x] if pp and mpf == pp[-1]: ee[-1] += 1 else: pp.append(mpf) ee.append(1) x //= mpf return [(p, e) for p, e in zip(pp, ee)] class UnionFind: def __init__(self, n): self.table = [-1]*n self.mem = [[u] for u in range(n)] self.cnt = n def root(self, u): stack = [] while self.table[u] >= 0: stack.append(u) u = self.table[u] for v in stack: self.table[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u = self.root(u) v = self.root(v) if u == v: return False su = -self.table[u] sv = -self.table[v] if su < sv: u, v = v, u self.table[u] = -su-sv self.table[v] = u self.mem[u] += self.mem[v] self.mem[v].clear() self.cnt -= 1 return True def member(self, u): return self.mem[self.root(u)] # グループの要素数 def size(self, u): return -self.table[self.root(u)] from collections import Counter def solve(): n = II() aa = [0]+LI() bb = [0]+LI() if aa[1] != bb[1]: print("No") return uf = UnionFind(n+1) for p in sv.plist: if p*2 > n: break for a in range(p*2, n+1, p): uf.merge(p, a) for ii in uf.mem: if not ii or ii[0] == 0: continue ca = Counter() cb = Counter() for i in ii: ca[aa[i]] += 1 cb[bb[i]] += 1 if ca != cb: print("No") return print("Yes") return sv = Sieve(100005) for case in range(II()): solve()