結果

問題 No.843 Triple Primes
ユーザー tomarint2
提出日時 2019-06-28 21:58:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,234 bytes
コンパイル時間 724 ms
コンパイル使用メモリ 81,564 KB
実行使用メモリ 11,976 KB
最終ジャッジ日時 2024-07-02 04:42:12
合計ジャッジ時間 2,105 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
#define for1(i,n) for (ll i=0;i<(n);i++)
#define for2(i,m,n) for (ll i=(m);i<(n);i++)
#define for3(i,m,n,d) for (ll i=(m);i<(n);i+=(d))
#define DEBUG 0

void solve()
{
    ll N;
    cin >> N;
    char isPrime[N+1];
    for1(i,N+1) {
        isPrime[i] = 1;
    }
    isPrime[1] = 0;
    for2 (i,2,N+1) {
        if (!isPrime[i]) continue;
        for3(j,i*2,N+1,i) {
            isPrime[j] = 0;
        }
    }
    vector<ll> primes;
    for2 (i,2,N+1) {
        if (isPrime[i]) {
            primes.push_back(i);
        }
    }
    vector<ll> r2(N*2+1,0);
    for (auto p : primes) {
        ll r = p * p;
        if (r <= N*2) {
            r2[r] = 1;
        }
    }

    ll ans = 0;
    for (auto p : primes) {
        ll r = p + 2;
        //cout << p << " " << q << " " << r << endl;
        if (r2[r]) {
            ++ans;
        }
    }

    cout << ans*2-1 << endl;
}

int main()
{
    do {
        solve();
    } while (DEBUG);
}
0