結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー shinichishinichi
提出日時 2021-08-27 21:37:57
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 698 ms / 2,000 ms
コード長 1,903 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,144 KB
実行使用メモリ 124,956 KB
最終ジャッジ日時 2023-08-13 08:16:58
合計ジャッジ時間 18,115 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 655 ms
124,452 KB
testcase_01 AC 663 ms
124,480 KB
testcase_02 AC 681 ms
124,408 KB
testcase_03 AC 667 ms
124,608 KB
testcase_04 AC 666 ms
124,188 KB
testcase_05 AC 666 ms
124,444 KB
testcase_06 AC 663 ms
124,556 KB
testcase_07 AC 666 ms
124,676 KB
testcase_08 AC 665 ms
124,700 KB
testcase_09 AC 664 ms
124,956 KB
testcase_10 AC 669 ms
124,692 KB
testcase_11 AC 663 ms
124,720 KB
testcase_12 AC 685 ms
124,648 KB
testcase_13 AC 678 ms
124,416 KB
testcase_14 AC 689 ms
124,652 KB
testcase_15 AC 679 ms
124,716 KB
testcase_16 AC 676 ms
124,728 KB
testcase_17 AC 677 ms
124,680 KB
testcase_18 AC 666 ms
124,920 KB
testcase_19 AC 674 ms
124,520 KB
testcase_20 AC 692 ms
124,788 KB
testcase_21 AC 698 ms
124,664 KB
testcase_22 AC 660 ms
124,488 KB
testcase_23 AC 679 ms
124,548 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
if er.isprime[2*R+1]:
    ans -= 1
print(ans)







0