poj1423

题意:给出n,求n!的位数。

分析:首先想到的是用log10(n!)=log10(1)+log10(2)+...+log10(n)。但是由于log10运行时间较长,会超时,所以可以先将log10(1)~log10(maxn)存入数组,则省去了多次调用的时间浪费。但是有一种更好的做法就是利用stirling公式。

stirling公式:lim(n→∞) (n/e)^n*√(2πn) / n! = 1

虽然本题中的n并不是正无穷,但是求出的n!的位数还是不会出现误差的。所以我们直接以此公式整理出log10(n!)=log10(sqrt(2 * acos(-1) * n)) + n * log10(n / exp(1.0));

View Code
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

#define eps 1e-8

int main()
{
    //freopen("t.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        double ans = log10(sqrt(2 * acos(-1) * n)) + n * log10(n / exp(1.0));
        printf("%d\n", (int)ans + 1);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值