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
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- using namespace std;
- #define MAX 0x3f3f3f3f
- typedef struct{
- int step;
- int now;
- }Node;
- int s, t;
- bool prime[1010];
- int marks[1001];
- int BFS(){
- queue<Node> q;
- Node start;
- start.now = s;
- start.step = 0;
- q.push( start );
- memset( marks, 0x3f, sizeof( marks ) );
- int ans = MAX;
- while( !q.empty() ){
- Node n = q.front();
- q.pop();
- if( n.now == t ){
- ans = min( ans, n.step );
- continue;
- }
- for( int i = 2; i < n.now; i++ ){
- if( n.now % i != 0 || !prime[i] ){
- continue;
- }
- if( n.now + i > t ){
- continue;
- }
- if( marks[n.now+i] > n.step + 1 ){
- Node temp;
- temp.now = n.now + i;
- temp.step = n.step + 1;
- marks[n.now+i] = temp.step;
- q.push( temp );
- }
- }
- }
- if( ans < MAX ){
- return ans;
- }
- return -1;
- }
- int main(){
- int T, Case = 1;
- memset( prime, true, sizeof( prime ) );
- for( int i = 2; i <= 1000; i++ ){
- for( int j = 2; i * j <= 1000; j++ ){
- prime[i*j] = false;
- }
- }
- cin >> T;
- while( T-- ){
- cin >> s >> t;
- cout << "Case " << Case++ << ": " << BFS() << endl;
- }
- return 0;
- }