結果
問題 | No.1955 Not Prime |
ユーザー | H20 |
提出日時 | 2022-04-10 18:36:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 619 ms / 2,000 ms |
コード長 | 3,052 bytes |
コンパイル時間 | 160 ms |
コンパイル使用メモリ | 82,352 KB |
実行使用メモリ | 290,804 KB |
最終ジャッジ日時 | 2024-06-01 04:43:19 |
合計ジャッジ時間 | 7,887 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 102 ms
85,504 KB |
testcase_01 | AC | 101 ms
84,992 KB |
testcase_02 | AC | 97 ms
84,992 KB |
testcase_03 | AC | 94 ms
84,864 KB |
testcase_04 | AC | 94 ms
85,120 KB |
testcase_05 | AC | 96 ms
84,992 KB |
testcase_06 | AC | 189 ms
98,028 KB |
testcase_07 | AC | 452 ms
116,488 KB |
testcase_08 | AC | 440 ms
125,004 KB |
testcase_09 | AC | 409 ms
107,136 KB |
testcase_10 | AC | 195 ms
97,704 KB |
testcase_11 | AC | 619 ms
290,804 KB |
testcase_12 | AC | 303 ms
96,768 KB |
testcase_13 | AC | 463 ms
118,752 KB |
testcase_14 | AC | 299 ms
97,664 KB |
testcase_15 | AC | 294 ms
97,024 KB |
testcase_16 | AC | 396 ms
108,008 KB |
testcase_17 | AC | 478 ms
132,280 KB |
testcase_18 | AC | 469 ms
118,072 KB |
testcase_19 | AC | 393 ms
145,600 KB |
testcase_20 | AC | 97 ms
85,504 KB |
testcase_21 | AC | 311 ms
97,152 KB |
testcase_22 | AC | 95 ms
85,120 KB |
testcase_23 | AC | 95 ms
85,376 KB |
testcase_24 | AC | 97 ms
85,120 KB |
testcase_25 | AC | 96 ms
85,504 KB |
ソースコード
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=[] if not 1<=N<=500: exit() for a,b in AB: if 1<=int(a)<1000 and 1<=int(b)<1000 and a==str(int(a)) and b==str(int(b)): continue exit() 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')