It's useless to use a good formula ... Think in fact very simple, first of all should be analyzed, because each add an LCM is greater than or equal to any one of the number, then my LCM plus which number above, that number will become big, so think, we know, each (x, y) corresponds to a situation.
The second point is that the formula, we can think about, is not the number can be split, we found
A=x*k,b=y*k, where K=GCD (A, A, b) and X and Y coprime, so that we can push (x*k,y*k) to (x*k,x*y+x*y*k), simplification is k * (x, (x+1) *y), GCD is still k, in turn, we just need to ensure that The previous step, the large value satisfies (b>a) b% (a+1) ==0 and gcd (A, b) =k. Continuous iterative process
#include <iostream>#include<string.h>#include<algorithm>#include<stdio.h>using namespacestd;intCNT;intgcdintXinty) { returnx%y==0? Y:GCD (y,x%y);}voidSolveintXinty) { if(x>y) {Swap (x, y); } if(Y% (x+1)){ return ; }Else{cnt++; Solve (x, y/(x+1)); }}intMain () {intT; intEx,ey; scanf ("%d",&t); intz=0; while(t--) {Z++; CNT=1; scanf ("%d%d",&ex,&ey); intx=Max (Ex,ey); inty=min (Ex,ey); intk=gcd (Ex,ey); X/=k;y/=K; Solve (x, y); printf ("Case #%d:%d\n", z,cnt); } return 0;}
Hdu 5584 LCM Walk