Derivation of mathematical formulas
Method One:
①:10^m < n! <10^ (m+1) If M is obtained, then m+1 is the answer.
① the logarithm of 10 on both sides of the formula
M < log10 (n!) < m+1
Because Log10 (n!) =LOG10 (1) +log10 (2) +......+log10 (n)
Available loops to calculate the value of m+1
#include <iostream>#include<cstdio>#include<cmath>using namespacestd;intMain () {intT; CIN>>T; while(t--) { intN; CIN>>N; Doubleans=0; for(intI=1; i<=n;i++) {ans+=log (i)/log (Ten); } cout<< (int) ans+1<<Endl; } return 0;}
Method Two: Sterling formula
N! ≈SQRT (2*N*PI) * (n/e) ^n
then m+1= (int) (0.5*log (2.0*N*PI) +n*log (n)-N)/(log (10.0))) +1;
#include <iostream>#include<cstdio>#include<cmath>using namespacestd;Const DoublePi=3.1415926;intMain () {intT; CIN>>u; while(t--) { intN; CIN>>N; Doubleans; Ans=(0.5*log (2.0*N*PI) +n*log (n)-N)/(log (10.0)); cout<< (Long) ans+1<<Endl; } return 0;}
Hdoj (1018)