-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbien_doi_snt.cpp
More file actions
64 lines (63 loc) · 1008 Bytes
/
bien_doi_snt.cpp
File metadata and controls
64 lines (63 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
#define PB push
const int limit=1e5;
bool F[limit+5]={};
int n,m;
void sang()
{
F[0]=F[1]=1;
for(int i=2;i*i<limit;i++)
{
if(!F[i])
for(int j=i*i;j<limit;j+=i) F[j]=1;
}
}
int solve()
{
if(n==m)return 0;
queue<int> q;queue<int> b;
int a[5]={};
bool check[limit+5]={};
q.PB(n);b.PB(0);
while(!q.empty())
{
int u=q.front();
//cout<<u<<endl;
check[u]=1;
a[3]=u%10;a[2]=(u/10)%10;a[1]=(u/100)%10;a[0]=(u/1000);
for(int j=0;j<10;j++)
{
for(int i=0;i<4;i++)
{
if(!i&&!j)continue;
int sum=0;
for(int k=0;k<4;k++)
{
sum*=10;
if(k==i)sum+=j;
else sum+=a[k];
}
if(sum==m){
return b.front()+1;
}
if(!F[sum]&&!check[sum])
{
q.PB(sum);b.PB(b.front()+1);
}
}
}
b.pop(); q.pop();
}
}
int main()
{
sang();
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
cout<< solve()<<endl;
}
}