結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹
提出日時 2016-07-09 13:43:24
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,669 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 151,052 KB
最終ジャッジ日時 2024-10-13 10:19:06
合計ジャッジ時間 9,849 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 29 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

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