結果

問題 No.1657 Sum is Prime (Easy Version)
ユーザー kuc_jacksonkuc_jackson
提出日時 2021-08-27 21:32:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 649 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 197,716 KB
最終ジャッジ日時 2025-01-24 02:37:24
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<bool> IsPrime;

void Sieve(int max){
    if(max + 1 > IsPrime.size()){
        IsPrime.assign(max + 1, true);
    }
    IsPrime[0] = false;
    IsPrime[1] = false;
    for(int i = 2; i * i <= max; i++){
        if(IsPrime[i]){
            for(int j = 2; i * j <= max; j++){
                IsPrime[i * j] = false;
            }
        }
    }
    return;
}

int main(){
    int L, R; cin >> L >> R;
    ll ans = 0;
    Sieve(3 * R);
    for(int a = L; a <= R; a++){
        if(IsPrime[a])++ans;
        if(a < R && IsPrime[2 * a + 1])++ans;
    }
    cout << ans << endl;
}
0