結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-10 21:25:31
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 495 ms / 1,000 ms
コード長 1,725 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 77,644 KB
実行使用メモリ 93,652 KB
最終ジャッジ日時 2023-08-21 22:38:18
合計ジャッジ時間 8,359 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
76,768 KB
testcase_01 AC 73 ms
76,396 KB
testcase_02 AC 72 ms
76,320 KB
testcase_03 AC 158 ms
80,240 KB
testcase_04 AC 74 ms
76,516 KB
testcase_05 AC 74 ms
76,484 KB
testcase_06 AC 74 ms
76,692 KB
testcase_07 AC 74 ms
76,816 KB
testcase_08 AC 73 ms
76,544 KB
testcase_09 AC 74 ms
76,688 KB
testcase_10 AC 72 ms
76,732 KB
testcase_11 AC 74 ms
76,184 KB
testcase_12 AC 75 ms
76,580 KB
testcase_13 AC 74 ms
76,508 KB
testcase_14 AC 79 ms
79,128 KB
testcase_15 AC 73 ms
76,408 KB
testcase_16 AC 73 ms
76,700 KB
testcase_17 AC 74 ms
76,880 KB
testcase_18 AC 72 ms
76,872 KB
testcase_19 AC 163 ms
81,564 KB
testcase_20 AC 241 ms
84,560 KB
testcase_21 AC 74 ms
76,708 KB
testcase_22 AC 75 ms
76,632 KB
testcase_23 AC 75 ms
76,852 KB
testcase_24 AC 75 ms
76,700 KB
testcase_25 AC 315 ms
86,004 KB
testcase_26 AC 75 ms
76,500 KB
testcase_27 AC 74 ms
76,204 KB
testcase_28 AC 75 ms
76,872 KB
testcase_29 AC 76 ms
76,816 KB
testcase_30 AC 76 ms
76,136 KB
testcase_31 AC 76 ms
76,816 KB
testcase_32 AC 219 ms
83,104 KB
testcase_33 AC 480 ms
93,484 KB
testcase_34 AC 495 ms
93,652 KB
testcase_35 AC 288 ms
85,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy
# -*- coding: utf-8 -*-

# 制約再変更後の想定解
# 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 = int(math.sqrt(end))
    is_prime = array.array("B", (False for _ in xrange(end + 1)))
    for x, y in itertools.product(xrange(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 xrange(5, sl + 1):
        if is_prime[n]:
            for k in xrange(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 xrange(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 = itertools.imap(int, raw_input().split())
    assert MIN_N <= n <= MAX_N
    assert MIN_L <= l <= MAX_L
    print(count_sequences(n, l))


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