結果

問題 No.732 3PrimeCounting
ユーザー platypus999
提出日時 2017-03-20 20:01:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,979 ms / 3,000 ms
コード長 1,356 bytes
コンパイル時間 1,126 ms
コンパイル使用メモリ 107,308 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-05 03:32:13
合計ジャッジ時間 32,914 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 89
権限があれば一括ダウンロードができます

ソースコード

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;

//たーのしー!
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);
	ll ans = 0;
	//crush[x]:=N以下の素数pのうち、x+pも素数である個数。
	for (int x = 0; x <= n + n; ++x)
	{
		if (x % 2)continue;
		for (int p : prime)
		{
			crush[x] += !flag[p + x - 2];
		}
	}
	//a,bを総当たり
	for (int a : prime)
	{
		if (a == 2)continue;
		for (int b : prime)
		{
			if (b == 2)continue;
			if (a < b)break;
			//いろいろ定数倍を落とすために頑張る
			ans += crush[a + b] * ((a != b) ? 2 : -2);
		}
	}
	cout << ans / 6 << endl;
	return 0;
}
0