結果
問題 | No.774 tatyamと素数大富豪 |
ユーザー |
|
提出日時 | 2018-12-22 18:15:58 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,690 ms / 2,000 ms |
コード長 | 1,446 bytes |
コンパイル時間 | 1,932 ms |
コンパイル使用メモリ | 171,668 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-01 13:23:51 |
合計ジャッジ時間 | 7,474 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define sorted(a) sort(a.begin(), a.end());typedef long long signed int ll;const vector<unsigned int> numset = {2,3,5,7,11,13,17,19,23,29,31,37};unsigned long long mod_pow(unsigned __int128 x, unsigned long long k, unsigned long long mod) {unsigned __int128 res = 1;while (k) {if (k & 1) res = res * x % mod;x = x * x % mod;k >>= 1;}return res;}bool isPrime(unsigned long long n) {if (n < 2) return false;unsigned long long d = n - 1, s = 0;while(d % 2 == 0){d /= 2;s++;}for(unsigned int a : numset) {if (a == n) return true;unsigned long long res = mod_pow(a, d, n);if (res == 1) continue;bool ok = true;for (unsigned int r = 0; r < s; r++) {if (res == n-1) {ok = false;break;}res = (__int128)res * res % n;}if (ok) return false;}return true;}int main(void) {#ifdef DEBUGfreopen("input.txt", "r", stdin);#endifios_base::sync_with_stdio(false);cin.tie(0);int N;cin >> N;vector<int> A(N);for (int i = 0; i < N; i++) {cin >> A[i];}ll ans = -1;ll n, l;do {n = 0;for (int i = 0; i < N; i++) {l = pow(10, to_string(A[i]).length());n = n * l + A[i];}if (isPrime(n)) {ans = max(ans, n);}} while (next_permutation(A.begin(), A.end()));cout << ans << endl;return 0;}