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!
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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!
Name |
---|
Any hints ??
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 :)
code is here ( works, AC in practice room :) )
Firstly Thank you so much and next for new line, you can use <br> tag.
Soooo many gcds. Very nice solution though.
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.