結果
問題 | No.1955 Not Prime |
ユーザー | chineristAC |
提出日時 | 2022-05-20 22:44:47 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,805 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 82,188 KB |
実行使用メモリ | 262,384 KB |
最終ジャッジ日時 | 2024-09-20 09:04:25 |
合計ジャッジ時間 | 11,960 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 172 ms
84,592 KB |
testcase_01 | AC | 172 ms
84,712 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 172 ms
84,396 KB |
testcase_05 | WA | - |
testcase_06 | AC | 310 ms
93,228 KB |
testcase_07 | AC | 657 ms
171,560 KB |
testcase_08 | AC | 617 ms
171,556 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 811 ms
255,496 KB |
testcase_14 | AC | 399 ms
116,436 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | AC | 570 ms
256,336 KB |
testcase_20 | WA | - |
testcase_21 | AC | 552 ms
141,760 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | AC | 166 ms
84,480 KB |
testcase_25 | WA | - |
ソースコード
import sys,random,bisect from collections import deque,defaultdict import heapq from itertools import permutations from math import gcd input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) class scc_graph: def __init__(self, N): self.N = N self.edges = [] def csr(self): self.start = [0]*(self.N+1) self.elist = [0]*len(self.edges) for e in self.edges: self.start[e[0]+1] += 1 for i in range(1, self.N+1): self.start[i] += self.start[i-1] counter = self.start[:] for e in self.edges: self.elist[counter[e[0]]] = e[1] counter[e[0]] += 1 def add_edge(self, v, w): self.edges.append((v, w)) def scc_ids(self): self.csr() N = self.N now_ord = group_num = 0 visited = [] low = [0]*N order = [-1]*N ids = [0]*N parent = [-1]*N stack = [] for i in range(N): if order[i] == -1: stack.append(i) stack.append(i) while stack: v = stack.pop() if order[v] == -1: low[v] = order[v] = now_ord now_ord += 1 visited.append(v) for i in range(self.start[v], self.start[v+1]): to = self.elist[i] if order[to] == -1: stack.append(to) stack.append(to) parent[to] = v else: low[v] = min(low[v], order[to]) else: if low[v] == order[v]: while True: u = visited.pop() order[u] = N ids[u] = group_num if u == v: break group_num += 1 if parent[v] != -1: low[parent[v]] = min(low[parent[v]], low[v]) for i, x in enumerate(ids): ids[i] = group_num-1-x return group_num, ids def scc(self): group_num, ids = self.scc_ids() groups = [[] for _ in range(group_num)] for i, x in enumerate(ids): groups[x].append(i) return groups class two_sat: def __init__(self,N): self.N = N self.answer = [-1]*N self.scc = scc_graph(2*N) def add_clause(self,i,f,j,g): self.scc.add_edge(2*i+(f==0),2*j+(g==1)) self.scc.add_edge(2*j+(g==0),2*i+(f==1)) def satisfiable(self): _,ids = self.scc.scc_ids() for i in range(N): if ids[2*i] == ids[2*i+1]: return False self.answer[i] = ids[2*i] < ids[2*i+1] return True M = 10**6 prime = [True] * (M+1) prime[1] = False for p in range(2,M+1): if not prime[p]: continue for n in range(2*p,M+1,p): prime[p] = False def check(x,y): n = int(str(x) + str(y)) return prime[n] N = int(input()) AB = [tuple(mi()) for i in range(N)] G = two_sat(N) for i in range(N): a,b = AB[i] for j in range(N): c,d = AB[j] if check(a,d) or check(c,b): G.add_clause(i,1,j,1) if check(a,c) or check(d,b): G.add_clause(i,1,j,0) if check(b,d) or check(c,a): G.add_clause(i,0,j,1) if check(b,c) or check(d,a): G.add_clause(i,0,j,0) if G.satisfiable(): print("Yes") else: print("No")