| Task: | Alkuluvut |
| Sender: | Yytsi |
| Submission time: | 2025-09-27 11:31:01 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | 0 |
| group | verdict | score |
|---|---|---|
| #1 | WRONG ANSWER | 0 |
| #2 | WRONG ANSWER | 0 |
| #3 | WRONG ANSWER | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | WRONG ANSWER | 0.21 s | 1, 2, 3 | details |
| #2 | TIME LIMIT EXCEEDED | -- | 2, 3 | details |
| #3 | TIME LIMIT EXCEEDED | -- | 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;
if (s.back() == '0' || s.back() == '2' || s.back() == '4' || s.back() == '5' ||
s.back() == '6' || s.back() == '8') 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;
}
sort(S.begin(), S.end());
if (!hasOkDigit) return void(cout << "NO\n");
string ans;
function<void(string&, int)> populateString = [&](string& s, int dep) {
if (dep == 0) return;
if (!ans.empty()) return;
if (s.size() == 16) return;
if (ans.empty() && s[0] != '0' && isPrime(to_ll(s))) {
ans = s;
return;
}
char digit = numsc[rand() % n];
int pos = rand() % s.size();
// 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 <= 10101; tries++) {
// pick a random index (not last) and random digit from selection
string s = S;
populateString(s, 17 - S.size() + 1);
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: WRONG ANSWER
| input |
|---|
| 10 1 0 1 1 ... |
| correct output |
|---|
| NO YES 11 YES 2 ... |
| user output |
|---|
| NO YES 11 NO YES ... |
Test 2
Group: 2, 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 175 1 0 1 1 ... |
| correct output |
|---|
| NO YES 11 YES 2 ... |
| user output |
|---|
| (empty) |
Test 3
Group: 3
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 848 4 0 1 2 3 4 0 1 2 4 ... |
| correct output |
|---|
| YES 10223 YES 4021 YES ... |
| user output |
|---|
| (empty) |
