Codeforces 1157A. Reachable Numbers Codeforces Round #555 (Div. 3)


Problem link

The key fact in this problem is that the answer is not very large (in fact, it's not greater than 91). Why is it so? Every 10 times we apply function f to our current number, it gets divided by 10 (at least), and the number of such divisions is bounded as O(logn).

So we can just do the following: store all reachable numbers somewhere, and write a loop that adds current number n to reachable numbers, and sets n=f(n) (we should end this loop when n already belongs to reachable numbers). The most convenient way to store reachable numbers is to use any data structure from your favourite programming language that implemenets a set, but, in fact, the constrains were so small that it was possible to store all reachable numbers in an array.

A solution in c++


Post a Comment

0 Comments