結果

問題 No.732 3PrimeCounting
ユーザー platypus999
提出日時 2017-03-20 19:27:15
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,383 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 107,164 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-07-05 03:29:20
合計ジャッジ時間 33,150 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 73 TLE * 1 -- * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;

//想定解法ではなさそう
//O(π(N)×N)≒10^9
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);
	//crush[x]:=N以下の素数pのうち、x+pも素数である個数。
	for (int x = 0; x <= n + n; ++x)
	{
		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;
}
0