LightOJ

来源:互联网 发布:帆布鞋淘宝描述 编辑:程序博客网 时间:2024/06/14 03:30

Description

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

Hint

题意

题解:

AC代码

#include<cstdio>#include<cstring>#include<stack>#include <set>#include <queue>#include <vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int N  = 1e3+100;int notprime[N];int vis[N];int s,b,ans;vector<int > yinz[N];struct node { int num,step;};void init(){    notprime[0] = notprime[1] = 1;    for (int i = 2;i < N ; ++i){        if (notprime[i]) continue;        for (int j = i*2; j<N; j+=i){            notprime[j] = 1;        }    }    for (int i = 2; i < N ;  ++i){        for (int j = 2; j < i; ++j){            if (notprime[j]==0&&i%j==0) yinz[i].push_back(j);        }    }}void bfs(){    memset(vis,0,sizeof(vis));    node x,y;    x.num = s;    x.step = 0;    queue<node> q;    while (!q.empty()) q.pop();    q.push(x);    vis[s] = 1;    while (!q.empty()){        x = q.front();        q.pop();        if (x.num==b)        {            ans = x.step;            break;        }        for (int i = 0;i < yinz[x.num].size(); ++i){            y.num = x.num+yinz[x.num][i];            if (y.num<=b&&vis[y.num]==0)            {                vis[y.num] = 1;                y.step = x.step+1;                q.push(y);            }        }    }}int main(){    init();    int t;    scanf("%d",&t);    int kase = 1;    while (t--){        scanf("%d%d",&s,&b);        printf("Case %d: ",kase++);        /*if (s>t)        {            printf("-1\n");            continue;        }*/        ans = -1;        bfs();        printf("%d\n",ans);    }    return 0;}