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

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

Hi dear community!

last day I was solving a GCD problems, it was easy for a,b <= 10^5 but when the limits change to a,b <=10^9, I got TLE how can I optimize this? what that it mean GDC(a,b)=GCD(b,a%b)? and how can I apply this to solve the problem?.

thanks in advanced

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

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

Consider the GCD of 10 and 45. Whatever it is, it divides 10. So if we know the GCD of 10 and 5, then we know that is also the GCD of 10 and 45. After all, if it divides 10, it also divides 40, so we can easily add 40 to 5 to get 45, without removing relevant factors.

If you use this algorithm, you can find the GCD of two numbers in log(n) time. A good demonstration of that can be seen here: https://m.youtube.com/watch?v=JUzYl1TYMcU

(that was the first YouTube video result after searching GCD)