結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-09 13:43:24
言語 PyPy3
(7.3.13)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,669 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 86,940 KB
実行使用メモリ 152,880 KB
最終ジャッジ日時 2023-08-03 13:35:59
合計ジャッジ時間 10,473 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,676 KB
testcase_01 AC 71 ms
71,772 KB
testcase_02 AC 70 ms
71,264 KB
testcase_03 AC 154 ms
79,392 KB
testcase_04 AC 71 ms
71,712 KB
testcase_05 AC 656 ms
76,304 KB
testcase_06 AC 341 ms
76,368 KB
testcase_07 AC 73 ms
71,764 KB
testcase_08 AC 72 ms
71,568 KB
testcase_09 AC 72 ms
71,708 KB
testcase_10 AC 71 ms
71,712 KB
testcase_11 AC 70 ms
71,424 KB
testcase_12 AC 72 ms
71,580 KB
testcase_13 AC 78 ms
76,276 KB
testcase_14 AC 83 ms
76,312 KB
testcase_15 AC 70 ms
71,324 KB
testcase_16 AC 76 ms
75,984 KB
testcase_17 AC 75 ms
75,908 KB
testcase_18 AC 76 ms
76,012 KB
testcase_19 AC 188 ms
84,916 KB
testcase_20 AC 377 ms
100,044 KB
testcase_21 AC 89 ms
76,324 KB
testcase_22 AC 91 ms
76,400 KB
testcase_23 AC 204 ms
76,336 KB
testcase_24 AC 175 ms
76,460 KB
testcase_25 AC 582 ms
118,444 KB
testcase_26 AC 306 ms
76,356 KB
testcase_27 AC 71 ms
71,276 KB
testcase_28 AC 163 ms
76,392 KB
testcase_29 AC 367 ms
76,228 KB
testcase_30 AC 90 ms
76,356 KB
testcase_31 AC 330 ms
76,632 KB
testcase_32 AC 482 ms
99,940 KB
testcase_33 TLE -
testcase_34 TLE -
testcase_35 AC 822 ms
113,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

# 制約変更後の想定解
# 以前の#101936はリジャッジでAssertionErrorとなるはず

import array
import itertools


MIN_N = 3
MAX_N = 10 ** 6
MIN_L = 1
MAX_L = 5 * 10 ** 6


# Eratosthenesの篩により、素数を列挙する
# is_prime[i] (0 <= i < end): iは素数か?
def sieve_of_eratosthenes(end):
    assert end > 1
    is_prime = array.array("B", (True for _ in range(end)))
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, end):
        if is_prime[i]:
            for j in range(2 * i, end, i):
                is_prime[j] = False
    return is_prime


# 素数計数関数π(x)の0 <= x <= lastに対する値をまとめた表を返す
# 上のsieve_of_eratosthenesとは区間のとり方が異なる
def pcf_table(last, typecode="L"):
    assert last >= 1
    is_prime = sieve_of_eratosthenes(last + 1)
    # 式 pcf[i] = pcf[i - 1] + f(i)により表を作る
    # ただし、f(i)はiが素数なら1、さもなければ0とする
    pcf = array.array(typecode, itertools.accumulate(is_prime))
    return pcf


# 等素数間隔列を数える
def count_seqs(n, l):
    # dの候補は1 <= d <= d_max(x0)
    def d_max(x0): return (l - x0) // (n - 1)
    # x0の候補は0 <= x0 <= x0_max
    x0_max = l - n + 1
    # 可能なx0が存在しなければ、答えは0
    if x0_max < 0:
        return 0
    pcf = pcf_table(max(1, d_max(0)))
    return sum(pcf[d_max(x0)] for x0 in range(0, x0_max + 1))


def main():
    n, l = map(int, input().split())
    assert MIN_N <= n <= MAX_N
    # assert MIN_L <= l <= MAX_L
    print(count_seqs(n, l))


if __name__ == '__main__':
    main()
0