結果

問題 No.732 3PrimeCounting
ユーザー startcpp
提出日時 2017-01-21 22:57:31
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 931 bytes
コンパイル時間 885 ms
コンパイル使用メモリ 59,484 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 04:36:02
合計ジャッジ時間 15,199 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 89
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void setPrime()’:
main.cpp:24:31: warning: iteration 299999 invokes undefined behavior [-Waggressive-loop-optimizations]
   24 |                 if (is_prime[i]) primes[id++] = i;
      |                     ~~~~~~~~~~^
main.cpp:23:27: note: within this loop
   23 |         for (int i = 2; i <= 400000; i++) {
      |                         ~~^~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n;
bool is_prime[300001];
int primes[100000];
int cnt[300001];
int max_exist = -1;

void setPrime() {
	for (int i = 2; i <= 3 * n; i++) is_prime[i] = true;
	for (int i = 2; i <= 3 * n; i++) {
		if (is_prime[i]) {
			for (int j = i * 2; j <= 3 * n; j += i) {
				is_prime[j] = false;
			}
		}
	}
	
	int id = 0;
	for (int i = 2; i <= 400000; i++) {
		if (is_prime[i]) primes[id++] = i;
	}
}

int main() {
	cin >> n;
	setPrime();
	
	long long ans = 0;
	
	cnt[primes[0] + primes[1]]++;
	max_exist = max(max_exist, primes[0] + primes[1]);
	
	for (int i = 2; primes[i] <= n; i++) {
		for (int j = i + 1; primes[j] - primes[i] <= max_exist; j++) {
			ans += cnt[primes[j] - primes[i]];
		}
		for (int j = 0; j < i; j++) {
			cnt[primes[j] + primes[i]]++;
		}
		max_exist = max(max_exist, primes[i] + primes[i-1]);
	}
	cout << ans << endl;
	
	return 0;
}
0