Task: | Alkuluvut |
Sender: | Yytsi |
Submission time: | 2025-09-27 11:55:43 +0300 |
Language: | C++ (C++20) |
Status: | READY |
Result: | 100 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 17 |
#2 | ACCEPTED | 41 |
#3 | ACCEPTED | 42 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
#2 | ACCEPTED | 0.04 s | 2, 3 | details |
#3 | ACCEPTED | 0.05 s | 3 | details |
Code
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include "debug.hpp" #else #define debug(...) 0xffff #endif #define SMALL_P 90808080 int nums[11]; char numsc[11]; // https://github.com/Laakeri/contestlib/blob/master/src/math/miller-rabin.cpp // Deterministic Miller-Rabin primality test // Works for all 64 bit integers // Support of 128 bit integers is required to test over 32 bit integers typedef __int128 lll; lll powmod(lll a, lll p, lll mod) { if (p==0) return 1; if (p%2==0) { a=powmod(a, p/2, mod); return (a*a)%mod; } return (a*powmod(a, p-1, mod))%mod; } bool is_w(ll a, ll even, ll odd, ll p) { lll u = powmod(a, odd, p); if (u==1) return 0; for (ll j=1;j<even;j*=2) { if (u==p-1) return 0; u*=u;u%=p; } return 1; } bool isPrime(ll p) { if (p==2) return 1; if (p<=1||p%2==0) return 0; ll odd=p-1;ll even=1; while (odd%2==0) { even*=2;odd/=2; } ll b[7]={2, 325, 9375, 28178, 450775, 9780504, 1795265022}; for (ll i=0;i<7;i++) { ll a=b[i]%p; if (a==0) return 1; if (is_w(a, even, odd, p)) return 0; } return 1; } bool permuteDrop(const string& s) { if (s[0] == '0') return true; return false; } ll to_ll(const string& s) { ll val = 0; for (char c : s) val = (val * 10LL) + ll(c - '0'); return val; } void solve() { int n; cin>>n; bool hasOkDigit = false; string S; for (int i = 0; i < n; i++) { cin>>nums[i]; numsc[i] = (char)(nums[i]+48); S += to_string(nums[i]); // cout << S << "\n"; // cout << numsc[i]<<" CHAR\n"; hasOkDigit |= nums[i] == 1 || nums[i] == 3 || nums[i] == 7 || nums[i] == 9; } if (S == "2") return void(cout << "YES\n2\n"); if (S == "5") return void(cout << "YES\n5\n"); sort(S.begin(), S.end()); if (!hasOkDigit) return void(cout << "NO\n"); string ans; function<void(string&, int)> populateString = [&](string& s, int dep) { if (!ans.empty()) return; if (s.size() >= 1 && s.size() <= 16 && s[0] != '0' && isPrime(to_ll(s))) { ans = s; return; } if (s.size() == 16) return; if (dep == 0) return; char digit = numsc[rand() % n]; int pos = rand() % (s.size() + 1); // cout << "adding digit "<<digit<<" to pos "<<pos<<" resulting in: "; s.insert(s.begin() + pos, digit); // cout << s << "\n"; populateString(s, dep - 1); s.erase(s.begin() + pos); }; // take digits, permute and add from set while keeping valid? do { if (permuteDrop(S)) continue; for (int tries = 0; tries <= 101; tries++) { string s = S; populateString(s, 16 - S.size()); if (!ans.empty()) return void(cout << "YES\n" << ans << "\n"); } } while (next_permutation(S.begin(), S.end())); cout << "NO\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(time(0)); int t; cin>>t; while (t--) { solve(); } }
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 21023 YES 10220411 YES ... |