In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This x is an integer number which is a prime factor of A (please note that 1 and A are not being considered as a factor of A). Now, your task is to find the minimum number of transformations required to transform s to another integer number t.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case contains two integers: s (1 ≤ s ≤ 100) and t (1 ≤ t ≤ 1000).
Output
For each case, print the case number and the minimum number of transformations needed. If it's impossible, then print -1.
Sample Input
2
6 12
6 13
Sample Output
Case 1: 2
Case 2: -1
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case contains two integers: s (1 ≤ s ≤ 100) and t (1 ≤ t ≤ 1000).
Output
For each case, print the case number and the minimum number of transformations needed. If it's impossible, then print -1.
Sample Input
2
6 12
6 13
Sample Output
Case 1: 2
Case 2: -1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int pri[1010],a,b,dis[1010];
int prime()
{
int f=0;
memset(pri,0,sizeof(pri));
for(int i=2; i<1010; i++)
{
f=0;
for(int j=2; j<=sqrt(i); j++)
{
if(i%j==0)
{
f=1;
pri[i]=1;
}
}
if(f==0)pri[i]=0;
}
}
void bfs(int a,int b)
{
memset(dis,INF,sizeof(dis));
queue<int>q;
q.push(a);
dis[a]=0;
while(!q.empty())
{
int v=q.front();
q.pop();
if(v==b)return ;
for(int i=2; i<v; i++)
{
if(v%i==0&&pri[i]==0)
{
if(v+i>b)break;
if(dis[v+i]>dis[v]+1)
{
dis[v+i]=dis[v]+1;
q.push(v+i);
}
}
}
}
}
int main()
{
int i,t,p=1;
prime();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
bfs(a,b);
if(dis[b]!=INF)
printf("Case %d: %d\n",p++,dis[b]);
else
printf("Case %d: -1\n",p++);
}
}