結果
問題 | No.774 tatyamと素数大富豪 |
ユーザー |
|
提出日時 | 2018-12-24 00:12:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,595 bytes |
コンパイル時間 | 1,964 ms |
コンパイル使用メモリ | 179,684 KB |
実行使用メモリ | 16,960 KB |
最終ジャッジ日時 | 2024-09-25 10:50:49 |
合計ジャッジ時間 | 8,403 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 5 |
other | TLE * 1 -- * 13 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; long long int modpow( __int128 b, long long int e, long long int m) { __int128 result = 1; while ( e > 0 ) { if ( ( e & 1 ) == 1 ) { result = (result * b) % m; } e /= 2; b = (b * b) % m; } return result; } // ミラーラビン法による確率的な素数判定 bool isPrimeMillerRabin( long long int p, int k ) { if ( p == 2 ) { return true; } // 2は素数 if ( p < 2 || !( p & 1 ) ) { return false; } // 2よりも小さいまたは(2以外の)偶数なら計算するまでもなし mt19937 mt(0); uniform_int_distribution<ll> rnd(1, p - 2); // p - 1 = pow( 2, s ) * d, s > 0 において、最初の奇数dを見つける。 long long int d = p - 1; while ( !( d & 1 ) ) { d /= 2; } for ( int n = 0; n != k; ++n ) { long long int a = rnd(mt); long long int t = d; long long int y = modpow( a % p, t, p ); while( t != p - 1 && y != 1 && y != p - 1 ) { y = modpow( y, 2, p ); t *= 2; } if ( y != p - 1 && !( t & 1 ) ) { return false; } } return true; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<string> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); ll ans = -1; do { string s = ""; for (int i = 0; i < n; i++) s += a[i]; ll x = stoll(s); if (isPrimeMillerRabin(x, 50)) { ans = max(ans, x); } } while (next_permutation(a.begin(), a.end())); cout << ans << endl; return 0; }