Submission details
Task:Alkuluvut
Sender:jhuun
Submission time:2025-09-27 22:26:41 +0300
Language:C++ (C++20)
Status:READY
Result:17
Feedback
groupverdictscore
#1ACCEPTED17
#20
#30
Test results
testverdicttimegroup
#1ACCEPTED0.02 s1, 2, 3details
#2--2, 3details
#3--3details

Code

#include <bits/stdc++.h>

constexpr auto L = 1e6;
using i128 = __int128_t;
 
std::vector<int64_t> P = []() {
    std::vector<int64_t> p(L + 1, true);
    p[0] = p[1] = false;
    for (int64_t i = 2; i <= L; ++i) {
        if (p[i]) {
            for (int64_t j = i * i; j <= L; j += i) {
                p[j] = false;
            }
        }
    }
    return p;
}();
 
i128 m_pow(i128 b, i128 exp, i128 m) {
    i128 res = 1;
    b %= m;
    while (exp) {
        if (exp & 1) {
            (res *= b) %= m;
        }
        (b *= b) %= m;
        exp >>= 1;
    }
    return res;
}
 
bool mr_check(i128 n, i128 p, i128 d, int s) {
    auto x = m_pow(p, d, n);
    if (x == 1 || x == n - 1) {
        return false;
    }
    for (auto r = 1; r < s; ++r) {
        (x *= x) %= n;
        if (x == n - 1) {
            return false;
        }
    }
    return true;
}
 
bool is_prime(int64_t x) {
    if (x <= L) {
        return P[x];
    }
    auto r = 0;
    auto d = x - 1;
    while ((d & 1) == 0) {
        d >>= 1;
        r++;
    }
    for (const auto p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
        if (mr_check(x, p, d, r)) {
            return false;
        }
    }
    return true;
}

int64_t to_number(const std::vector<int64_t>& N) {
    int64_t x = 0;
    for (const auto n : N) {
        x *= 10;
        x += n;
    }
    return x;
}

void check_perms(const std::vector<int64_t>& N_orig, std::vector<int64_t> N, bool& ok) {
    std::sort(N.begin(), N.end());
    do {
        if (N.back() % 2 == 0 && N.back() % 5 == 0) {
            continue;
        }
        const auto n = to_number(N);
        if (is_prime(n)) {
            std::cout << "YES\n" << n << '\n';
            ok = true;
            return;
        }
    } while (std::next_permutation(N.begin(), N.end()));
    if (N.size() < 16) {
        for (const auto n : N_orig) {
            N.push_back(n);
            check_perms(N_orig, N, ok);
            if (ok) {
                return;
            }
            N.pop_back();
        }
    }
}

bool ok(const std::vector<int64_t>& N) {
    if (N.size() == 1 && N.back() == 2) {
        std::cout << "YES\n2\n";
        return true;
    }
    if (N.size() == 2 && N[0] * N[1] == 10) {
        return false;
    }
    bool ok = false;
    for (const auto n : N) {
        if (n % 2) {
            ok = true;
        }
    }
    if (!ok) {
        return false;
    }
    check_perms(N, N, ok = false);
    return ok;
}

int main() {
    int t;
    std::cin >> t;
    for (int i = 0, k; i < t; ++i) {
        std::cin >> k;
        std::vector<int64_t> N(k);
        for (auto& n : N) {
            std::cin >> n;
        }
        if (!ok(N)) {
            std::cout << "NO\n";
        }
    }
}

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:

input
175
1
0
1
1
...

correct output
NO
YES
11
YES
2
...

user output
(empty)

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
(empty)