結果

問題 No.774 tatyamと素数大富豪
ユーザー finefine
提出日時 2018-12-27 16:53:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,600 ms / 2,000 ms
コード長 1,594 bytes
コンパイル時間 2,238 ms
コンパイル使用メモリ 177,168 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:04:18
合計ジャッジ時間 7,313 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,600 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,948 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,948 KB
testcase_09 AC 26 ms
6,948 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 9 ms
6,944 KB
testcase_12 AC 742 ms
6,948 KB
testcase_13 AC 379 ms
6,948 KB
testcase_14 AC 8 ms
6,948 KB
testcase_15 AC 13 ms
6,948 KB
testcase_16 AC 8 ms
6,944 KB
testcase_17 AC 6 ms
6,948 KB
testcase_18 AC 8 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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, 7)) {
            ans = max(ans, x);
        }
    } while (next_permutation(a.begin(), a.end()));
    cout << ans << endl;
    return 0;
}
0