Hangzhou Electric 1018 Factorial number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.