TransformTime limit:4000/2000ms (java/others) Memory limit:128000/64000kb (java/others) Submit statistic Next Problemproblem Description
One can transformxIntox+d IfDis divisor ofx. Find out the minimum number of steps to transform a into b.
Input
Integers a and b.
(1≤a,b≤5)
Output
The only integer denotes the minimum number of steps. Print −1if impossible.
Sample Input
1 6
Sample Output
3
Hint1 →2 →4 →6 or 1 & #x2192; 2 & #x2192; 3 & #x2192; 6 " > 1→2 →3 →6 Sourceftiaschmanagernanae Test instructions: The factor of x changing to X+d D is the number of steps that change from a to B at least the output-1: The processing of the BFS attention factor is handled if (GG.X<=B&&VI S[gg.x]==0) Note Two comparison of sequential vis array re multiple
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5#include <stack>6#include <cmath>7 #definell Long Long8 #definePi ACOs (-1.0)9 #defineMoD 1000000007Ten using namespacestd; One intb; A structnode - { - intx; the intStep; - }exm,gg; -Queue<node>Q; - intvis[100005]; + voidBFsintSt) - { +memset (Vis,0,sizeof(Vis)); A while(!q.empty ()) at Q.pop (); -exm.x=St; -exm.step=0; -vis[st]=1; - Q.push (EXM); - while(!q.empty ()) in { -exm=Q.front (); to Q.pop (); + if(exm.x==b) - { thecout<<exm.step<<Endl; * Break; $ }Panax Notoginseng for(intI=1; I<=sqrt (exm.x); i++) - { the if(exm.x%i==0) + { Agg.x=exm.x+i; thegg.step=exm.step+1; + if(gg.x<=b&&vis[gg.x]==0) - { $ Q.push (GG); $vis[gg.x]=1; - } -gg.x=exm.x+exm.x/i; thegg.step=exm.step+1; - if(gg.x<=b&&vis[gg.x]==0)Wuyi { the Q.push (GG); -vis[gg.x]=1; Wu } - } About } $ } - } - intMain () - { A while(SCANF ("%d%d", &a,&b)! =EOF) + { the if(a<=b) - BFS (a); $ Else thecout<<"-1"<<Endl; the } the return 0; the}
Acdream 1025 BFS