結果

問題 No.774 tatyamと素数大富豪
ユーザー pekempeypekempey
提出日時 2018-12-22 02:07:40
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,034 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 85,408 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-08 22:29:21
合計ジャッジ時間 7,789 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
    if (n == 3) return true;
    if (n == 5) return true;
    if (n == 7) return true;
    if (n % 2 == 0) return false;
    if (n % 3 == 0) return false;
    if (n % 5 == 0) return false;
    if (n % 7 == 0) return false;
    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;
}

0