結果
問題 | No.1955 Not Prime |
ユーザー | rlangevin |
提出日時 | 2024-04-15 12:24:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 543 ms / 2,000 ms |
コード長 | 4,457 bytes |
コンパイル時間 | 287 ms |
コンパイル使用メモリ | 82,264 KB |
実行使用メモリ | 118,624 KB |
最終ジャッジ日時 | 2024-10-05 08:23:21 |
合計ジャッジ時間 | 6,511 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 72 ms
66,176 KB |
testcase_01 | AC | 77 ms
66,432 KB |
testcase_02 | AC | 72 ms
66,432 KB |
testcase_03 | AC | 68 ms
66,304 KB |
testcase_04 | AC | 67 ms
65,920 KB |
testcase_05 | AC | 68 ms
66,304 KB |
testcase_06 | AC | 147 ms
85,888 KB |
testcase_07 | AC | 463 ms
108,080 KB |
testcase_08 | AC | 422 ms
103,700 KB |
testcase_09 | AC | 385 ms
97,892 KB |
testcase_10 | AC | 69 ms
66,816 KB |
testcase_11 | AC | 68 ms
67,072 KB |
testcase_12 | AC | 259 ms
90,668 KB |
testcase_13 | AC | 401 ms
102,344 KB |
testcase_14 | AC | 254 ms
91,100 KB |
testcase_15 | AC | 245 ms
89,876 KB |
testcase_16 | AC | 382 ms
99,312 KB |
testcase_17 | AC | 543 ms
118,624 KB |
testcase_18 | AC | 418 ms
102,528 KB |
testcase_19 | AC | 362 ms
106,088 KB |
testcase_20 | AC | 71 ms
66,304 KB |
testcase_21 | AC | 259 ms
91,720 KB |
testcase_22 | AC | 69 ms
66,432 KB |
testcase_23 | AC | 75 ms
66,432 KB |
testcase_24 | AC | 71 ms
66,560 KB |
testcase_25 | AC | 66 ms
66,304 KB |
ソースコード
import sys input = sys.stdin.readline from math import sqrt, ceil def Sieve(n): lst = [True] * (n + 1) lst[0] = lst[1] = False for i in range(2, ceil(sqrt(n)) + 1): if lst[i]: for j in range(2 * i, n + 1, i): lst[j] = False return lst class DirectedGraph(): def __init__(self, N): self.N = N self.G = [[] for i in range(N)] self.rG = [[] for i in range(N)] self.order = [] self.used1 = [0] * N self.used2 = [0] * N self.group = [-1] * N self.label = 0 self.seen = [0] * N self.Edge = set() def add_edge(self, u, v): #多重辺は排除する if (u, v) not in self.Edge: self.G[u].append(v) self.rG[v].append(u) self.Edge.add((u, v)) def dfs(self, s): stack = [~s, s] while stack: u = stack.pop() if u >= 0: if self.used1[u]: continue self.used1[u] = 1 for v in self.G[u]: if self.used1[v]: continue stack.append(~v) stack.append(v) else: u = ~u if self.seen[u]: continue self.seen[u]= 1 self.order.append(u) def rdfs(self, s, num): stack = [s] while stack: u = stack.pop() if u >= 0: self.used2[u] = 1 self.group[u] = num for v in self.rG[u]: if self.used2[v]: continue stack.append(v) def scc(self): for i in range(self.N): if self.used1[i]: continue self.dfs(i) for s in reversed(self.order): if self.used2[s]: continue self.rdfs(s, self.label) self.label += 1 return self.label, self.group def construct(self): nG = [set() for _ in range(self.label)] mem = [[] for i in range(self.label)] for s in range(self.N): now = self.group[s] for u in self.G[s]: if now == self.group[u]: continue nG[now].add(self.group[u]) mem[now].append(s) return nG, mem class TwoSAT(): def __init__(self, N): self.N = N self.G = DirectedGraph(2 * N) def add(self, x1, x2, f1, f2): if f1 == True and f2 == True: # ¬x1∪¬x2 # (x1⇒¬x2)∩(x2⇒¬x1) self.G.add_edge(x1, x2 + self.N) self.G.add_edge(x2, x1 + self.N) if f1 == True and f2 == False: # ¬x1∪x2 # (x1⇒x2)∩(¬x2⇒¬x1) self.G.add_edge(x1, x2) self.G.add_edge(x2 + self.N, x1 + self.N) if f1 == False and f2 == True: # x1∪¬x2 # (¬x1⇒¬x2)∩(x2⇒x1) self.G.add_edge(x1 + self.N, x2 + self.N) self.G.add_edge(x2, x1) if f1 == False and f2 == False: # x1∪x2 # (¬x1⇒x2)∩(¬x2⇒x1) self.G.add_edge(x1 + self.N, x2) self.G.add_edge(x2 + self.N, x1) def check(self): _, group = self.G.scc() ans = [] for i in range(self.N): if group[i] == group[i + self.N]: print("No") exit() if group[i] > group[i + self.N]: ans.append(1) else: ans.append(0) return ans N = int(input()) A, B = [], [] S = set() for i in range(N): a, b = input().split() if (a, b) in S: continue S.add((a, b)) S.add((b, a)) A.append(a) B.append(b) N = len(A) D = Sieve(10**6+5) TS = TwoSAT(N) for i in range(N): for j in range(i, N): if D[int(A[i]+B[j])] or D[int(A[j]+B[i])]: TS.add(i, j, True, True) if D[int(B[i]+B[j])] or D[int(A[j]+A[i])]: TS.add(i, j, False, True) if D[int(A[i]+A[j])] or D[int(B[j]+B[i])]: TS.add(i, j, True, False) if D[int(B[i]+A[j])] or D[int(B[j]+A[i])]: TS.add(i, j, False, False) if TS.check(): print("Yes")