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.
  • It is guaranteed that the number of digits of n! is at most 106. This implies that n<3105 (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 n10, each factorial can be uniquely identified by its length (i.e. number of digits).
  • The length of an integer x can be computed as log10(x)+1.
  • Let Lk:=log10(k!)=log10(i=1ki)=i=1klog10(i)
  • Then the length of k! is Lk+1.
  • Using the fact that Lk+1=Lk+log10(k+1), we can successively compute L1,L2,, until we find the factorial with the required length.
  • Each step takes O(1) time, and the answer will be found in at most 3105 steps.
  • Handle n<10 as special cases. As 0!=1!=1, make sure you output 1 when the input is 1 (the output should be positive).

Comments

There are no comments at the moment.