結果
| 問題 |
No.2751 429-like Number
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-10 23:02:30 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,578 bytes |
| コンパイル時間 | 259 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,008 KB |
| 最終ジャッジ日時 | 2024-12-20 07:13:20 |
| 合計ジャッジ時間 | 16,492 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 6 |
| other | AC * 6 WA * 16 |
ソースコード
def suspect(a, t, n):
x = pow(a, t, n)
n1 = n - 1
while t != n1 and x != 1 and x != n1:
x = pow(x, 2, n)
t <<= 1
return t & 1 or x == n1
# メイン
# 2^64までの決定的アルゴリズムとして実装しているので、ランダム要素は無い
# ランダムとして用いる場合は、check_listにランダム抽出された数を採用し、20~50個程度試す
def miller_rabin(n):
if n == 2:
return True
if n < 2 or n % 2 == 0:
return False
d = (n - 1) >> 1
while d & 1 == 0:
d >>= 1
check_list = (2, 7, 61) if n < 2 ** 32 else (2, 325, 9375, 28178, 450775, 9780504, 1795265022)
for i in check_list:
if i >= n:
break
if not suspect(i, d, n):
return False
return True
def get_prime(n):
sieve = [True] * (n + 1)
i = 2
while i * i <= n:
if sieve[i]:
for j in range(i * i, n + 1, i):
sieve[j] = False
i += 1
return [i for i in range(2, n + 1) if sieve[i]]
l = get_prime(4000)
# print(l)
q = int(input())
for _ in range(q):
n = int(input())
if n==1:
print('No')
continue
cnt=0
c=0
for i in range(len(l)):
c+=1
if n%l[i] == 0:
n = n//l[i]
cnt+=1
break
for i in range(len(l)-c):
c+=1
if n%l[i] == 0:
n = n//l[i]
cnt+=1
break
if miller_rabin(n):
cnt+=1
if cnt==3:
print('Yes')
else:
print('No')