Editorial for ICPC NAQ 2016 G - Inverse Factorial
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
- It is guaranteed that the number of digits of
is at most
. This implies that
(found by experimentation).
- A naive approach computing factorials will be too slow, due to the overhead of big integer arithmetic.
- Instead we can notice that, when
, each factorial can be uniquely identified by its length (i.e. number of digits).
- The length of an integer
can be computed as
.
- Let
- Then the length of
is
.
- Using the fact that
, we can successively compute
, until we find the factorial with the required length.
- Each step takes
time, and the answer will be found in at most
steps.
- Handle
as special cases. As
, make sure you output
when the input is
(the output should be positive).
Comments