How can we use the concept of GCD to solve this problem?

  • Context: MHB 
  • Thread starter Thread starter Saitama
  • Start date Start date
  • Tags Tags
    Gcd
Click For Summary
SUMMARY

The discussion centers on the application of the Greatest Common Divisor (GCD) in solving the "Traversing Grid" problem from CodeChef. Participants explore how the GCD remains invariant under specific operations, particularly the operation (x, y) → (x + y, y). It is established that points reachable from the initial coordinates (3, 5) must have a GCD that is a power of 2 multiplied by the GCD of the starting point. The conclusion emphasizes that any point with a GCD not of this form is unreachable.

PREREQUISITES
  • Understanding of GCD (Greatest Common Divisor) and its properties
  • Familiarity with coordinate systems and grid traversal
  • Basic knowledge of mathematical operations on pairs of numbers
  • Experience with algorithmic problem-solving techniques
NEXT STEPS
  • Study the properties of GCD and its applications in number theory
  • Learn about grid traversal algorithms and their optimizations
  • Explore mathematical operations that preserve GCD values
  • Practice solving similar problems on CodeChef or other competitive programming platforms
USEFUL FOR

Mathematicians, computer scientists, algorithm developers, and competitive programmers seeking to enhance their understanding of GCD applications in problem-solving.

Saitama
Messages
4,244
Reaction score
93
Hello all!

I was trying to solve this problem which is supposed to be easy: Traversing Grid | CodeChef

I couldn't solve it so I looked at some solutions, they seem to be using the concept of GCD. I am unable to understand why this works, please help. Thanks!
 
Technology news on Phys.org
I would try and plot all the points near the origin reachable from, say, (3, 5) and see if you can find a pattern. That won't be part of your solution, but it'll help you understand where the GCD comes in.
 
Bacterius said:
I would try and plot all the points near the origin reachable from, say, (3, 5) and see if you can find a pattern. That won't be part of your solution, but it'll help you understand where the GCD comes in.

Sorry, but I do not really see it. I found the following points reachable from (3,5) near the origin: (2,3), (1,3), (1,-3), (2,-3), (-1,3), (-2,3), (-2,-3), (-1,-3), (1,2), (1,-2), (0,2), (0,-2)... but no matter, I can never reach the origin. I am still clueless about this, please help!
 
Pranav said:
Sorry, but I do not really see it. I found the following points reachable from (3,5) near the origin: (2,3), (1,3), (1,-3), (2,-3), (-1,3), (-2,3), (-2,-3), (-1,-3), (1,2), (1,-2), (0,2), (0,-2)... but no matter, I can never reach the origin. I am still clueless about this, please help!

Make a grid and draw the points reachable and those not reachable..
 
Bacterius said:
Make a grid and draw the points reachable and those not reachable..

I drew a grid for -3<=x<=3 and -5<=y<=5 and couldn't reach the following points: (0,0), (0,3), (0,-3), (3,3), (3,-3), (-3,-3), (-3,3), (0,5) and (0,-5). But I still do not see how this helps. :(
 
Pranav said:
I drew a grid for -3<=x<=3 and -5<=y<=5 and couldn't reach the following points: (0,0), (0,3), (0,-3), (3,3), (3,-3), (-3,-3), (-3,3), (0,5) and (0,-5). But I still do not see how this helps. :(

What's the $\gcd$ of each of those reachable versus unreachable pairs? (Wondering)
 
I like Serena said:
What's the $\gcd$ of each of those reachable versus unreachable pairs? (Wondering)

$\gcd$ of unreachable pairs is $0, 3, 5$ and for the reachable ones, $1, 2$ (as per the above grid). But I am still stumped. :confused:
 
Pranav said:
$\gcd$ of unreachable pairs is $0, 3, 5$ and for the reachable ones, $1, 2$ (as per the above grid). But I am still stumped. :confused:

The initial point has $\gcd(3,5)=1$.
What does the $\gcd$ become after each of the operations?
 
I like Serena said:
The initial point has $\gcd(3,5)=1$.
What does the $\gcd$ become after each of the operations?

$\gcd$ stays the same i.e 1 after each operation.
 
  • #10
Pranav said:
$\gcd$ stays the same i.e 1 after each operation.

Almost.
How did you reach a point with $\gcd=2$ then?

