結果

問題 No.732 3PrimeCounting
ユーザー るこーそー
提出日時 2024-08-24 01:11:25
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 566 ms / 3,000 ms
コード長 1,578 bytes
コンパイル時間 5,463 ms
コンパイル使用メモリ 310,608 KB
実行使用メモリ 12,376 KB
最終ジャッジ日時 2024-08-24 01:11:39
合計ジャッジ時間 14,312 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define vvvi vector<vvi>
#define vvvl vector<vvl>

template <class T>
auto debug(const T &vec){
    if constexpr (!std::is_arithmetic_v<typename T::value_type>){
        for (const auto &v : vec)debug<typename T::value_type>(v);
        cout << endl;}
    else{
        for (const auto &e : vec){
            cout << e << " ";}
        cout << endl;}
}

void print(){cout << '\n';}
template <class T, class... Ts>
void print(const T &a, const Ts &...b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';}

vector<long long> eratostenes(int n){
    vector<long long> vis(n+1,0);
    vector<long long> res;
    for (int i=2;i<n+1;i++){
        if (vis[i])continue;
        res.push_back(i);
        for (int j=i;j<n+1;j+=i){
            vis[j]=1;
        }
    }
    return res;
}

int main() {
    int n;
    cin >> n;

    vl prime=eratostenes(1000000);
    vl cnt(1000000,0);
    ll ans=0;
    for (int c=2;prime[c]<=n;c++){
        int b=c-1;
        for (int a=0;a<b;a++){
            cnt[prime[a]+prime[b]]++;
        }

        for (int d=c+1;prime[d]<=3*n;d++){
            ans+=cnt[prime[d]-prime[c]];
        }
    }
    cout << ans << endl;
}
0