Submission details
Task:Alkuluvut
Sender:Lieska
Submission time:2025-09-26 22:13:47 +0300
Language:C++ (C++20)
Status:READY
Result:58
Feedback
groupverdictscore
#1ACCEPTED17
#2ACCEPTED41
#30
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.01 s2, 3details
#30.06 s3details

Compiler report

input/code.cpp: In function 'bool f(std::vector<long long int>)':
input/code.cpp:21:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |         for (ll i=0; i<temp.size(); ++i){
      |                      ~^~~~~~~~~~~~

Code

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

typedef long long ll;

bool p[1000001];
vector<ll> primes;
ll pow_ten[20];


bool f(vector<ll> temp){
    sort(temp.begin(), temp.end());
    ll div_three = 0;
    for (auto u:temp) div_three += u;
    if (div_three%3==0) return 0;
    do {
        // With approach below we want get leading zeros, but in this case we need at least one zero.
        if (temp[temp.size()-1]==0) continue; 

        ll a= 0;
        for (ll i=0; i<temp.size(); ++i){
            a += temp[i]*pow_ten[i];
            // Now the first number will be ones, second tens and so on, but shouldn't matter.
        }
        bool prime = true;
        for (auto u:primes){
            if (a%u==0){
                prime = false;
                break;
            }
            if (u*u > a) break;
        }
        if (prime){
            cout << "YES\n";
            cout << a << "\n";
            return true;
        }
    } while (next_permutation(temp.begin(), temp.end()));
    return false;
}

void test(){
    ll k;
    cin >> k;
    vector<ll> v;
    for (ll i=1; i<=k; ++i){
        ll a;
        cin >> a;
        v.push_back(a);
    }
    if (k==1){
        if (v[0]==2 || v[0]==3 || v[0]==5 || v[0]==7) {
            cout << "YES\n" << v[0] << "\n";
            return;
        }
        if (v[0]==1) {
            cout << "YES\n11 \n";
            return;
        }
    }
    if (k==10){
        cout << "YES\n";
        cout << "89765432101\n";
        return;
    }
    if (k==9){
        cout << "YES\n";
        int sum= 0;
        for (auto u:v) sum+=u;
        if (sum==36) cout << "6875432101\n";
        if (sum==37) cout << "956743201\n";
        if (sum==38) cout << "689543201\n";
        if (sum==39) cout << "8597432101\n";
        if (sum==40) cout << "798643201\n";
        if (sum==41) cout << "987653201\n";
        if (sum==42) cout << "7986542101\n";
        if (sum==43) cout << "798654301\n";
        if (sum==44) cout << "689754203\n";
        if (sum==45) cout << "9876543211\n";
    }
    // Rule out some simple failures (which I think are all failures with k >= 3)
    bool all_div_three = true;
    bool no_suitable_last = true;
    for (auto u:v){
        if (!(u==0 || u==3 || u==6 || u==9)) all_div_three = false;
        if (u%2==1 && u!=5) no_suitable_last = false;
    }
    if (all_div_three || no_suitable_last) {
        cout << "NO\n";
        return;
    }
    
    // In randomness we trust.
    if (f(v)) return;
    for (auto u:v){
        vector<ll> temp = v;
        temp.push_back(u);
        if (f(temp)==1) return;
    }
    for (auto u:v){
        for (auto w:v){
            // I really hope two extra numbers is enough. 
            vector<ll> temp = v;
            temp.push_back(u);
            temp.push_back(w);
            if (f(temp)==1) return;
        }
    }
    cout << "NO\n";
}

int main(){
    ll t;
    cin >> t;
    for (ll i=2; i<=1000000; ++i){
        if (p[i] == 0){
            primes.push_back(i);
            for (ll j= 2; j*i <= 1000000; ++j){
                p[i*j] = 1;
            }
        }
    }
    ll a = 1;
    for (ll i=0; i<=16; ++i){
        pow_ten[i] = a;
        a *=10;
    }
    for (ll i=0; i<t; ++i){
        test();
    }
    
}

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:

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

correct output
YES
10223
YES
4021
YES
...

user output
YES
12301
YES
4201
YES
...