結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-10 21:16:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 593 ms / 1,000 ms
コード長 1,689 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 88,244 KB
最終ジャッジ日時 2024-05-09 04:38:54
合計ジャッジ時間 5,040 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,248 KB
testcase_01 AC 40 ms
52,736 KB
testcase_02 AC 38 ms
52,480 KB
testcase_03 AC 114 ms
77,748 KB
testcase_04 AC 38 ms
52,352 KB
testcase_05 AC 39 ms
52,864 KB
testcase_06 AC 40 ms
53,248 KB
testcase_07 AC 43 ms
52,864 KB
testcase_08 AC 40 ms
52,352 KB
testcase_09 AC 39 ms
52,608 KB
testcase_10 AC 38 ms
53,252 KB
testcase_11 AC 38 ms
52,480 KB
testcase_12 AC 38 ms
52,224 KB
testcase_13 AC 38 ms
52,608 KB
testcase_14 AC 43 ms
59,520 KB
testcase_15 AC 37 ms
52,480 KB
testcase_16 AC 38 ms
52,992 KB
testcase_17 AC 39 ms
52,992 KB
testcase_18 AC 38 ms
52,864 KB
testcase_19 AC 141 ms
77,964 KB
testcase_20 AC 237 ms
80,844 KB
testcase_21 AC 39 ms
52,600 KB
testcase_22 AC 38 ms
52,736 KB
testcase_23 AC 39 ms
52,608 KB
testcase_24 AC 39 ms
53,120 KB
testcase_25 AC 339 ms
83,216 KB
testcase_26 AC 41 ms
52,992 KB
testcase_27 AC 37 ms
52,480 KB
testcase_28 AC 38 ms
53,248 KB
testcase_29 AC 38 ms
52,864 KB
testcase_30 AC 37 ms
52,864 KB
testcase_31 AC 38 ms
53,120 KB
testcase_32 AC 210 ms
80,276 KB
testcase_33 AC 587 ms
88,244 KB
testcase_34 AC 593 ms
87,964 KB
testcase_35 AC 314 ms
81,936 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