結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー shinichishinichi
提出日時 2021-08-27 21:35:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,868 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 123,904 KB
最終ジャッジ日時 2024-05-01 01:53:05
合計ジャッジ時間 20,308 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 725 ms
121,088 KB
testcase_01 AC 725 ms
120,960 KB
testcase_02 AC 756 ms
123,136 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 745 ms
121,344 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 739 ms
123,136 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 749 ms
123,596 KB
testcase_12 AC 785 ms
123,116 KB
testcase_13 AC 757 ms
123,648 KB
testcase_14 AC 766 ms
123,264 KB
testcase_15 AC 763 ms
123,264 KB
testcase_16 AC 754 ms
123,180 KB
testcase_17 AC 747 ms
123,280 KB
testcase_18 AC 756 ms
123,264 KB
testcase_19 AC 764 ms
123,092 KB
testcase_20 AC 768 ms
123,304 KB
testcase_21 AC 785 ms
123,552 KB
testcase_22 AC 760 ms
121,276 KB
testcase_23 AC 790 ms
123,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class Eratosthenes():
    def __init__(self, size):
        self.size = size
        self.isprime = [True] * size
        self.minfactor = [-1] * size
        self.mobius = [1] * size
        self.isprime[1] = False
        self.minfactor[1] = 1
        self.eratosthenes()

    # 初めに篩にかけて,minfactorとisprimeを生成
    def eratosthenes(self):
        for p in range(2, self.size):
            if not self.isprime[p]:
                continue
            self.minfactor[p] = p
            self.mobius[p] = -1
            for q in range(p+p, self.size, p):
                self.isprime[q] = False
                if self.minfactor[q] == -1:
                    self.minfactor[q] = p
                if (q//p) % p == 0:
                    self.mobius[q] = 0
                else:
                    self.mobius[q] = -self.mobius[q]

    # 高速素因数分解
    def prime_factorize(self, number):
        assert 1 <= number <= self.size
        factors = defaultdict(int)
        while number != 1:
            factors[self.minfactor[number]] += 1
            number //= self.minfactor[number]
        return factors

    # 高速約数列挙
    def divisors(self, number):
        res = [1]
        factors = self.prime_factorize(number)
        for p, cnt in factors.items():
            # 追加前の大きさを保存
            res_size = len(res)
            for i in range(res_size):
                pp = 1
                for j in range(cnt):
                    pp *= p
                    res.append(res[i]*pp)
        return res




L, R = map(int, input().split())
er = Eratosthenes(2*10**6+10)
ans = 0
for a in range(L, R+1):
    for b in range(a, a+2):
        if a != b and er.isprime[a+b]:
            ans += 1
        elif a == b and er.isprime[a]:
            ans += 1
print(ans)







0