Anyway, do you see that none of the operations can bring you to a point with a $\gcd$ of $3$ or $5$?

Btw, you mentioned that some points had $\gcd=0$.
Can you give an example?
Does it really have a $\gcd$ of $0$?
 
  • #11
I like Serena said:
Almost.

Umm...is my answer incorrect? You asked for the $\gcd$ if I apply each of the operations once to (3,5). So, in one operation, I can reach (8,5), (6,5), (5,3) or (3,-5) and all of them have $\gcd$ as 1.

How did you reach a point with $\gcd=2$ then?
For example, (3,5)->(6,5)->(6,-5)->(1,-5)->(1,5)->(5,1)->(5,-1)->(4,-1)->(1,4)->(2,4).

Anyway, do you see that none of the operations can bring you to a point with a $\gcd$ of $3$ or $5$?
Yes, I do but how does that help? :confused:

Btw, you mentioned that some points had $\gcd=0$.
Can you give an example?
Does it really have a $\gcd$ of $0$?

Really sorry about that, when I was calculating the gcds, I accidentally considered (0,0) as a reachable point. (Tmi)
 
  • #12
Pranav said:
Umm...is my answer incorrect? You asked for the $\gcd$ if I apply each of the operations once to (3,5). So, in one operation, I can reach (8,5), (6,5), (5,3) or (3,-5) and all of them have $\gcd$ as 1.

For example, (3,5)->(6,5)->(6,-5)->(1,-5)->(1,5)->(5,1)->(5,-1)->(4,-1)->(1,4)->(2,4).

Yes, I do but how does that help? :confused:

Ah. I meant repeated application of the operations.
Note that for any $x,y$ we have: $\gcd(x,y)=\gcd(y,x)=\gcd(x,-y)=\gcd(x+y,y)$.
So if we keep repeating any of the first 3 operations, the $\gcd$ will always remain the same.
The odd one out is the 4th operation. The question remains what is can do with the $\gcd$.
A single application to (3,5) won't affect the $\gcd$ yet, but there are ways that it can.

Really sorry about that, when I was calculating the gcds, I accidentally considered (0,0) as a reachable point. (Tmi)

What is $\gcd(0,0)$?
And what is $\gcd(2,0)$?
 
  • #13
What is $\gcd(0,0)$?
And what is $\gcd(2,0)$?

$0$ and $2$ respectively but I am still clueless about solving the given problem. :(
 
  • #14
Pranav said:
$0$ and $2$ respectively but I am still clueless about solving the given problem. :(

The 4th operation combined with the 1st operation can multiply the $\gcd$ with $2$.
So the $\gcd$ can only be of the form $2^k\cdot \gcd(x_0,y_0)$.
Any point with a $\gcd$ that is not of that form is not reachable.

Btw, $\gcd(0,0)$ is undefined instead of $0$. (Nerd)
 
  • #15
I like Serena said:
The 4th operation combined with the 1st operation can multiply the $\gcd$ with $2$.
So the $\gcd$ can only be of the form $2^k\cdot \gcd(x_0,y_0)$.
Any point with a $\gcd$ that is not of that form is not reachable.

Btw, $\gcd(0,0)$ is undefined instead of $0$. (Nerd)

Thanks for all the help ILS!

But still, how one is supposed to know that the concept of gcd is to be used in this problem? I had no idea until I looked at the solutions.
 
  • #16
Pranav said:
Thanks for all the help ILS!

But still, how one is supposed to know that the concept of gcd is to be used in this problem? I had no idea until I looked at the solutions.

We are looking for a property of a pair of numbers that the operations will leave unchanged.
The $\gcd$ is a natural property to consider.Furthermore, the given operations give us a clue, since they match the properties of a $\gcd$.

In particular the operation $(x,y) \mapsto (x+y,y)$ looks like the key concept to evaluate a $\gcd$, which is that adding or subtracting one of the numbers from the other, leaves the $\gcd$ unchanged.
It leads to the algorithm:
\begin{cases}\gcd(x,x) &= x \\
\gcd(x,y) &= \gcd(x - y,y) & \text{if }x > y \\
\gcd(x,y) &= \gcd(x, y-x) & \text{if }y > x \end{cases}
 

Similar threads

Replies
1
Views
3K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 43 ·
2
Replies
43
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
9
Views
3K