Submission details
Task:Alkuluvut
Sender:Yytsi
Submission time:2025-09-27 13:32:50 +0300
Language:C++ (C++20)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED17
#2ACCEPTED41
#3ACCEPTED42
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3details
#2ACCEPTED0.02 s2, 3details
#3ACCEPTED0.04 s3details

Code

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

#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) 0xffff
#endif

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;

  if (n == 10) {
    cout << "YES\n10234567879\n";
    return;
  }

  bool hasOkDigit = false;
  string S;
  int sm = 0;

  for (int i = 0; i < n; i++) {
    cin>>nums[i];
    sm += (int)(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 (n == 9) {
    if (sm == 45) return void(cout << "YES\n1234568789\n");
    if (sm == 44) return void(cout << "YES\n2203456789\n");
    if (sm == 43) return void(cout << "YES\n103456789\n");
    if (sm == 42) return void(cout << "YES\n5180248567859\n");
    if (sm == 41) return void(cout << "YES\n102356789\n");
    if (sm == 40) return void(cout << "YES\n102346789\n");
    if (sm == 39) return void(cout << "YES\n1023047577809\n");
    if (sm == 38) return void(cout << "YES\n102345689\n");
    if (sm == 37) return void(cout << "YES\n1023455306479\n");
    if (sm == 36) return void(cout << "YES\n10234256783\n");
  }

  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 <= 50; 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
10042441
YES
...