Блог пользователя aopo

Автор aopo, история, 4 года назад, По-английски

There is a grid of ($$$10^{18}$$$, $$$10^{18}$$$) blocks. The person standing on (i, j) block can move to (2i, j) or (i, 2j) or (i-j, j) or(i, j-i) inside the grid. If he starts from (1, 1) output "Yes" if he/ she could reach (m, n) block else output "No".

Ex: Input:

3

1 2

4 7

10 10

Output:

Yes

Yes

No

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Similar problem here. Might be a good idea to look for a rather "mathematical" solution, we can't just exhaustively search all cells.

Here is the time complexity I am thinking of
»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Seems the algorithm is simple:

while( n % 2 == 0 ) n /= 2;
while( m % 2 == 0 ) m /= 2;
printf( "%s\n", gcd( n, m) == 1 ? "Yes" : "No");

But link to the problem will be helpful, yes.