51nod 1130 阶乘长度 [Stirling公式]

来源:互联网 发布:最高人民法院网络拍卖 编辑:程序博客网 时间:2024/04/30 11:26

输出n!的位数

n! 约等于 根号下(2πn) * (n/e )^n
那么 res = 1/2*lg(2πn) + nlg(n/e) + 1
实测n很小也是正确的答案

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const double pi = M_PI;const double e = M_E;const double eps = 1e-7;typedef long long LL;inline LL get_res(int n){    if(n<=10)    {        int tot = 1;        for(int i=1;i<=n;i++) tot *= i;        return floor(log10(tot) + eps) + 1;    }    return floor(0.5*log10(2*pi*(double)n) + (double)n*log10((double)n/e) + eps) + 1;}int main(){    freopen("stirling.in","r",stdin);    freopen("stirling.out","w",stdout);    int cas;    scanf("%d",&cas);    while(cas--)    {        int n;        scanf("%d",&n);        LL ans = get_res(n);        printf(AUTO"\n",ans);    }    return 0;}

公式链接:
http://m.blog.csdn.net/article/details?id=51145807

Summary:
cmath里面有π和e的精确值!!!!!!

0 0
原创粉丝点击