結果
問題 | No.308 素数は通れません |
ユーザー |
![]() |
提出日時 | 2015-12-01 01:54:03 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,555 bytes |
コンパイル時間 | 456 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 88,832 KB |
最終ジャッジ日時 | 2024-11-27 18:09:52 |
合計ジャッジ時間 | 18,614 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | RE * 107 |
ソースコード
from fractions import gcddef suspect(a, s, d, n):x = pow(a, d, n)if x == 1:return Truefor r in range(s):if x == n - 1:return Truex = x * x % nreturn Falsedef isPrime(n):if n <= 1 or (n > 2 and n % 2 == 0):return Falsetest = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]d, s = n - 1, 0while d % 2 == 0:s += 1d //= 2for a in test:if a >= n:breakif not suspect(a, s, d, n):return Falsereturn Truedef getNext(n, W, N):i, j = (n - 1) // W, (n - 1) % Wfor dy, dx in [(0,1),(1,0),(0,-1),(-1,0)]:yy, xx = i + dy, j + dxif yy < 0 or xx < 0 or xx >= W:continuem = yy * W + xx + 1if m > N or isPrime(m):continueyield mdef search(W, N):ds = [d for d in range(0,W) if gcd(d,W) != 1]vis, dvis = {}, {}q = [1]vis[1] = Trueh = 0while h < len(q):n = q[h]if n == N:return 0h += 1if gcd(n % W, W) > 1:if not n % W in dvis:# print("%d: %d, %d" % (W, n % W, n))dvis[n % W] = Trueif len(dvis) == len(ds):breakfor m in getNext(n, W, N):if not m in vis:vis[m] = Trueq.append(m)return len(dvis) == len(ds) and 1 or 2def search2(W, N):vis = {}q = [N]vis[N] = Trueh = 0while h < len(q):n = q[h]if gcd(n % W, W) > 1:return Trueh += 1for m in getNext(n, W, N):if not m in vis:vis[m] = Trueq.append(m)return FalseN = int(input())W = 2ok = Falsewhile True:if ok:breakres = search(W, N)if res == 0:breakif res == 1:if search2(W, N):breakW += 1print(W)