#include <bits/stdc++.h>
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)
using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
bool isprime(ll x) {
if(x == 0 || x == 1) return false;
for(ll i = 2; i * i <= x; i++) if(x % i == 0) return false;
return true;
}
ll a, b;
vi vis, ans;
vvi adj;
vector<ll> primes;
int done = 0;
void dfs(int node) {
if(done) return;
if(vis[node]) return;
vis[node] = 1;
if(primes[node] == b) {
cout << ans.size() << endl;
for(int i : ans) cout << i << " ";
cout << endl;
done = 1;
return;
}
for(int i : adj[node]) {
ans.pub(primes[i]);
dfs(i);
ans.pob();
if(done) return;
}
if(done) return;
}
int main() {
// freopen("INSERT_NAME_HERE.in", "r", stdin);
// freopen("INSERT_NAME_HERE.out", "w", stdout);
cin >> a >> b;
for(int i = a - 2; i <= a + 2; i += 2) if(isprime(i)) primes.push_back(i);
for(int i = b - 2; i <= b + 2; i += 2) if(isprime(i)) primes.push_back(i);
primes.pub(2);
sort(all(primes));
primes.erase(unique(all(primes)), primes.end());
adj.resize(primes.size());
vis.resize(primes.size(), 0);
for(int i = 0; i < primes.size(); i++) {
for(int j = i + 1; j < primes.size(); j++) {
if(isprime(primes[j] - primes[i])) {
adj[i].pub(j);
adj[j].pub(i);
}
}
}
for(int i = 0; i < primes.size(); i++) {
if(primes[i] == a) {
ans.pub(a);
dfs(i);
break;
}
}
if(!done) cout << -1 << endl;
return 0;
}
Compilation message
lutrija.cpp: In function 'int main()':
lutrija.cpp:71:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < primes.size(); i++) {
~~^~~~~~~~~~~~~~~
lutrija.cpp:72:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i + 1; j < primes.size(); j++) {
~~^~~~~~~~~~~~~~~
lutrija.cpp:80:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < primes.size(); i++) {
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2088 ms |
528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
535 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2068 ms |
912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2092 ms |
404 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |