結果

問題 No.843 Triple Primes
ユーザー mikianaaa
提出日時 2020-09-04 13:19:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,089 bytes
コンパイル時間 1,391 ms
コンパイル使用メモリ 171,324 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-25 08:21:18
合計ジャッジ時間 2,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    int MAX = N + 1;
    vector<int> is_prime(MAX, 1);
    vector<ll> primes, pows;
    is_prime[0] = 0, is_prime[1] = 0;
    for (int i = 2; i < MAX; ++i) {
        if (!is_prime[i])
            continue;
        for (int j = 2 * i; j < MAX; j += i)
            is_prime[j] = 0;
    }

    rep(i, MAX) {
        if (is_prime[i]) {
            primes.push_back(i);
            if ((ll)i * i <= N * 2)
                pows.push_back(i * i);
        }
    }

    int cnt = 0;
    rep(i, primes.size()) {
        rep(j, pows.size()) {
            if (primes[i] > pows[j] || pows[j] - primes[i] > N)
                continue;
            else if (is_prime[pows[j] - primes[i]])
                cnt++;
        }
    }

    cout << cnt << endl;
}
0