achaitanyasai's blog

By achaitanyasai, 9 years ago, In English

Hello, I am trying to solve this problem Knight Of Integer Land but I could not get any idea. So can some one please give a hint/idea.
Thanks in advance!

  • Vote: I like it
  • +10
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Any hints ??

»
9 years ago, # |
  Vote: I like it +6 Vote: I do not like it

First, we find all pairs of x,y such that x^2 + y^2 is d and p,q is the point where we are in. For each pair, find its gcd. Let it be g.
There are two possibilities —

1.Both x/g and y/g are odd

2.Only one of them is odd

In the second case, we can add any number of g's to either p or q. So, we find all the pairs of second kind, find gcd of their gcds. Let that be a.

For first kind, let the gcd be g, Either we can add g to both p,q or we can add 2g to one of p,q

Thus we can take gcd of a with 2g of all gs in first kind.

Take gcd of all gs in first kind, call it b

Now, all the operations can be summarized into two operations — add b to both p,q or add a to one of p,q — To find whether we can reach x,y using these, we can check using the below code, it's an exercise to see why this works :)

if( (x-y)%a != 0)return "NO";
for(int k=0;k<a;k++)
{
	if( (x-b*k)%a==0)return "YES";
}
return "NO";

code is here ( works, AC in practice room :) )

»
9 years ago, # |
Rev. 3   Vote: I like it +6 Vote: I do not like it

Your movement will be determined by a set of vectors {[vx1, vy1], [vx2, vy2], [vx3, vy3]...}, such that vxi2 + vyi2 = d. Assume that g = gcd(vx1, vy1, vx2, vy2, vx3, vy3...) = 1 (if not, just divide all the vectors, as well as goal coordinates x and y by g, and you'll have an analogous problem (if x or y are not divisible by g, clearly there's no solution)).

Those vectors can be used in 8 ways (swapping vx with vy, and 2 possible signs for each coordinate). And since the gcd of all the vectors is 1, we can always adjust our movement to move 1 unit of distance in one direction and whatever in the other, arriving at (1, something). It also means we can do the same thing again, but this time changing the sign on the other coordinate, hence arriving at (2, 0).

So, if , we have a solution. If not, we can still find one if there's a vector (vx, vy) such that , because we can change the sign of either coordinate and make . If there's no such vector either, no solution exists.