結果
| 問題 |
No.407 鴨等素数間隔列の数え上げ
|
| コンテスト | |
| ユーザー |
はむ吉🐹
|
| 提出日時 | 2016-07-10 21:16:37 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 509 ms / 1,000 ms |
| コード長 | 1,689 bytes |
| コンパイル時間 | 179 ms |
| コンパイル使用メモリ | 82,580 KB |
| 実行使用メモリ | 88,224 KB |
| 最終ジャッジ日時 | 2024-12-15 14:10:10 |
| 合計ジャッジ時間 | 4,527 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 31 |
ソースコード
#!/usr/bin/env pypy3
# 制約再変更後の想定解
# Eratosthenesの篩を用いたPyPy3解がTLEになったため、
# Atkinの篩に切り替えた
import array
import itertools
import math
MIN_N = 3
MAX_N = 10 ** 6
MIN_L = 1
MAX_L = 2 * 10 ** 7
# Atkinの篩により、end未満の素数を列挙する
# この実装では計算量O(end)
# http://www.prefield.com/algorithm/math/sieve_of_atkin.html
def sieve_of_atkin(end, typecode="L"):
sl = math.floor(math.sqrt(end))
is_prime = array.array("B", (False for _ in range(end + 1)))
for x, y in itertools.product(range(1, sl + 1), repeat=2):
n = 4 * x * x + y * y
if n <= end and (n % 12 == 1 or n % 12 == 5):
is_prime[n] ^= True
n = 3 * x * x + y * y
if n <= end and n % 12 == 7:
is_prime[n] ^= True
n = 3 * x * x - y * y
if x > y and n <= end and n % 12 == 11:
is_prime[n] ^= True
for n in range(5, sl + 1):
if is_prime[n]:
for k in range(n * n, end, n * n):
is_prime[k] = False
if end > 2:
is_prime[2] = True
if end > 3:
is_prime[3] = True
primes = array.array(typecode)
primes.extend(p for p in range(end) if is_prime[p])
return primes
def count_sequences(n, l):
def x_max(d):
return l - (n - 1) * d
d_max = l // (n - 1)
if d_max < 2:
return 0
ds = sieve_of_atkin(d_max + 1)
ans = sum(x_max(d) + 1 for d in ds)
return ans
def main():
n, l = map(int, input().split())
assert MIN_N <= n <= MAX_N
assert MIN_L <= l <= MAX_L
print(count_sequences(n, l))
if __name__ == '__main__':
main()
はむ吉🐹