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

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

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
  • Проголосовать: не нравится

»
23 месяца назад, # |
  Проголосовать: нравится 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
»
23 месяца назад, # |
  Проголосовать: нравится 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.

  • »
    »
    23 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I too have the same idea, just a different way to find, $$$gcd(n, m) | 2^{k}$$$, for some $$$k \in [0;59]$$$.

    • »
      »
      »
      23 месяца назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      I think this should be gcd(n,m) = $$$2^{k}$$$ for some k lying in [0,59].

  • »
    »
    23 месяца назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Sorry, but i don't have link to the problem. But, your approach seems correct to me...

    And yeah, I got if gcd(x,y)!=1 and x and y are odd then it's never possible. Since, any tranformation of any kind like (x,y) --> (x+y,y) or (x,x+y) doesn't change the gcd(x,y) which is in this case > 1