Submission details
Task:Alkuluvut
Sender:anton_h
Submission time:2025-09-28 12:43:38 +0300
Language:C++ (C++20)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED17
#2ACCEPTED41
#3ACCEPTED42
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3details
#2ACCEPTED0.01 s2, 3details
#3ACCEPTED0.03 s3details

Code

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
using vl=vector<ll>;
#define all(c) begin(c),end(c)
#define pb push_back
#define sz(c) ll((c).size())

#define FOR(i,a,b) for(ll i=(a);i<(b);i++)

#define TR(x) ({ if(1) cout << __LINE__ << ": " #x " = " << (x) << endl; })

// Only when needed
using dd = double;
const dd eps = 1e-10;
using vd = vector<dd>;
using vvd = vector<vd>;

using vvl = vector<vl>;


using i128 = __int128;
i128 modPow(i128 a, ll e, ll P) {
	return (e%2 ? a : 1) * (e ? modPow(a*a%P, e/2, P) : 1) % P;
}

bool isPrime(ll P) {
	if(P<=1) return 0;
	ll lo=P-1;
	while(lo%2 == 0) lo /= 2;
	for(ll w : {2,325,9375,28178,450775,9780504,1795265022}) {
		i128 v = modPow(w,lo,P);
		if(v==1 || w%P == 0) continue;
		for(ll r=lo;r != P-1 && v != P-1;r*=2) v = (v*v)%P;
		if(v != P-1) return 0;
	}
	return 1;
}


void solve(){
    ll k; cin >> k;
    vl dig(k);
    bool div3 = true, div_2_5 = true;
    for(ll &d : dig){
        cin >> d;
        if(d % 3) div3 = false;
        if(d % 2 && d % 5) div_2_5 = false;
    }
    if(div3){
        if(dig == vl{3}){
            cout << "YES\n" << 3 << "\n";
        }else{
            cout << "NO\n";
        }
        return;
    }
    if(div_2_5){
        if(dig == vl{2} || dig == vl{5}){
            cout << "YES\n" << dig[0] << "\n";
        }else{
            cout << "NO\n";
        }
        return;
    }
    if(sz(dig) == 1){
        cout << "YES\n";
        cout << (dig[0] == 1 ? 11 : dig[0]) << "\n";
        return;
    }
    if(dig == vl{0, 7} || dig == vl{7, 0}){
        cout << "NO\n";
        return;
    }
    auto rng = mt19937_64((random_device())());
    FOR(it, 0, 10000){
        ll rem = 15 - k;
        string ds;
        for(ll d : dig) ds.push_back('0' + d);
        while(rem > 0){
            for(ll d : dig){
                if(rng() % 2){
                    rem--;
                    ds.push_back('0' + d);
                    if(rng() % 6 == 0){
                        rem = 0;
                    }
                }           
                if(rem == 0) break;
            }
        }
        ll d3 = 0;
        for(char c : ds) d3 += c - '0';
        if(d3 % 3 == 0) continue;
        FOR(it2, 0, 30){
            shuffle(all(ds), rng);
            if(ds[0] == '0') continue;
            if((ds.back() -'0') % 2 == 0 || (ds.back() - '0') % 5 == 0) continue;
            ll num = stoll(ds);
            if(isPrime(num)){
                cout << "YES\n";
                cout << num << "\n";
                return;
            }
        }
    }
    assert(false);
}

int main(int argc, char const *argv[])
{
    cin.sync_with_stdio(0);
    cin.tie(0);

    ll tc; cin >> tc;
    while(tc--) solve();
    return 0;
}

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
10
1
0
1
1
...

correct output
NO
YES
11
YES
2
...

user output
NO
YES
11
YES
2
...

Test 2

Group: 2, 3

Verdict: ACCEPTED

input
175
1
0
1
1
...

correct output
NO
YES
11
YES
2
...

user output
NO
YES
11
YES
2
...

Test 3

Group: 3

Verdict: ACCEPTED

input
848
4
0 1 2 3
4
0 1 2 4
...

correct output
YES
10223
YES
4021
YES
...

user output
YES
2311120003
YES
424041210102401
YES
...