結果
問題 |
No.732 3PrimeCounting
|
ユーザー |
|
提出日時 | 2017-03-20 19:32:46 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,643 bytes |
コンパイル時間 | 1,184 ms |
コンパイル使用メモリ | 113,364 KB |
実行使用メモリ | 14,056 KB |
最終ジャッジ日時 | 2024-07-05 03:30:02 |
合計ジャッジ時間 | 30,548 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 73 TLE * 1 -- * 15 |
ソースコード
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <unordered_set> #include <unordered_map> using namespace std; using ll = long long; const ll PMAX = 300000; //unordered_setを使って高速化できるのだろうか //O(π(N)×μ(N))≒10^8 //(μ(N)は1~Nまでの素数2つの和として表せる数の個数とする) int main(void) { int n; vector<int>prime; vector<bool>flag(PMAX); cin >> n; //primeにn以下のすべての素数が入る //flag[i-2]はiが素数の時false for (int x = 2; x <= PMAX; ++x) { if (flag[x - 2])continue; if (x <= n)prime.push_back(x); for (int y = x + x; y <= PMAX; y += x) { flag[y - 2] = true; } } vector<int>crush(n + n + 1); unordered_set<int>sum; for (int a : prime) { for (int b : prime) { sum.insert(a + b); } } //crush[x]:=N以下の素数pのうち、x+pも素数である個数。 //xが2つの素数の和で表せる場合のみを算出 for (int x : sum) { for (int p : prime) { if (!flag[p + x - 2]) { crush[x]++; } } } ll ans = 0; //a,bを総当たり for (int a : prime) { for (int b : prime) { //cとして考えられる数を総当たり ans += crush[a + b]; } } //2つの素数を含む組みを消す for (int a : prime) { ans -= crush[a + a] * 3; } cout << ans / 6 << endl; return 0; }