結果
問題 |
No.774 tatyamと素数大富豪
|
ユーザー |
|
提出日時 | 2018-12-22 02:06:49 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,811 bytes |
コンパイル時間 | 728 ms |
コンパイル使用メモリ | 84,704 KB |
実行使用メモリ | 16,960 KB |
最終ジャッジ日時 | 2024-09-25 09:56:56 |
合計ジャッジ時間 | 7,087 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 5 |
other | TLE * 1 -- * 13 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <set> #include <ctime> #include <random> using namespace std; int n; int a[9]; mt19937 mt(11111); long long modmul(long long a, long long b, long long m) { long long res = 0; while (b > 0) { if (b & 1) { res += a; if (res >= m) res -= m; } a += a; if (a >= m) a -= m; b >>= 1; } return res; } long long modpow(long long a, long long b, long long m) { long long res = 1; while (b > 0) { if (b & 1) { res = modmul(res, a, m); } a = modmul(a, a, m); b >>= 1; } return res; } bool suspect(long long a, long long e, long long k, long long n) { a = modpow(a, e, n); if (a == 1) return true; while (k--) { if (a == n - 1) return true; a = modmul(a, a, n); } return false; } bool is_prime(long long n) { if (n <= 1) return false; if (n == 2) return true; long long e = n - 1; long long k = 0; while (e % 2 == 0) { e /= 2; k++; } for (int ii = 0; ii < 2; ii++) { long long b = uniform_int_distribution<long long>(2, n - 2)(mt); if (!suspect(b, e, k, n)) return false; } return true; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); long long ans = -1; do { long long x = 0; for (int i = 0; i < n; i++) { if (a[i] < 10) { x = x * 10 + a[i]; } else { x = x * 100 + a[i]; } } if (is_prime(x)) { ans = max(ans, x); } } while (next_permutation(a, a + n)); cout << ans << endl; }