When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

aopo's blog

By aopo, history, 18 months ago, In English

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

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    18 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      18 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    18 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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