結果
| 問題 | No.843 Triple Primes | 
| コンテスト | |
| ユーザー |  lam6er | 
| 提出日時 | 2025-03-20 21:12:04 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 53 ms / 2,000 ms | 
| コード長 | 796 bytes | 
| コンパイル時間 | 191 ms | 
| コンパイル使用メモリ | 82,228 KB | 
| 実行使用メモリ | 62,488 KB | 
| 最終ジャッジ日時 | 2025-03-20 21:13:28 | 
| 合計ジャッジ時間 | 3,469 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 42 | 
ソースコード
import math
def count_triple_primes(N):
    if N < 2:
        return 0
    # Sieve of Eratosthenes up to N
    is_prime = [True] * (N + 1)
    is_prime[0], is_prime[1] = False, False
    for i in range(2, int(math.isqrt(N)) + 1):
        if is_prime[i]:
            for j in range(i * i, N + 1, i):
                is_prime[j] = False
    count = 0
    # Check r=2 case
    if N >= 2 and 4 <= 2 * N:
        count += 1
    # Check odd primes r where r^2 <= N + 2
    max_r = math.isqrt(N + 2)
    for r in range(3, max_r + 1, 2):
        if is_prime[r]:
            r_squared = r * r
            q = r_squared - 2
            if q >= 2 and q <= N and is_prime[q]:
                count += 2
    return count
# Read input and output the result
N = int(input())
print(count_triple_primes(N))
            
            
            
        