結果

問題 No.843 Triple Primes
ユーザー GrayCoderGrayCoder
提出日時 2019-06-29 20:16:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 758 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 11,976 KB
実行使用メモリ 20,948 KB
最終ジャッジ日時 2023-10-19 17:56:08
合計ジャッジ時間 4,887 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,112 KB
testcase_01 AC 123 ms
20,948 KB
testcase_02 AC 30 ms
10,272 KB
testcase_03 AC 30 ms
10,260 KB
testcase_04 AC 31 ms
10,388 KB
testcase_05 AC 29 ms
10,264 KB
testcase_06 AC 30 ms
10,404 KB
testcase_07 AC 100 ms
19,968 KB
testcase_08 AC 106 ms
20,220 KB
testcase_09 AC 118 ms
20,916 KB
testcase_10 AC 103 ms
19,984 KB
testcase_11 AC 111 ms
20,500 KB
testcase_12 AC 115 ms
20,800 KB
testcase_13 AC 113 ms
20,648 KB
testcase_14 AC 111 ms
20,588 KB
testcase_15 AC 101 ms
20,040 KB
testcase_16 AC 101 ms
19,952 KB
testcase_17 AC 29 ms
10,020 KB
testcase_18 AC 29 ms
10,112 KB
testcase_19 AC 28 ms
10,052 KB
testcase_20 AC 51 ms
14,364 KB
testcase_21 AC 40 ms
12,092 KB
testcase_22 AC 78 ms
18,520 KB
testcase_23 AC 79 ms
18,796 KB
testcase_24 AC 52 ms
14,452 KB
testcase_25 AC 46 ms
12,608 KB
testcase_26 AC 121 ms
20,904 KB
testcase_27 AC 34 ms
11,036 KB
testcase_28 AC 115 ms
20,700 KB
testcase_29 AC 54 ms
14,568 KB
testcase_30 AC 118 ms
20,824 KB
testcase_31 AC 36 ms
11,252 KB
testcase_32 AC 33 ms
10,744 KB
testcase_33 AC 57 ms
14,756 KB
testcase_34 AC 75 ms
18,272 KB
testcase_35 AC 119 ms
20,920 KB
testcase_36 AC 45 ms
12,272 KB
testcase_37 AC 98 ms
19,800 KB
testcase_38 AC 86 ms
19,196 KB
testcase_39 AC 114 ms
20,748 KB
testcase_40 AC 29 ms
10,112 KB
testcase_41 AC 29 ms
10,020 KB
testcase_42 AC 103 ms
20,092 KB
testcase_43 AC 109 ms
20,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from sys import stdin

def erastosthenes(n):
    prime = [0, 1] * ((n + 1) // 2)
    if not n % 2:
        prime += [0]
    prime[1], prime[2] = 0, 1

    i = 3
    while i <= n ** 0.5:
        for j in range(i * i, n + 1, i):
            prime[j] = 0
        i += 2
    return prime

def main():
    N = int(input())

    if N < 2:
        print(0)
        return

    lst = erastosthenes(N)
    primes = [2] + [i for i in range(3, N + 1, 2) if lst[i]]
    r2 = defaultdict(int)
    for i in primes:
        r2[i*i] = 1

    ans = 0
    for i in primes:
        if r2[2+i]:
            if i == 2:
                ans += 1
            else:
                ans += 2
    print(ans)

input = lambda: stdin.readline()
main()
0