Official

A - Reverse and Minimize Editorial by evima


If \(f(K)\neq K\), the answer is \(0\).
Otherwise, it is the number of values at most \(N\) among the following:

  • \(K\) multiplied by \(1,10,100,\ldots\)
  • the integer obtained by reversing \(K\), multiplied by \(1,10,100,\ldots\)

We can start with \(K\) or its reversal, repeat multiplying it by \(10\), and quit when it exceeds \(N\) to check all those candidates in \(\mathrm O(\log N)\) time, which is fast enough.
Take care to remove duplication when the reversal of \(K\) coincides with \(K\).

posted:
last update: