結果

問題 No.774 tatyamと素数大富豪
ユーザー tottoripapertottoripaper
提出日時 2018-12-25 02:09:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,174 bytes
コンパイル時間 1,856 ms
コンパイル使用メモリ 171,232 KB
実行使用メモリ 16,680 KB
最終ジャッジ日時 2024-04-08 16:19:44
合計ジャッジ時間 8,464 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N;
std::vector<int> A;

template <typename T>
T mult(T a, T b, T mod){
    T res = 0;
    while(b){
        if(b & 1){res = (res + a) % mod;}
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

template <typename T>
T expt(T a, T n, T mod){
    T res = 1;
    while(n){
        if(n & 1){res = mult(res, a, mod);}
        a = mult(a, a, mod);
        n >>= 1;
    }
    return res;
}

bool isPrime(ll n){
    if(n % 2 == 0){
        return n == 2;
    }

    ll k = 0, q = n - 1;
    while(q % 2 == 0){
        k += 1;
        q /= 2;
    }

    constexpr ll sets[] = {2, 325, 28178}; //, 9375, 28178, 450775, 9780504, 1795265022};

    for(ll a : sets){
        ll p = expt(a, q, n);
        if(p == 1){
            continue;
        }

        bool ok = false;
        for(int i=0;i<k;++i){
            if(p == n - 1){
                ok = true;
                break;
            }

            p = mult(p, p, n);
        }

        if(!ok){
            return false;
        }
    }

    return true;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N;

    A.resize(N);

    for(int i=0;i<N;++i){
        std::cin >> A[i];
    }

    ll res = -1;
    while(true){
        unless(N > 1 && (A[N-1] % 2 == 0 || A[N-1] % 5 == 0)){
            ll n = 0;
            for(int a : A){
                if(a < 10){
                    n = n * 10 + a;
                }else{
                    n = n * 100 + a;
                }
            }

            unless(n > 100 && (n % 3 == 0 || n % 7 == 0 || n % 11 == 0 || n % 13 == 0)){
                if(isPrime(n)){
                    res = std::max(res, n);
                }
            }
        }

        if(!next_permutation(A.begin(), A.end())){
            break;
        }
    }

    std::cout << res << std::endl;
}
0