The Stirling formula is a mathematical formula used to approximate the n factorial. In general, when N is very large, the calculation of n factorial is very large, so the Stirling formula is very useful, and even when N is very small, the Stirling formula is very accurate:
When the Stirling formula is used to calculate the number of bits of the n! result, the logarithm can be taken on either side: log10 (n!) = log10 (2*pi*n)/2+n*log10 (n/e);
So the number of n! is log10 (2*pi*n)/2+n*log10 (n/e) +1 (note: When n=1, the result is 0)
Code: Floor (log10 (sqrt (2*pi*n)) +n*log10 (n/e)) +1;
#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<stack>#include<map>#include<vector>#definePI ACOs ((double)-1)#defineE exp (double (1))using namespacestd;intMain (void){ intn,ans,t; CIN>>T; while(t--) {scanf ("%d",&N); Ans= Floor (log10 (sqrt (2*pi*n)) +n*log10 (n/e)) +1; cout<<ans<<Endl; } return 0;}
Hangzhou Electric 1018 Factorial number