結果

問題 No.774 tatyamと素数大富豪
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2022-10-10 21:19:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 1,573 bytes
コンパイル時間 1,856 ms
コンパイル使用メモリ 172,220 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 00:37:43
合計ジャッジ時間 3,370 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 18 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 29 ms
4,380 KB
testcase_13 AC 12 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 10 ms
4,376 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 6 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2022.10.10 21:19:24
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


bool MillerRabin(long long N) {
    if (N <= 1) return false;
    if (N == 2) return true;
    if (N % 2 == 0) return false;

    auto modpow = [](__int128_t a, long long n, long long mo) {
        __int128_t r = 1;
        a %= mo;
        while (n) r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
        return r;
    };

    vector<long long> A = {2, 325, 9375, 28178, 450775,
                           9780504, 1795265022};
    long long s = 0, d = N - 1;
    while (d % 2 == 0) d >>= 1, s++;

    for (long long a : A) {
        if (a % N == 0) return true;
        long long j, r = modpow(a, d, N);
        if (r == 1) continue;
        for (j = 0; j < s; j++) {
            if (r == N - 1) break;
            r = __int128_t(r) * r % N;
        }
        if (j == s) return false;
    }
    return true;
}

int main() {
    int n;
    cin >> n;
    vector< int > a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    ll ans = -1;
    do {
        ll num = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= 10) {
                num *= 100;
                num += a[i];
            } else {
                num *= 10;
                num += a[i];
            }
        }
        
        if (ans < num && MillerRabin(num)) ans = max(ans, num);
    } while (next_permutation(a.begin(), a.end()));
    cout << ans << endl;
    return 0;
}
0