結果

問題 No.774 tatyamと素数大富豪
ユーザー tottoripapertottoripaper
提出日時 2018-12-25 18:26:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,128 bytes
コンパイル時間 7,330 ms
コンパイル使用メモリ 361,288 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-08 22:38:40
合計ジャッジ時間 13,690 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 834 ms
6,676 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1,218 ms
6,548 KB
testcase_03 AC 3 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 4 ms
6,548 KB
testcase_06 TLE -
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>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using namespace boost::multiprecision;

#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::deque<int> deq;

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

inline int f(int n){
    if(n < 10){
        return 10;
    }
    return 100;
}

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, 9375}; // , 28178

    for(ll a : sets){
        auto p = expt<int128_t>(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 = p * p % n;
        }

        if(!ok){
            return false;
        }
    }

    return true;
}

ll rec(ll n, ll x, ll k){
    if(k == 0){
        if(isPrime(n)){
            return n;
        }
        return -1;
    }

    ll res = -1;
    for(int i=0;i<k;++i){
        int A = deq.front();

        deq.pop_front();
        res = std::max(res, rec(n + x * A, x * f(A), k - 1));
        deq.emplace_back(A);
    }

    return res;
}

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

    std::cin >> N;

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

        deq.emplace_back(A);
    }

    ll res = -1;
    for(int i=0;i<N;++i){
        int A = deq.front();

        deq.pop_front();
        unless(A % 2 == 0 || A % 5 == 0){
            res = std::max(res, rec(A, f(A), N - 1));
        }        
        deq.emplace_back(A);
    }

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