結果

問題 No.732 3PrimeCounting
ユーザー nebukuro09
提出日時 2018-09-07 22:44:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,389 bytes
コンパイル時間 1,993 ms
コンパイル使用メモリ 203,608 KB
最終ジャッジ日時 2025-01-06 13:02:35
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 WA * 66 TLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);i++)
#define REP2(i,m,n) for (int i=m;i<(n);i++)
typedef long long ll;
typedef long double ld;

const int MAX = 300300;
int N;
bool is_P[MAX];
vector<int> P;
vector<int> P1;
vector<int> P2;
map<int, ll> A;
map<int, ll> B;

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

    ll ans = 0;
    cin >> N;

    REP(i, 3*N+1) is_P[i] = true;
    is_P[0] = is_P[1] = false;
    for (int i = 2; i <= 3*N; ++i) {
        if (!is_P[i]) continue;
        if (i <= N) P.push_back(i);
        for (int j = i + i; j <=3*N; j += i) is_P[j] = false;
    }

    int M = P.size();

    REP2(i, 2, M) {
        if (P[i] % 3 == 1) P1.push_back(P[i]);
        if (P[i] % 3 == 2) P2.push_back(P[i]);
    }

    REP(i, P1.size()) REP2(j, i+1, P1.size()) A[P1[i]+P1[j]] += 1;
    REP(i, P2.size()) REP2(j, i+1, P2.size()) B[P2[i]+P2[j]] += 1;

    for (auto itr = A.begin(); itr != A.end(); ++itr) if (is_P[itr->first - 3]) ans += itr->second;
    for (auto itr = B.begin(); itr != B.end(); ++itr) if (is_P[itr->first - 3]) ans += itr->second;
    REP(i, P1.size()) for (auto itr = B.begin(); itr != B.end(); ++itr) if (is_P[P1[i] + itr->first]) ans += itr->second;
    REP(i, P2.size()) for (auto itr = A.begin(); itr != A.end(); ++itr) if (is_P[P2[i] + itr->first]) ans += itr->second;

    cout << ans << endl;
}
0