# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
578916 | AGE | Lutrija (COCI19_lutrija) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define F first
#define S second
#define int long long
#define pb push_back
using namespace std;
const int N=6e7,M=2e3,mod=1e9+7;
map<int,int>dist,p,okk;
map<int,vector<int>>adj;
int seive[N];
vector<int>primess;
bool is_prime(int x){
for(int j=0;j<primess.size();j++){
if(primess[j]>x)
continue;
if(x%primess[j]==0)
return 0;
}
return 1;
}
main(){
for(int i=2;i<=3e7;i++){
if(seive[i]==1)
continue;
for(int j=i+i;j<=3e7;j+=i)
seive[j]=1;
}
for(int i=2;i<=3e7;i++)
if(seive[i]==0)
primess.pb(i);
vector<int>v;
int a,b;
cin>>a>>b;
v.pb(a);
v.pb(b);
v.pb(a-2);
v.pb(a+2);
v.pb(b-2);
v.pb(b+2);
for(int i=0;i<v.size();i++){
if(is_prime(v[i])==0||v[i]==0||v[i]==1)
continue;
for(int j=0;j<v.size();j++){
if(v[i]==v[j]||(is_prime(v[j])==0)||v[j]==0||v[j]==1)
continue;
int x=abs(v[i]-v[j]);
if((is_prime(x)==0)||x==0||x==1)
continue;
adj[v[i]].pb(v[j]);
adj[v[j]].pb(v[i]);
}
}
queue<int>q;
q.push(a);
dist[a]=1;
while(!q.empty()){
int node=q.front();
q.pop();
for(auto x:adj[node])
if(!dist[x])
dist[x]=1,q.push(x),p[x]=node;
}
if(dist[b]==0){
cout<<"-1"<<endl;
continue;
}
int node=b;
vector<int>ans;
while(node!=a){
ans.pb(node);
node=p[node];
}
ans.pb(a);
reverse(ans.begin(),ans.end());
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
cout<<endl;
return 0;
}