結果
| 問題 |
No.371 ぼく悪いプライムじゃないよ
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-31 17:54:02 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 252 ms / 1,000 ms |
| コード長 | 2,399 bytes |
| コンパイル時間 | 286 ms |
| コンパイル使用メモリ | 82,608 KB |
| 実行使用メモリ | 81,084 KB |
| 最終ジャッジ日時 | 2025-03-31 17:55:53 |
| 合計ジャッジ時間 | 6,703 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 42 |
ソースコード
import sys
import math
import bisect
def sieve(n):
is_prime = [True] * (n + 1)
is_prime[0], is_prime[1] = False, False
for i in range(2, int(math.isqrt(n)) + 1):
if is_prime[i]:
for j in range(i * i, n + 1, i):
is_prime[j] = False
return [i for i, val in enumerate(is_prime) if val]
def is_prime_miller(n):
if n < 2:
return False
small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
for p in small_primes:
if n % p == 0:
return n == p
d = n - 1
s = 0
while d % 2 == 0:
d //= 2
s += 1
for a in small_primes:
if a >= n:
continue
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(s - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True
def main():
L, H = map(int, sys.stdin.readline().split())
max_p = int(math.isqrt(H))
primes = sieve(max_p)
candidates = {}
for p in primes:
if p * p > H:
continue
q_max = H // p
k_min = max(p, (L + p - 1) // p)
if q_max < k_min:
continue
idx = bisect.bisect_left(primes, p)
primes_below = primes[:idx]
found = -1
for k in range(q_max, k_min - 1, -1):
x = p * k
if is_prime_miller(x):
continue # x is prime, skip
if is_prime_miller(k):
if k >= p:
found = x
break
else:
continue
valid = True
for q in primes_below:
if k % q == 0:
valid = False
break
if valid:
found = x
break
if found != -1:
candidates[found] = p
if not candidates:
for x in range(H, L - 1, -1):
if not is_prime_miller(x) and x >= 4:
print(x)
return
print(-1)
else:
max_spf = -1
ans = -1
for x in candidates:
spf = candidates[x]
if spf > max_spf or (spf == max_spf and x > ans):
max_spf = spf
ans = x
print(ans)
if __name__ == "__main__":
main()
lam6er