結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-10 21:16:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 608 ms / 1,000 ms
コード長 1,689 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 90,496 KB
最終ジャッジ日時 2023-08-21 22:27:11
合計ジャッジ時間 6,435 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,656 KB
testcase_01 AC 71 ms
71,684 KB
testcase_02 AC 71 ms
71,428 KB
testcase_03 AC 142 ms
77,892 KB
testcase_04 AC 73 ms
71,232 KB
testcase_05 AC 71 ms
71,336 KB
testcase_06 AC 73 ms
71,436 KB
testcase_07 AC 73 ms
71,512 KB
testcase_08 AC 72 ms
71,156 KB
testcase_09 AC 70 ms
71,488 KB
testcase_10 AC 72 ms
71,576 KB
testcase_11 AC 70 ms
71,464 KB
testcase_12 AC 71 ms
71,136 KB
testcase_13 AC 71 ms
71,560 KB
testcase_14 AC 77 ms
76,188 KB
testcase_15 AC 70 ms
71,344 KB
testcase_16 AC 71 ms
71,492 KB
testcase_17 AC 71 ms
71,704 KB
testcase_18 AC 71 ms
71,676 KB
testcase_19 AC 168 ms
79,184 KB
testcase_20 AC 261 ms
81,828 KB
testcase_21 AC 70 ms
71,164 KB
testcase_22 AC 72 ms
71,232 KB
testcase_23 AC 72 ms
71,748 KB
testcase_24 AC 71 ms
71,440 KB
testcase_25 AC 357 ms
84,684 KB
testcase_26 AC 72 ms
71,612 KB
testcase_27 AC 71 ms
71,316 KB
testcase_28 AC 72 ms
71,564 KB
testcase_29 AC 72 ms
71,500 KB
testcase_30 AC 73 ms
71,328 KB
testcase_31 AC 73 ms
71,620 KB
testcase_32 AC 238 ms
80,752 KB
testcase_33 AC 595 ms
90,496 KB
testcase_34 AC 608 ms
89,764 KB
testcase_35 AC 330 ms
83,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/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()
0