結果
問題 | No.1955 Not Prime |
ユーザー | terasa |
提出日時 | 2022-09-28 23:49:39 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,845 bytes |
コンパイル時間 | 353 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 326,756 KB |
最終ジャッジ日時 | 2024-06-02 01:43:20 |
合計ジャッジ時間 | 5,377 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 95 ms
91,108 KB |
testcase_01 | AC | 90 ms
83,276 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 91 ms
83,764 KB |
testcase_05 | AC | 88 ms
84,644 KB |
testcase_06 | AC | 131 ms
90,772 KB |
testcase_07 | AC | 231 ms
96,368 KB |
testcase_08 | AC | 212 ms
93,876 KB |
testcase_09 | AC | 239 ms
91,892 KB |
testcase_10 | AC | 199 ms
91,572 KB |
testcase_11 | TLE | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
ソースコード
import sys import pypyjit import itertools import heapq import math from collections import deque, defaultdict from functools import lru_cache from typing import List # for AtCoder Easy test if __file__ == 'prog.py': pass else: sys.setrecursionlimit(10 ** 6) pypyjit.set_param('max_unroll_recursion=-1') input = sys.stdin.readline def readints(): return map(int, input().split()) def readlist(): return list(readints()) def readstr(): return input()[:-1] class SCC: def __init__(self, N, E): self.N = N self.E = E self.I = [[] for _ in range(N)] for s in range(N): for t in E[s]: self.I[t].append(s) self.V = [] self.cid = [None] * N self.c_num = 0 self.traverse() self.traverse2() self.C = [[] for _ in range(self.c_num)] for i in range(self.N): c = self.cid[i] self.C[c].append(i) def traverse(self): flag = [False] * self.N for i in range(self.N): if flag[i] is False: self.dfs(i, flag) self.V.reverse() def traverse2(self): flag = [False] * self.N cid = 0 for v in self.V: if flag[v] is False: self.dfs2(v, flag) self.c_num += 1 def dfs(self, v, flag): stack = [~v, v] while stack: v = stack.pop() if v < 0: self.V.append(~v) continue if flag[v] is True: stack.pop() continue flag[v] = True for dest in self.E[v]: if flag[dest] is False: stack.append(~dest) stack.append(dest) def dfs2(self, v, flag): stack = [v] while stack: v = stack.pop() flag[v] = True self.cid[v] = self.c_num for dest in self.I[v]: if flag[dest] is False: stack.append(dest) class TwoSat: def __init__(self, N: int): self.N = N self.E = [[] for _ in range(2 * N)] def add_clause(self, s: int, t: int) -> None: def vid(n): return 2 * (~n) + 1 if n < 0 else 2 * n def oppose(id): return id ^ 1 sid, tid = vid(s), vid(t) # ~s -> t self.E[oppose(sid)].append(tid) # ~t -> s self.E[oppose(tid)].append(sid) def solve(self) -> List[bool]: cid = SCC(2 * self.N, self.E).cid for i in range(self.N): if cid[2 * i] == cid[2 * i + 1]: return None # if cid[2 * a] > cid[2 * a + 1], possibly ~a -> a ans = [cid[2 * i] > cid[2 * i + 1] for i in range(self.N)] return ans class PrimeTable: def __init__(self, N): self.is_prime = [True] * (N + 1) self.is_prime[0] = False self.is_prime[1] = False for i in range(2, N + 1): if i * i > N: break if self.is_prime[i] is False: continue for j in range(2, N + 1): if i * j > N: break self.is_prime[i * j] = False self.primes = [n for n in range(2, N + 1) if self.is_prime[n]] N = int(input()) is_prime = PrimeTable(10 ** 6).is_prime A = [] for _ in range(N): a, b = readints() A.append(a) A.append(b) solver = TwoSat(2 * N) for i in range(N): solver.add_clause(2 * i, 2 * i + 1) solver.add_clause(~(2 * i), ~(2 * i + 1)) for i in range(2 * N): for j in range(2 * N): if i ^ j <= 1: continue n = int(str(A[i]) + str(A[j])) if is_prime[n]: solver.add_clause(i, ~j) print('Yes' if solver.solve() is not None else 'No')