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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 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.