can someone explain how to do this problem http://mirror.codeforces.com/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 ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3803 |
2 | jiangly | 3707 |
3 | Benq | 3627 |
4 | ecnerwala | 3584 |
5 | orzdevinwang | 3573 |
6 | Geothermal | 3569 |
6 | cnnfls_csy | 3569 |
8 | Radewoosh | 3542 |
9 | jqdai0815 | 3532 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 164 |
2 | awoo | 163 |
3 | adamant | 162 |
4 | maroonrk | 152 |
5 | -is-this-fft- | 151 |
6 | nor | 150 |
7 | atcoder_official | 147 |
8 | TheScrasse | 146 |
9 | Petr | 145 |
10 | SecondThread | 142 |
Название |
---|
read about Euclidean algorithm.
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.