#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
int t = 0;
bool visit[30];
ll arr[30], a, b;
vector <int> adj[30];
bool prime(ll x) {
if (x < 2) return false;
for (ll i = 2; i <= sqrt(x); ++ i)
if (x % i == 0) return false;
return true;
}
int par[30];
vector <ll> ans;
void dfs(int u) {
visit[u] = true;
for (int v : adj[u]) {
if (visit[v]) continue;
if (prime(abs(arr[u] - arr[v]))) {
par[v] = u;
dfs(v);
}
}
}
int main() {
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> a >> b;
++ t;
arr[t] = a;
++ t;
arr[t] = b;
for (ll i = a - 2; i <= a + 2; ++ i) {
if (prime(i) && i != a) {
++ t;
arr[t] = i;
}
}
for (ll i = b - 2; i <= b + 2; ++ i) {
if (prime(i) && i != b) {
++ t;
arr[t] = i;
}
}
++ t;
arr[t] = 2;
for (int i = 1; i <= t; ++ i) {
for (int j = 1; j <= t; ++ j) {
if (i != j && prime(abs(arr[i] - arr[j]))) {
adj[i].pb(j);
adj[j].pb(i);
}
}
}
dfs(1);
if (!visit[2]) {
cout << -1;
return 0;
}
ans.pb(2);
int pivot = 2;
while (par[pivot] != 0) {
ans.pb(par[pivot]);
pivot = par[pivot];
}
cout << ans.size() << endl;
for (int i = ans.size() - 1; i >= 0; -- i) {
cout << arr[ans[i]] << " ";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
804 ms |
384 KB |
Output is correct |
2 |
Correct |
428 ms |
504 KB |
Output is correct |
3 |
Correct |
881 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
776 ms |
384 KB |
Output is correct |
2 |
Correct |
392 ms |
504 KB |
Output is correct |
3 |
Correct |
619 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
873 ms |
384 KB |
Output is correct |
2 |
Correct |
434 ms |
508 KB |
Output is correct |
3 |
Correct |
574 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
634 ms |
376 KB |
Output is correct |
2 |
Correct |
311 ms |
384 KB |
Output is correct |
3 |
Correct |
203 ms |
384 KB |
Output is correct |