結果
問題 | No.1955 Not Prime |
ユーザー | terasa |
提出日時 | 2022-09-29 00:15:20 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,139 bytes |
コンパイル時間 | 349 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 319,772 KB |
最終ジャッジ日時 | 2024-06-02 01:44:56 |
合計ジャッジ時間 | 6,458 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 100 ms
83,144 KB |
testcase_01 | AC | 98 ms
83,200 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 107 ms
82,688 KB |
testcase_05 | AC | 106 ms
82,688 KB |
testcase_06 | AC | 154 ms
91,008 KB |
testcase_07 | AC | 238 ms
96,000 KB |
testcase_08 | AC | 239 ms
92,672 KB |
testcase_09 | AC | 256 ms
91,224 KB |
testcase_10 | AC | 216 ms
91,136 KB |
testcase_11 | AC | 981 ms
319,772 KB |
testcase_12 | AC | 179 ms
91,328 KB |
testcase_13 | AC | 260 ms
94,592 KB |
testcase_14 | AC | 186 ms
90,880 KB |
testcase_15 | AC | 172 ms
91,136 KB |
testcase_16 | AC | 210 ms
91,368 KB |
testcase_17 | AC | 297 ms
113,920 KB |
testcase_18 | AC | 263 ms
93,184 KB |
testcase_19 | AC | 289 ms
96,512 KB |
testcase_20 | AC | 102 ms
83,712 KB |
testcase_21 | AC | 191 ms
91,008 KB |
testcase_22 | WA | - |
testcase_23 | AC | 104 ms
83,072 KB |
testcase_24 | AC | 102 ms
83,328 KB |
testcase_25 | AC | 103 ms
82,816 KB |
ソースコード
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() if flag[v] is True: continue 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 vid(self, n): return 2 * (~n) + 1 if n < 0 else 2 * n def add_clause(self, s: int, t: int) -> None: def oppose(id): return id ^ 1 sid, tid = self.vid(s), self.vid(t) # ~s -> t self.E[oppose(sid)].append(tid) # ~t -> s self.E[oppose(tid)].append(sid) def if_then(self, s: int, t: int) -> None: sid, tid = self.vid(s), self.vid(t) self.E[sid].append(tid) 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.if_then(2 * i, ~(2 * i + 1)) solver.if_then(~(2 * i), 2 * i + 1) solver.if_then(2 * i + 1, ~(2 * i)) solver.if_then(~(2 * i + 1), 2 * i) 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.if_then(i, j) solver.if_then(~j, ~i) print('Yes' if solver.solve() is not None else 'No')