結果

問題 No.36 素数が嫌い!
ユーザー pekempeypekempey
提出日時 2016-02-10 02:58:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,861 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 160,988 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 20:38:07
合計ジャッジ時間 8,283 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
typedef long long ll;

long long modmul(long long a, long long b, long long mod) {
	long long ret = 0;
	for (; b > 0; a = a * 2 % mod, b /= 2) if (b & 1) ret = (ret + a) % mod;
	return ret;
}

long long modpow(long long a, long long b, long long mod) {
	long long ret = 1;
	for (; b > 0; a = modmul(a, a, mod), b /= 2) if (b & 1) ret = modmul(ret, a, mod);
	return ret;
}

bool isPrime(long long n) {
	if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
	const int bases[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
	int d = n - 1, s = 0;
	while (d % 2 == 0) s++, d /= 2;
	auto suspect = [&](ll a) {
		long long x = modpow(a, d, n);
		if (x == 1) return true;
		for (int r = 0; r < s; r++, x = modmul(x, x, n)) if (x == n - 1) return true;
		return false;
	};
	for (int base : bases) if (base < n && !suspect(base)) return false; 
	return true;
}

long long rho(long long n) {
	for (long long c = 1; c < n; c++) if (c != n - 2) {
		long long x = 2, y = 2, d = 1;
		auto f = [&](long long x) { return (modmul(x, x, n) + c) % n; };
		while (d == 1) {
			x = f(x);
			y = f(f(y));
			d = __gcd(abs(x - y), n);
		}
		if (d != n) return d;
	}
	return -1;
}

bool solve() {
	ll n;
	cin >> n;
	if (n == 1) return false;
	if (isPrime(n)) return false;
	
	ll r = rho(n);
	if (r == -1) return false;
	if (isPrime(r)) return !isPrime(n / r);

	return true;
}

int main() {
	cout << (solve() ? "YES" : "NO") << endl;
	return 0;
}
0