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

Автор looool, 15 лет назад, По-английски
can someone explain how to do this problem http://codeforces.me/contest/134/problem/B
i used dfs that resulted in TLE, number theory tag is given at hints, but i am not able to understand how to do it ?
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

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

read about Euclidean algorithm.

  • »
    »
    15 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +6 Проголосовать: не нравится

    when you go forward, from any vertex (a, b) you can go into 2 other vertices: (a, a + b) or (a + b, b), but when you go backward at any step there is only one vertex to move in: (a, b - a) if b > a, or (a - b, b) if a > b

    so, solution is simple bruteforce, and it works fast.