結果

問題 No.713 素数の和
ユーザー kept1994kept1994
提出日時 2021-09-29 02:52:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,229 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 75,700 KB
最終ジャッジ日時 2023-09-22 23:45:36
合計ジャッジ時間 1,919 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
75,340 KB
testcase_01 AC 68 ms
71,252 KB
testcase_02 AC 74 ms
71,344 KB
testcase_03 AC 68 ms
71,144 KB
testcase_04 AC 71 ms
75,700 KB
testcase_05 AC 69 ms
71,524 KB
testcase_06 AC 68 ms
71,432 KB
testcase_07 AC 67 ms
71,376 KB
testcase_08 AC 69 ms
71,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
import bisect
class Eratosthenes():
    """ 素数列挙
    計算量 : O(NloglogN)
    """
    def __init__(self, N: int) -> None:
        self.isPrime = [True] * (N + 1) # 数iが素数かどうかのフラグ
        self.isPrime[0] = False
        self.isPrime[1] = False
        self.minfactor = [0] * (N + 1) # 数iの最小の素因数
        self.minfactor[1] = 1
        self.primes = []    # 数Nまでの素数のリスト
        for p in range(2, N + 1):  # p : 判定対象の数
            if not self.isPrime[p]:
                continue
            self.minfactor[p] = p
            self.primes.append(p)
            # pが素数のためそれ以降に出現するpの倍数を除外する。
            # なお、ループはp始まりでも良いが、p * _ のかける側はすでに同じ処理で弾かれているはずのため無駄。
            for i in range(p * p, N + 1, p):
                if self.minfactor[i] == 0:
                    self.minfactor[i] = p
                self.isPrime[i] = False
        return

def main():
    N = int(input())
    er = Eratosthenes(N)
    print(sum(er.primes))
    return
        
if __name__ == '__main__':
    main()
0