結果

問題 No.36 素数が嫌い!
ユーザー Mcpu3
提出日時 2019-03-16 13:33:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 143 ms / 5,000 ms
コード長 854 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 69,760 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-01 22:28:36
合計ジャッジ時間 3,430 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//	vector
struct theSieveOfEratosThenes {
	vector<bool> prm;

	theSieveOfEratosThenes(int n) {
		prm = vector<bool>(n + 1, true);
		prm[0] = prm[1] = false;
		for (int i = 2; i * i <= n; ++i) {
			if (prm[i]) {
				for (int j = 2 * i; j <= n; j += i) prm[j] = false;
			}
		}
	}

	bool isPrime(int a) {
		return prm[a];
	}
};

template<typename T>
bool trialDivision(T a) {
	if (a == 1) return false;
	T i;
	for (i = 2; i * i <= a; ++i) {
		if (a % i == 0) break;
	}
	if (a < i * i) return true;
	return false;
}

int main() {
	long N, i;
	cin >> N;
	theSieveOfEratosThenes tsoet(10000000);
	for (i = 2; i * i <= N; ++i) {
		if (N % i == 0 && (!tsoet.isPrime(i) || !trialDivision(N / i))) break;
	}
	if (N < i * i) cout << "NO";
	else cout << "YES";
}
0