結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー NoneNone
提出日時 2021-09-30 05:07:34
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 3,336 bytes
コンパイル時間 1,620 ms
コンパイル使用メモリ 86,868 KB
実行使用メモリ 100,504 KB
最終ジャッジ日時 2023-09-24 07:44:23
合計ジャッジ時間 4,808 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,660 KB
testcase_01 AC 74 ms
71,684 KB
testcase_02 AC 139 ms
99,304 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 73 ms
71,700 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 80 ms
75,908 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 86 ms
76,732 KB
testcase_12 AC 138 ms
99,380 KB
testcase_13 AC 109 ms
88,636 KB
testcase_14 AC 123 ms
96,156 KB
testcase_15 AC 128 ms
96,712 KB
testcase_16 AC 102 ms
85,192 KB
testcase_17 AC 104 ms
86,720 KB
testcase_18 AC 106 ms
87,468 KB
testcase_19 AC 141 ms
97,468 KB
testcase_20 AC 113 ms
91,212 KB
testcase_21 AC 139 ms
100,504 KB
testcase_22 AC 122 ms
100,104 KB
testcase_23 AC 135 ms
100,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
約数の個数
    10^6以下: 240個
    10^9以下: 1344個
    10^12以下: 6720個
    10^18以下: 103680個

    ⇒1000個の10^9以下の数字の約数の全列挙が可能

"""


class PrimeFactor():

    def __init__(self, n):
        """
        エラトステネス O(N loglog N)
        """
        self.n = n
        self.table = list(range(n+1))       # 最小素因数のリスト
        self.table[2::2] = [2]*(n//2)
        for p in range(3, int(n**0.5) + 2, 2):
            if self.table[p] == p:
                for q in range(p * p, n + 1, 2 * p):
                    if self.table[q] == q:
                        self.table[q] = p

    def is_prime(self, x):
        """ 素数判定 O(1) """
        if x < 2:
            return False
        return self.table[x] == x

    def prime_factors(self, x):
        """ 素因数分解 O(logN)  (試し割りだとO(sqrt(N))) """
        res = []
        if x < 2:
            return res
        while self.table[x] != 1:
            res.append(self.table[x])
            x //= self.table[x]
        return res

    def divisors(self, x):
        """ 約数列挙 x=[1,10**6]の約数全列挙も間に合う """
        primes=self.prime_counter(x)
        P=set([1])
        for key, value in primes.items():
            Q=[]
            for p in P:
                for k in range(value+1):
                    Q.append(p*pow(key,k))
            P|=set(Q)
        P = list(P)
        P.sort()
        return P

    def prime_counter(self, x):
        """
        素因数分解(個数のリスト) O(logN)
        {素因数: 個数} の形で返す
        """
        res = dict()
        if x < 2:
            return res
        while self.table[x] != 1:
            res[self.table[x]] = res.get(self.table[x], 0) + 1
            x //= self.table[x]
        return res

    def divisors_counter(self, x):
        """ 約数の個数 O((logN)^2) """
        res = 1
        for value in self.prime_counter(x).values():
            res *= (value+1)
        return res

    def prime_gcd(self,X,MOD=10**9+7):
        """ n個の最大公約数 X:n個のリスト  ( O(|X|*(log X_max)^2) ) """
        exponents = self.prime_counter(X[0])
        for x in X[1:]:
            Y = self.prime_counter(x)
            for prime, exp in exponents.items():
                if Y[prime] < exp:
                    exponents[prime] = Y[prime]
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime,exp,MOD)
            res %= MOD
        return res

    def prime_lcm(self,X,MOD=10**9+7):
        """ n個の最小公倍数 X:n個のリスト  ( O(|X|*(log X_max)^2) )  """
        exponents = dict()
        for x in X:
            for prime, exp in self.prime_counter(x).items():
                if exp > exponents.get(prime, 0):
                    exponents[prime] = exp
        res = 1
        for prime, exp in exponents.items():
            res *= pow(prime,exp,MOD)
            res %= MOD
        return res


#####################################################################################################
import sys
input = sys.stdin.readline

L,R=map(int, input().split())
PF = PrimeFactor(2*R+1)

res=0
for n in range(L,R+1):
    if PF.is_prime(n):
        res+=1
    if PF.is_prime(2*n+1):
        res+=1
print(res)
0