結果
問題 | No.1955 Not Prime |
ユーザー | terasa |
提出日時 | 2022-09-29 00:27:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 902 ms / 2,000 ms |
コード長 | 3,966 bytes |
コンパイル時間 | 180 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 319,708 KB |
最終ジャッジ日時 | 2024-06-02 01:45:50 |
合計ジャッジ時間 | 6,017 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 99 ms
84,208 KB |
testcase_01 | AC | 94 ms
83,888 KB |
testcase_02 | AC | 94 ms
83,412 KB |
testcase_03 | AC | 99 ms
83,460 KB |
testcase_04 | AC | 97 ms
83,156 KB |
testcase_05 | AC | 96 ms
83,172 KB |
testcase_06 | AC | 140 ms
91,148 KB |
testcase_07 | AC | 230 ms
95,944 KB |
testcase_08 | AC | 211 ms
92,556 KB |
testcase_09 | AC | 237 ms
91,524 KB |
testcase_10 | AC | 206 ms
91,224 KB |
testcase_11 | AC | 902 ms
319,708 KB |
testcase_12 | AC | 179 ms
91,720 KB |
testcase_13 | AC | 241 ms
94,984 KB |
testcase_14 | AC | 166 ms
91,692 KB |
testcase_15 | AC | 163 ms
91,636 KB |
testcase_16 | AC | 207 ms
91,236 KB |
testcase_17 | AC | 274 ms
107,016 KB |
testcase_18 | AC | 244 ms
92,588 KB |
testcase_19 | AC | 278 ms
96,852 KB |
testcase_20 | AC | 98 ms
84,344 KB |
testcase_21 | AC | 177 ms
91,580 KB |
testcase_22 | AC | 97 ms
83,952 KB |
testcase_23 | AC | 94 ms
83,784 KB |
testcase_24 | AC | 92 ms
84,108 KB |
testcase_25 | AC | 94 ms
83,216 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: sid, tid = self.vid(s), self.vid(t) # ~s -> t self.E[sid ^ 1].append(tid) # ~t -> s self.E[tid ^ 1].append(sid) def if_then(self, s: int, t: int) -> None: sid, tid = self.vid(s), self.vid(t) self.add_clause(~s, t) 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: continue n = int(str(A[i]) + str(A[j])) if is_prime[n]: solver.if_then(i, j) print('Yes' if solver.solve() is not None else 'No')