結果

問題 No.1955 Not Prime
ユーザー 👑 H20H20
提出日時 2022-03-21 12:38:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 654 ms / 2,000 ms
コード長 2,899 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 87,236 KB
実行使用メモリ 297,480 KB
最終ジャッジ日時 2023-08-23 07:00:03
合計ジャッジ時間 9,818 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
97,272 KB
testcase_01 AC 126 ms
96,972 KB
testcase_02 AC 130 ms
96,944 KB
testcase_03 AC 124 ms
96,920 KB
testcase_04 AC 123 ms
96,908 KB
testcase_05 AC 126 ms
97,016 KB
testcase_06 AC 218 ms
98,176 KB
testcase_07 AC 471 ms
115,388 KB
testcase_08 AC 461 ms
117,804 KB
testcase_09 AC 439 ms
116,440 KB
testcase_10 AC 214 ms
98,208 KB
testcase_11 AC 654 ms
297,480 KB
testcase_12 AC 446 ms
99,052 KB
testcase_13 AC 481 ms
117,332 KB
testcase_14 AC 325 ms
98,432 KB
testcase_15 AC 326 ms
99,448 KB
testcase_16 AC 432 ms
110,148 KB
testcase_17 AC 491 ms
126,844 KB
testcase_18 AC 487 ms
117,540 KB
testcase_19 AC 427 ms
147,636 KB
testcase_20 AC 128 ms
96,956 KB
testcase_21 AC 333 ms
100,476 KB
testcase_22 AC 127 ms
97,004 KB
testcase_23 AC 127 ms
96,984 KB
testcase_24 AC 123 ms
97,004 KB
testcase_25 AC 127 ms
96,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False

    for i in range(2, n+1):
        if is_prime[i-1]:
            j = i * i
            while j <= n:
                is_prime[j-1] = False
                j += i
    table = [ i for i in range(1, n+1) if is_prime[i-1]]
    return [False]+is_prime, table

def two_sat(n,clause):
    answer=[0]*n
    edges=[]
    N=2*n
    for s in clause:
        i,f,j,g=s
        edges.append((2*i+(0 if f else 1),2*j+(1 if g else 0)))
        edges.append((2*j+(0 if g else 1),2*i+(1 if f else 0)))
    M=len(edges)
    start=[0]*(N+1)
    elist=[0]*M
    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
    visited=[]
    low=[0]*N
    Ord=[-1]*N
    ids=[0]*N
    NG=[0,0]
    def dfs(v):
        stack=[(v,-1,0),(v,-1,1)]
        while stack:
            v,bef,t=stack.pop()
            if t:
                if bef!=-1 and Ord[v]!=-1:
                    low[bef]=min(low[bef],Ord[v])
                    stack.pop()
                    continue
                low[v]=NG[0]
                Ord[v]=NG[0]
                NG[0]+=1
                visited.append(v)
                for i in range(start[v],start[v+1]):
                    to=elist[i]
                    if Ord[to]==-1:
                        stack.append((to,v,0))
                        stack.append((to,v,1))
                    else:
                        low[v]=min(low[v],Ord[to])
            else:
                if low[v]==Ord[v]:
                    while(True):
                        u=visited.pop()
                        Ord[u]=N
                        ids[u]=NG[1]
                        if u==v:
                            break
                    NG[1]+=1
                low[bef]=min(low[bef],low[v])
    for i in range(N):
        if Ord[i]==-1:
            dfs(i)
    for i in range(N):
        ids[i]=NG[1]-1-ids[i]
    for i in range(n):
        if ids[2*i]==ids[2*i+1]:
            return None
        answer[i]=(ids[2*i]<ids[2*i+1])
    return answer

N = int(input())
T,_ =  sieve(10**6)
AB = [list(input().split()) for _ in range(N)]
clause=[]
for i in range(N):
    for j in range(i,N):
        ia,ib,ja,jb = AB[i][0],AB[i][1],AB[j][0],AB[j][1]
        if T[int(ia+ib)] or T[int(ja+jb)] or T[int(ia+jb)] or T[int(ja+ib)]:
            clause.append((i,0,j,0))
        if T[int(ia+ib)] or T[int(jb+ja)] or T[int(ia+ja)] or T[int(jb+ib)]:
            clause.append((i,0,j,1))
        if T[int(ib+ia)] or T[int(ja+jb)] or T[int(ib+jb)] or T[int(ja+ia)]:
            clause.append((i,1,j,0))
        if T[int(ib+ia)] or T[int(jb+ja)] or T[int(jb+ia)] or T[int(ib+ja)]:
            clause.append((i,1,j,1))

ans=two_sat(N,clause)
if ans==None:
    print('No')
else:
    print('Yes')
0