I was practising the algorithm based programming problem.I am having difficulty,in solving this problem.I want idea (only small approach/hint)to solve this problm efficienty,so plz help me.!
Here Is the problem Statement::
Suppose there are two rabbits,named rabbit foo and rabbit bar.Initially both of them are located at the origin(center) facing each others.
Foo knows jump of only two lengths m,n.That is foo can jump either m length to his left or m length to his right or n length to his left or n length to his
right side in a single attempt.
Similarly bar also knows jump of only two lengths-p,q.That is bar can jump either p length to his left or p length to his right or q length to his left or q length to his
right side in a single attempt.
Now the master of these two rabbits want to place himself exactly at one point such that both of the rabbits will be able to reach his master in one or more attempts.
Also ,the master place himself at most L lengths away from the origin.
We have to calculate At how many position the master can place himself.
m,n,p,q and L are very large ,as larger as 10^17.
So how to solve it efficiently.
Example::
m=1 n=2
m=4 n=5
L=1
answer=3;
As
Foo can jumps 2 length to his right side and after that one length to his left side.
Bar can jumps 5 lengths to his rgt and after that four lenth to his left side.
to reach his master who is place 1 units away from the origin.
Foo 2 length left and after that one length right side.
Bar 5 length left and 5 length rgt to reach his master who located himself at 1 length away from origin
Master can also place himself at the origin since both foo and bar would be able to reach his master in two moves
so total positions=3.
Other Examples::
m=2 n=4
p=3 q=6
L=7
answer=3.
m=10 n=12
p=3 q=9
answer=5
Foo can reach any position that is a multiple of
gcd(m,n)and only those. Bar can reach the positions that are multiples ofgcd(p,q), so the positions reachable by both are exactly the multiples oflcm(gcd(m,n),gcd(p,q)).