結果

問題 No.774 tatyamと素数大富豪
ユーザー 👑 tatyamtatyam
提出日時 2018-06-23 20:19:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 2,112 ms
コンパイル使用メモリ 169,992 KB
実行使用メモリ 7,504 KB
最終ジャッジ日時 2024-04-08 22:24:43
合計ジャッジ時間 3,084 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
7,504 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 33 ms
7,504 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 41 ms
7,504 KB
testcase_10 AC 37 ms
7,504 KB
testcase_11 AC 40 ms
7,504 KB
testcase_12 AC 42 ms
7,504 KB
testcase_13 AC 42 ms
7,504 KB
testcase_14 AC 6 ms
6,676 KB
testcase_15 AC 40 ms
7,504 KB
testcase_16 AC 39 ms
7,504 KB
testcase_17 AC 35 ms
7,504 KB
testcase_18 AC 39 ms
7,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define fcout cout << fixed << setprecision(10)

vector<int> card;
vector<ll> permlist;
ll bigMul(ll a, ll b, ll m){
    int base = (int)1e9;
    ll a_low = a % base, a_high = a / base, b_low = b % base, b_high = b / base, result;
    result = (a_high * b_high) % m;
    rep(i, 0, 9) result = (result * 10) % m;
    result = (result + a_low*b_high + b_low*a_high) % m;
    rep(i, 0, 9) result = (result * 10) % m;
    result = (result + a_low*b_low) % m;
    return result;
}

//n**p % m
ll bigPowMod(ll n, ll p, ll m){
    ll ans = 1, ln = n;
    if(p <= 0) return 1;
    while(p != 0){
        if((p & 1) == 1) ans = bigMul(ans, ln, m); //ans = (ans*ln) % m;
        //ln = (ln * ln) % m;
        ln = bigMul(ln, ln, m);
        p = p >> 1;
    }
    return ans;
}

bool suspect(int a, int s, ll d, ll n) {
    ll x = bigPowMod(a, d, n);
    if (x == 1) return true;
    for (int r = 0; r < s; ++r) {
        if (x == n - 1) return true;
        //x = x * x % n;
        x = bigMul(x, x, n);
    }
    return false;
}
//MillerRabin primality test
// {2,7,61,-1}                 is for n < 4759123141 (= 2^32)
// {2,3,5,7,11,13,17,19,23,-1} is for n < 10^16 (at least)
int test[] = {2,3,5,7,11,13,17,19,23,-1};
bool MillerRabin(ll n) {
    if (n <= 1 || (n > 2 && n % 2 == 0)) return false;
    ll d = n - 1;
    int s = 0;
    while (d % 2 == 0){
        s++;
        d /= 2;
    }
    for (int i = 0; test[i] < n && test[i] != -1; ++i)
        if (!suspect(test[i], s, d, n)) return false;
    return true;
}
bool is_prime(ll n){
    if (n < 2) return false;
    if (n < 4) return true;
    if (!(n % 2)) return false;
    if (!(n % 3)) return false;
    ll sq = sqrt(n);
    for(int i = 5 ;i < 32; i += 4 - i % 6 / 2){
        if (sq < i) return true;;
        if (!(n % i)) return false;
    }
    if(MillerRabin(n))return true;
    return false;
}
ll putBack(ll a, int b){
    return a * (b < 10 ? 10 : 100) + b;
}
vector<int> erase(vector<int> v,int pos){
    v.erase(v.begin()+pos);
    return v;
}
void permutations(vector<int> v,ll cnt){
    if(v.size()==1){
        permlist.push_back(putBack(cnt, v[0]));
        return;
    }
    rep(i,0,v.size()){
        permutations(erase(v,i), putBack(cnt, v[i]));
    }
}
int main(){
    int n,a;
    cin >> n;
    card.reserve(n);
    rep(i, 0, n){
        cin >> a;
        card.push_back(a);
    }
    permlist.push_back(0);
    permutations(card,0);
    sort(permlist.begin()+1, permlist.end(), greater<ll>());
    rep(i,1,permlist.size())if(permlist[i-1]!=permlist[i]&&is_prime(permlist[i])){
        cout<<permlist[i]<<endl;
        return 0;
    }
    cout<<-1<<endl;
}
0