結果

問題 No.732 3PrimeCounting
ユーザー fine
提出日時 2018-09-22 19:02:55
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 539 ms / 3,000 ms
コード長 962 bytes
コンパイル時間 1,696 ms
コンパイル使用メモリ 175,880 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-07-18 10:43:11
合計ジャッジ時間 7,510 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

vector<int> sieve(int n, vector<bool>& is_prime) {
    vector<int> res;
    is_prime.assign(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (!is_prime[i]) continue;
        res.emplace_back(i);
        for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
    }
    return res;
}

ll solve(int n) {
    vector<bool> is_prime;
    vector<int> v = sieve(n * 3, is_prime);
    vector<ll> cnt(n * 3 + 1, 0);
    ll res = 0;
    for (int i = 0; i < v.size(); i++) {
        if (v[i] > n) break;

        for (int j = i + 1; j < v.size(); j++) {
            res += cnt[v[j] - v[i]];
        }

        for (int j = 0; j < i; j++) {
            cnt[v[i] + v[j]]++;
        }
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    cout << solve(n) << endl;
    return 0;
}
0