結果

問題 No.774 tatyamと素数大富豪
ユーザー 👑 tatyamtatyam
提出日時 2018-06-21 23:53:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,249 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 201,728 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 07:44:55
合計ジャッジ時間 2,965 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 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)

array<int, 14> card;
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) {cout << n; return true;}
    if (!(n % 2)) return false;
    if (!(n % 3)) return false;
    ll sq = sqrt(n);
    for(int i = 5 ;i < 64; i += 4 - i % 6 / 2){
        if (sq < i) {cout << n; return true;};
        if (!(n % i)) return false;
    }
    if(MillerRabin(n)){cout << n; return true;}
    return false;
}
ll putBack(ll a, int b){
    return a * (b < 10 ? 10 : 100) + b;
}
bool ans(int n, ll cnt){
    if(!n){
        if(is_prime(cnt)) return true;
        else return false;
    }
    if(n == 1) rep(i, 1, 14) if(card[i]){
        if(ans(n - 1, putBack(cnt, i))) return true;
        else return false;
    }
    for(int i = 9 ; i > 1 ; i--)if(card[i]){
        card[i]--;
        if(ans(n - 1, putBack(cnt, i))) return true;
        card[i]++;
    }
    if(card[1]){
        card[1]--;
        for(int i = 9 ; i > 3 ; i--)if(card[i]){
            card[i]--;
            if(ans(n - 2, putBack(cnt, 10 + i))) return true;
            card[i]++;
        }
        card[1]++;
    }
    for(int i = 3 ; i > -1 ; i--){
        if(card[10 + i]){
            card[10 + i]--;
            if(ans(n - 1, putBack(cnt, 10 + i))) return true;
            card[10 + i]++;
        }
        else if(card[1]){
            card[1]--;
            if(card[i]){
                card[i]--;
                if(ans(n - 2, putBack(cnt, 10 + i))) return true;
                card[i]++;
            }
            card[1]++;
        }
    }
    return false;
}
int main(){
    int n, a;
    cin >> n;
    rep(i, 0, n){
        cin >> a;
        card[a]++;
    }
    if(!ans(n, 0))cout << -1;
}
0