結果

問題 No.1779 Magical Swap
ユーザー tktk_snsntktk_snsn
提出日時 2021-12-19 00:54:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 780 ms / 2,000 ms
コード長 2,017 bytes
コンパイル時間 489 ms
コンパイル使用メモリ 11,032 KB
実行使用メモリ 52,476 KB
最終ジャッジ日時 2023-10-13 18:33:34
合計ジャッジ時間 10,293 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 199 ms
52,304 KB
testcase_01 AC 197 ms
52,308 KB
testcase_02 AC 231 ms
52,344 KB
testcase_03 AC 206 ms
52,476 KB
testcase_04 AC 560 ms
52,368 KB
testcase_05 AC 348 ms
52,356 KB
testcase_06 AC 364 ms
52,340 KB
testcase_07 AC 505 ms
52,340 KB
testcase_08 AC 211 ms
52,384 KB
testcase_09 AC 304 ms
52,448 KB
testcase_10 AC 690 ms
52,468 KB
testcase_11 AC 436 ms
52,360 KB
testcase_12 AC 266 ms
52,360 KB
testcase_13 AC 559 ms
52,424 KB
testcase_14 AC 737 ms
52,360 KB
testcase_15 AC 780 ms
52,348 KB
testcase_16 AC 652 ms
52,368 KB
testcase_17 AC 468 ms
52,376 KB
testcase_18 AC 201 ms
52,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import chain
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
U = 1 << 20


def prime_set(N):
    """
    Nまでの素数のsetを返す
    """
    if N < 4:
        return ({}, {}, {2}, {2, 3})[N]
    Nsq = int(N ** 0.5 + 0.5) + 1
    primes = {2, 3} | set(chain(range(5, N + 1, 6), range(7, N + 1, 6)))
    for i in range(5, Nsq, 2):
        if i in primes:
            primes -= set(range(i * i, N + 1, i * 2))
    return primes


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)
        self.rank = [0] * (n + 1)

    def find(self, x):
        stack = []
        while self.root[x] >= 0:
            stack.append(x)
            x = self.root[x]
        for i in stack:
            self.root[i] = x
        return x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
        return True

    def size(self, x):
        return -self.root[self.find(x)]


T = int(input())
P = sorted(prime_set(U))

for _ in range(T):
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    uf = UF_tree(N)
    for p in P:
        if p > N:
            break
        for i in range(p + p, N+1, p):
            uf.unite(p-1, i-1)
    gA = [[] for _ in range(N)]
    gB = [[] for _ in range(N)]
    for i in range(N):
        gA[uf.find(i)].append(A[i])
        gB[uf.find(i)].append(B[i])

    ok = True
    for i in range(N):
        gA[i].sort()
        gB[i].sort()
        if gA[i] != gB[i]:
            ok = False
            break
    if ok:
        print("Yes")
    else:
        print("No")
0