結果

問題 No.843 Triple Primes
ユーザー vwxyzvwxyz
提出日時 2022-07-19 17:28:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 212 ms / 2,000 ms
コード長 2,498 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 15,556 KB
最終ジャッジ日時 2023-09-14 13:22:34
合計ジャッジ時間 7,400 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,096 KB
testcase_01 AC 212 ms
15,556 KB
testcase_02 AC 19 ms
8,712 KB
testcase_03 AC 18 ms
8,676 KB
testcase_04 AC 20 ms
8,796 KB
testcase_05 AC 19 ms
8,656 KB
testcase_06 AC 20 ms
8,764 KB
testcase_07 AC 171 ms
14,228 KB
testcase_08 AC 181 ms
14,700 KB
testcase_09 AC 204 ms
15,544 KB
testcase_10 AC 176 ms
14,400 KB
testcase_11 AC 187 ms
15,000 KB
testcase_12 AC 203 ms
15,472 KB
testcase_13 AC 199 ms
15,148 KB
testcase_14 AC 200 ms
15,200 KB
testcase_15 AC 175 ms
14,424 KB
testcase_16 AC 172 ms
14,496 KB
testcase_17 AC 16 ms
8,052 KB
testcase_18 AC 16 ms
8,148 KB
testcase_19 AC 17 ms
8,160 KB
testcase_20 AC 67 ms
10,260 KB
testcase_21 AC 44 ms
9,496 KB
testcase_22 AC 120 ms
12,536 KB
testcase_23 AC 126 ms
12,672 KB
testcase_24 AC 70 ms
10,476 KB
testcase_25 AC 57 ms
10,152 KB
testcase_26 AC 204 ms
15,480 KB
testcase_27 AC 27 ms
9,060 KB
testcase_28 AC 197 ms
15,260 KB
testcase_29 AC 72 ms
10,524 KB
testcase_30 AC 206 ms
15,456 KB
testcase_31 AC 34 ms
9,232 KB
testcase_32 AC 26 ms
9,068 KB
testcase_33 AC 79 ms
10,784 KB
testcase_34 AC 110 ms
12,288 KB
testcase_35 AC 205 ms
15,484 KB
testcase_36 AC 52 ms
9,800 KB
testcase_37 AC 165 ms
14,056 KB
testcase_38 AC 142 ms
13,212 KB
testcase_39 AC 203 ms
15,364 KB
testcase_40 AC 17 ms
8,064 KB
testcase_41 AC 17 ms
8,032 KB
testcase_42 AC 182 ms
14,604 KB
testcase_43 AC 193 ms
15,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            A=[1]
            for _ in range(e):
                A.append(A[-1]*p)
            divisors=[i*j for i in divisors for j in A]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

N=int(readline())
P=Prime(N)
ans=0
if N>=2:
    ans+=1
for r in range(3,N+1):
    if P.Is_Prime(r) and r**2-2<=N and P.Is_Prime(r**2-2):
        ans+=2
print(ans)
0