斯特林公式

Posted flyer_duck

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了斯特林公式相关的知识,希望对你有一定的参考价值。

斯特林公式(Stirling\'s approximation)是一条用来取n的阶乘近似值的数学公式。一般来说,当n很大的时候,n阶乘的计算量十分大,所以斯特林公式十分好用,而且,即使在n很小的时候,斯特林公式的取值已经十分准确。

链接:https://www.nowcoder.com/acm/contest/75/A
来源:牛客网

题目描述

夫夫有一天对一个数有多少位数感兴趣,但是他又不想跟凡夫俗子一样,
所以他想知道给一个整数n,求n!的在8进制下的位数是多少位。

输入描述:

第一行是一个整数t(0<t<=1000000)(表示t组数据)
接下来t行,每一行有一个整数n(0<=n<=10000000)

输出描述:

输出n!在8进制下的位数。
示例1

输入

3
4
2
5

输出

2
1
3
#include <bits/stdc++.h>
using namespace std;
 
const long double PI = 3.1415926535898;//acos(-1.0);
const long double E = 2.718281828459;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--) {
        scanf("%d", &n);
        if(n==0){
            printf("1\\n");
            continue;
        }
        long double ans = 0.5 * log10(2*PI*n)/log10(8)+ 1.0*n*log10(1.0*n/E)/log10(8);
            printf("%d\\n", (int)ans+1);
    }
    return 0;
}

  

以上是关于斯特林公式的主要内容,如果未能解决你的问题,请参考以下文章

51nod1130-斯特林公式

斯特林公式 (Stirling's approximation)

斯特林公式证明

斯特林公式

斯特林数的两个公式

Codeforces 717A Festival Organization(组合数学:斯特林数+Fibonacci数列+推公式)