結果

問題 No.1955 Not Prime
ユーザー vwxyzvwxyz
提出日時 2023-05-17 00:41:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 5,240 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 311,328 KB
最終ジャッジ日時 2024-12-14 22:06:09
合計ジャッジ時間 22,604 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 309 ms
31,592 KB
testcase_01 AC 305 ms
24,724 KB
testcase_02 AC 296 ms
24,488 KB
testcase_03 AC 301 ms
24,660 KB
testcase_04 AC 294 ms
24,640 KB
testcase_05 AC 294 ms
24,484 KB
testcase_06 AC 365 ms
25,076 KB
testcase_07 AC 1,079 ms
36,972 KB
testcase_08 AC 944 ms
34,608 KB
testcase_09 AC 1,472 ms
34,024 KB
testcase_10 AC 1,346 ms
24,620 KB
testcase_11 TLE -
testcase_12 AC 634 ms
27,640 KB
testcase_13 AC 1,500 ms
37,896 KB
testcase_14 AC 518 ms
28,616 KB
testcase_15 AC 506 ms
27,884 KB
testcase_16 AC 834 ms
34,792 KB
testcase_17 AC 1,442 ms
52,204 KB
testcase_18 AC 1,501 ms
38,064 KB
testcase_19 AC 1,568 ms
44,748 KB
testcase_20 AC 308 ms
24,548 KB
testcase_21 AC 653 ms
28,272 KB
testcase_22 AC 300 ms
24,492 KB
testcase_23 AC 308 ms
24,564 KB
testcase_24 AC 305 ms
24,664 KB
testcase_25 AC 309 ms
311,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            pow_p=[1]
            for _ in range(e):
                pow_p.append(pow_p[-1]*p)
            divisors=[i*j for i in divisors for j in pow_p]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

def SCC(N,edges):
    start = [0] * (N + 1)
    elist = [0] * len(edges)
    for e in edges:
        start[e[0] + 1] += 1
    for i in range(1, N + 1):
        start[i] += start[i - 1]
    counter = start[:]
    for e in edges:
        elist[counter[e[0]]] = e[1]
        counter[e[0]] += 1
    N = 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(start[v], start[v + 1]):
                        to = 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
    groups = [[] for _ in range(group_num)]
    for i, x in enumerate(ids):
        groups[x].append(i)
    return groups

class TwoSAT:
    def __init__(self,N):
        self.N=N
        self.edges=[]

    def Add_Clause(self,i,f,j,g):
        assert 0<=i<self.N
        assert 0<=j<self.N
        self.edges.append((2*i+(0 if f else 1),2*j+(1 if g else 0)))
        self.edges.append((2*j+(0 if g else 1),2*i+(1 if f else 0)))

    def Satisfiable(self):
        scc=SCC(2*self.N,self.edges)
        idx=[None]*2*self.N
        for i,lst in enumerate(scc):
            for x in lst:
                idx[x]=i
        retu=[None]*self.N
        for i in range(self.N):
            if idx[2*i]==idx[2*i+1]:
                return None
            retu[i]=idx[2*i]<idx[2*i+1]
        return retu

N=int(readline())
A,B=[],[]
for n in range(N):
    a,b=map(int,readline().split())
    A.append(a)
    B.append(b)
AB=[A,B]
P=Prime(10**6)
TSAT=TwoSAT(N)
for i in range(N):
    for j in range(N):
        for bl_i in (0,1):
            for bl_j in (0,1):
                if P.Is_Prime(int(str(AB[bl_i][i])+str(AB[bl_j][j]))):
                    TSAT.Add_Clause(i,bl_i,j,bl_j^1)
if TSAT.Satisfiable():
    ans="Yes"
else:
    ans="No"
print(ans)
0