結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-09 13:43:24
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,669 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 150,804 KB
最終ジャッジ日時 2024-04-21 11:50:49
合計ジャッジ時間 9,477 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
51,840 KB
testcase_01 AC 37 ms
52,096 KB
testcase_02 AC 37 ms
51,712 KB
testcase_03 AC 141 ms
77,640 KB
testcase_04 AC 37 ms
51,968 KB
testcase_05 AC 633 ms
75,264 KB
testcase_06 AC 309 ms
75,008 KB
testcase_07 AC 38 ms
52,608 KB
testcase_08 AC 37 ms
52,224 KB
testcase_09 AC 38 ms
52,224 KB
testcase_10 AC 38 ms
52,352 KB
testcase_11 AC 37 ms
51,840 KB
testcase_12 AC 37 ms
51,840 KB
testcase_13 AC 44 ms
60,288 KB
testcase_14 AC 58 ms
71,424 KB
testcase_15 AC 36 ms
51,968 KB
testcase_16 AC 44 ms
59,264 KB
testcase_17 AC 43 ms
58,752 KB
testcase_18 AC 46 ms
59,520 KB
testcase_19 AC 165 ms
84,820 KB
testcase_20 AC 377 ms
98,760 KB
testcase_21 AC 64 ms
71,936 KB
testcase_22 AC 69 ms
75,136 KB
testcase_23 AC 179 ms
75,008 KB
testcase_24 AC 149 ms
75,648 KB
testcase_25 AC 614 ms
117,132 KB
testcase_26 AC 278 ms
75,392 KB
testcase_27 AC 38 ms
51,584 KB
testcase_28 AC 134 ms
75,392 KB
testcase_29 AC 338 ms
75,264 KB
testcase_30 AC 62 ms
72,576 KB
testcase_31 AC 302 ms
75,264 KB
testcase_32 AC 506 ms
96,160 KB
testcase_33 TLE -
testcase_34 TLE -
testcase_35 AC 868 ms
112,556 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