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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.