Input
T 1<=t<=1000
X y
Output
How many starting points can walk N (n>=0) step to (x, y), only from (x, y) (X,Y+LCM (x, Y))/(X+LCM (x, y), y)
Standard solution: From (x,y0) to (x, y), then set X=AG,Y0=BG,G=GCD (X,Y0), there is y=bg+abg= (a+1) BG, because A, b coprime, A, (a+1) coprime, so A and (a+1) b coprime, so if you can go from (x,y0) to (X, Y), gcd (x,y0) =gcd (x, y), and then divide x and y gcd (x, y) by dividing it continuously (x+1)
1#include <iostream>2#include <cstdio>3#include <Set>4#include <algorithm>5 6 using namespacestd;7 8typedefLong LongLL;9 Ten //Beg Greatest common divisor Onell GCD (ll A, ll b) {if(!B)returnAElse returnGCD (b,a%b); } A - intMain () - { the intT,case =0; -scanf"%d",&t); - while(t--) - { +LL Ex,ey;//End coordinates - +scanf"%lld%lld",&ex,&ey); ALL GCD =gcd (Ex,ey); atEx/=gcd,ey/=GCD; - intAns =0; - while(1) - { - if(EY <ex) Swap (Ex,ey); -ans++; in if(EY% (ex+1)) Break; -EY/= (ex+1); to } + -printf"Case #%d:%d\n",++Case,ans); the } *}
Answer
1#include <cstdio>2#include <queue>3#include <cstring>4#include <iostream>5#include <cstdlib>6#include <algorithm>7#include <vector>8#include <map>9#include <Set>Ten#include <ctime> One#include <cmath> A#include <cctype> - #defineMAX 100000 - #defineLL Long Long the intcas=1, t,x,y,d[ +],dn; - voidFindintXint*d,int&DN) - { -dn=-1; + intm=sqrt (x); - for(intI=1; i<=m;i++)if(x%i==0) d[++dn]=i; + for(inti=dn;i>=0; i--) d[++dn]=x/D[i]; A } at intgcdintAintb) {returnb==0? A:GCD (b,a%b); } - intMain () - { - //freopen ("in", "R", stdin); -scanf"%d",&T); - while(t--) in { -scanf"%d%d",&x,&y); to intstep=1, flag=1; + while(flag&&x!=y) - { theflag=0; * if(x>y) std::swap (x, y); $ intg=gcd (x, y);Panax Notoginseng find (G,D,DN); - for(inti=dn;i>=0; i--) the { + if(y% (d[i]+x) = =0) A { the inty1=y/(d[i]+x) *D[i]; + if(GCD (X,Y1) ==d[i]) {y=y1;step++;flag=1; Break; } - } $ } $ } -printf"Case #%d:%d\n", cas++, step); - } the //printf ("Time=%.3lf", (double) clock ()/clocks_per_sec); - return 0;Wuyi}
My Code
HDU 5584 gcd/lcm/Mathematical formula