結果

問題 No.774 tatyamと素数大富豪
ユーザー square1001square1001
提出日時 2019-03-27 14:24:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,517 ms / 2,000 ms
コード長 1,872 bytes
コンパイル時間 1,114 ms
コンパイル使用メモリ 89,212 KB
実行使用メモリ 7,992 KB
最終ジャッジ日時 2024-04-19 09:55:27
合計ジャッジ時間 6,169 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
7,992 KB
testcase_01 AC 4 ms
5,376 KB
testcase_02 AC 4 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 866 ms
5,376 KB
testcase_11 AC 713 ms
5,376 KB
testcase_12 AC 51 ms
5,820 KB
testcase_13 AC 57 ms
5,948 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 777 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 1,517 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int lim = 2 * 3 * 5 * 7 * 11 * 13 * 17; // = 510510
vector<int> primes;
bool isprime(long long x) {
	if (x == 1) return false;
	if (x == 2) return true;
	if (x % 2 == 0) return false;
	if (x == 3) return true;
	if (x % 3 == 0) return false;
	if (x == 5) return true;
	if (x % 5 == 0) return false;
	if (x == 7) return true;
	if (x % 7 == 0) return false;
	if (x == 11) return true;
	if (x % 11 == 0) return false;
	if (x == 13) return true;
	if (x % 13 == 0) return false;
	if (x == 17) return true;
	if (x % 17 == 0) return false;
	bool finish = false;
	for (int i = 0; !finish; i += lim) {
		for (int j : primes) {
			long long t = i + j;
			if (t * t > x) {
				finish = true;
				break;
			}
			if (x % t == 0) return false;
		}
	}
	return true;
}
void initialize() {
	for (int i = 2; i <= lim + 1; ++i) {
		if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0 && i % 11 != 0 && i % 13 != 0 && i % 17 != 0) {
			primes.push_back(i);
		}
	}
}
int main() {
	initialize();
	int N;
	cin >> N;
	vector<int> A(N);
	int sum = 0;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		sum += A[i];
	}
	if (sum % 3 == 0) {
		if (N == 1 && sum == 3) cout << 3 << endl;
		else cout << -1 << endl;
	}
	else {
		vector<long long> S;
		vector<int> perm(N);
		for (int i = 0; i < N; ++i) {
			perm[i] = A[i];
		}
		sort(perm.begin(), perm.end());
		do {
			string str;
			for (int i = 0; i < N; ++i) {
				str += to_string(perm[i]);
			}
			S.push_back(stoll(str));
		} while (next_permutation(perm.begin(), perm.end()));
		sort(S.begin(), S.end(), greater<long long>());
		S.erase(unique(S.begin(), S.end()), S.end());
		long long ans = -1;
		for (long long i : S) {
			if (isprime(i)) {
				ans = i;
				break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
0