結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-08 20:38:02
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,567 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 99,272 KB
最終ジャッジ日時 2024-04-21 11:50:38
合計ジャッジ時間 4,156 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,224 KB
testcase_01 AC 37 ms
52,096 KB
testcase_02 AC 37 ms
51,712 KB
testcase_03 AC 159 ms
77,776 KB
testcase_04 AC 38 ms
52,736 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 38 ms
52,224 KB
testcase_08 AC 38 ms
52,096 KB
testcase_09 AC 40 ms
52,096 KB
testcase_10 AC 40 ms
52,608 KB
testcase_11 AC 38 ms
52,096 KB
testcase_12 AC 38 ms
51,712 KB
testcase_13 AC 44 ms
60,416 KB
testcase_14 AC 59 ms
71,296 KB
testcase_15 AC 38 ms
51,712 KB
testcase_16 AC 46 ms
59,904 KB
testcase_17 AC 46 ms
58,880 KB
testcase_18 AC 46 ms
59,264 KB
testcase_19 AC 171 ms
84,692 KB
testcase_20 AC 393 ms
99,272 KB
testcase_21 AC 62 ms
72,320 KB
testcase_22 AC 72 ms
75,136 KB
testcase_23 AC 192 ms
75,264 KB
testcase_24 AC 155 ms
75,392 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 39 ms
52,096 KB
testcase_28 AC 137 ms
75,392 KB
testcase_29 RE -
testcase_30 AC 63 ms
72,704 KB
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3

import array
import itertools


MIN_N = 3
MAX_N = 10 ** 6
MIN_L = 1
MAX_L = 3 * 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