結果

問題 No.774 tatyamと素数大富豪
コンテスト
ユーザー square1001
提出日時 2019-03-27 14:09:19
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 1,094 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 888 ms
コンパイル使用メモリ 99,780 KB
実行使用メモリ 7,776 KB
最終ジャッジ日時 2026-04-27 08:11:44
合計ジャッジ時間 4,408 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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] = i;
		}
		do {
			string str;
			for (int i = 0; i < N; ++i) {
				str += to_string(A[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