| Task: | Alkuluvut |
| Sender: | Metabolix |
| Submission time: | 2025-09-26 21:54:32 +0300 |
| Language: | C++ (C++17) |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 17 |
| #2 | ACCEPTED | 41 |
| #3 | ACCEPTED | 42 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.20 s | 1, 2, 3 | details |
| #2 | ACCEPTED | 0.20 s | 2, 3 | details |
| #3 | ACCEPTED | 0.20 s | 3 | details |
Compiler report
input/code.cpp: In function 'long int bitit_luvuksi(int)':
input/code.cpp:15:9: warning: unused variable 'numero' [-Wunused-variable]
15 | int numero = 0;
| ^~~~~~Code
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <queue>
#include <stdexcept>
using namespace std;
int numero_biteiksi(int numero) {
return 1 << numero;
}
long bitit_luvuksi(int bitit) {
int numero = 0;
long luku = 0;
for (int numero = 1; numero <= 10; ++numero) {
if (bitit & numero_biteiksi(numero % 10)) {
luku = luku * 10 + numero % 10;
}
}
return luku;
}
int tutki_numerot(long luku) {
int numerot = 0;
while (luku > 0) {
numerot |= numero_biteiksi(luku % 10);
luku /= 10;
}
return numerot;
}
vector<long> ennakko() {
vector<long> luvut(1024, 0);
int luvut_puuttuu = luvut.size();
auto luvut_lisaa = [&](int numerot, long alkuluku) {
if (luvut[numerot] == 0) {
luvut[numerot] = alkuluku;
--luvut_puuttuu;
return true;
}
return false;
};
luvut_lisaa(0, -1);
luvut_lisaa(tutki_numerot(1), 11);
luvut_lisaa(tutki_numerot(2), 2);
luvut_lisaa(tutki_numerot(3), 3);
luvut_lisaa(tutki_numerot(5), 5);
luvut_lisaa(tutki_numerot(7), 7);
// 7 ja 0 eivät tee alkulukuja.
luvut_lisaa(tutki_numerot(70), -1);
for (int i = 0; i < 1024; ++i) {
// Parillisista ja viitosesta ei saa alkulukuja (paitsi 2)
if (!(i & tutki_numerot(1379))) {
luvut_lisaa(i, -1);
}
// Kolmella jaollisista ei saa alkulukuja (paitsi 3)
if (!(i & tutki_numerot(124578))) {
luvut_lisaa(i, -1);
}
}
vector<long> alkuluvut = {2, 3};
auto onko_alkuluku = [&](long luku) {
if (luku >= alkuluvut.back() * alkuluvut.back()) {
throw runtime_error("Alkuluvut loppuivat");
}
for (long jakaja : alkuluvut) {
if (jakaja * jakaja > luku) {
break;
}
if (luku % jakaja == 0) {
return false;
}
}
return true;
};
for (long i = 5, j = 2; i * i <= 1234567890999l; i += j, j = 6 - j) {
if (onko_alkuluku(i)) {
alkuluvut.push_back(i);
}
}
for (int numerot = 1; numerot <= 1023; ++numerot) {
if (luvut[numerot] != 0) {
continue;
}
long luku = bitit_luvuksi(numerot);
queue<long> yritykset;
yritykset.push(luku);
while (true) {
long yritys = yritykset.front();
if (onko_alkuluku(yritys)) {
luvut_lisaa(numerot, yritys);
break;
}
yritykset.pop();
for (int numero = 0; numero <= 9; ++numero) {
if (!(numerot & numero_biteiksi(numero))) {
continue;
}
long uusi = yritys * 10 + numero;
yritykset.push(uusi);
}
}
}
return luvut;
}
int main() {
vector<long> luvut = ennakko();
int testeja;
cin >> testeja;
for (int testi = 0; testi < testeja; ++testi) {
int numeroita;
cin >> numeroita;
int numerot = 0;
for (int i = 0; i < numeroita; ++i) {
int numero;
cin >> numero;
numerot |= numero_biteiksi(numero);
}
if (luvut[numerot] > 0) {
cout << "YES\n" << luvut[numerot] << "\n";
} else {
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: ACCEPTED
| input |
|---|
| 175 1 0 1 1 ... |
| correct output |
|---|
| NO YES 11 YES 2 ... |
| user output |
|---|
| NO YES 11 YES 2 ... Truncated |
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 12301 YES 12401 YES ... Truncated |
