結果

問題 No.843 Triple Primes
ユーザー ngtkana
提出日時 2020-03-12 21:24:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 708 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 193,340 KB
最終ジャッジ日時 2025-01-09 06:33:09
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define endl enjoy_codeforces
using lint=long long;
int main(){
    std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false);
    int n;std::cin>>n;
    if(n==1){
        std::cout<<0<<'\n';
        return 0;
    }
    std::vector<int>sieve(n+1);
    for(int p=2;p<=n/p;p++){
        if(sieve.at(p))continue;
        for(int i=p*p;i<=n;i+=p){
            sieve.at(i)=true;
        }
    }
    int ans=0;
    for(int r=2;r*r-2<=n;r++){
        ans+=!sieve.at(r)&&!sieve.at(r*r-2);
    }
    std::cout<<2*ans-1<<'\n';
}

/*
 * p か q のいずれかは 2 です。
 * p が 2 であるものをカウントして 2 倍して、
 * p = q = 2 の場合を引けばよいです。
 */
0