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

输出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"
#endif
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值