結果

問題 No.843 Triple Primes
ユーザー rpy3cpp
提出日時 2019-09-09 00:08:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 903 bytes
コンパイル時間 1,825 ms
コンパイル使用メモリ 168,528 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 15:19:01
合計ジャッジ時間 3,586 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    if (N == 1){
        cout << 0 << endl;
        return 0;
    }else if (N <= 6){
        cout << 1 << endl;
        return 0;
    }
    vector<bool> isPrime (N + 1, true);
    for (int p = 5, d = 2; p * p <= N; p += d, d = 6 - d){
        if (isPrime[p]){
            for (int q = p * p, delta = d; q <= N; q += delta * p, delta = 6 - delta){
                isPrime[q] = false;
            }
        }
    }
    isPrime[0] = false;
    isPrime[1] = false;
    isPrime[4] = false;
    int count = 3;  // (2, 2, 2), (7, 2, 3), (2, 7, 3)
    for (int r = 5, d = 2; r * r <= N + 2; r += d, d = 6 - d){
        if (isPrime[r]){
            int tmp = r * r - 2;
            int res = tmp % 6;
            if (isPrime[tmp] and (res == 1 or res == 5)) count += 2;
        }
    }
    cout << count << endl;
    return 0;
}
0