結果

問題 No.774 tatyamと素数大富豪
ユーザー square1001square1001
提出日時 2019-03-27 14:17:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 8,788 KB
最終ジャッジ日時 2024-04-19 09:54:19
合計ジャッジ時間 8,547 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
8,788 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1,463 ms
6,940 KB
testcase_11 AC 1,197 ms
6,940 KB
testcase_12 AC 50 ms
6,944 KB
testcase_13 AC 58 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1,314 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 16 ms
6,940 KB
testcase_18 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
bool isprime(long long x) {
	if (x == 1) return false;
	if (x == 2) return true;
	if (x % 2 == 0) return false;
	if (x % 3 == 0) return false;
	for (int i = 5; 1LL * i * i <= x; i += 6) {
		if (x % i == 0) {
			return false;
		}
		if (x % (i + 2) == 0) {
			return false;
		}
	}
	return true;
}
int main() {
	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) {
		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