zoukankan      html  css  js  c++  java
  • 斯特林(Stirling)公式 求大数阶乘的位数


    我们知道整数n的位数的计算方法为:log10(n)+1
    n!=10^m
    故n!的位数为 m = log10(n!)+1

    lgN!=lg1+lg2+lg3+lg4+lg5+....................+lgN;

    但是当N很大的时候,我们可以通过数学公式进行优化:(即Stirling公式)

    N!=sqrt(2*pi*N)*(N/e)^N;(pi=3.1415926=acos(-1.0),e=exp(1))

    lgN!=(lg(2*pi)+lgN)/2+N*(lgN-lge);

    斯特林公式可以用来估算某数的大小结合lg可以估算某数的位数,或者可以估算某数的阶乘是另一个数的倍数。

    例题

    https://www.nowcoder.net/acm/contest/75/A

    题意 求解n的阶乘八进制下的位数

    n!=8^res     n!=e^m

    res=log8(n!)   m=loge(n!)

    log8(n!)= loge(n!)/loge(8)  res = m/loge(8)换底公式

    m=loge(2*pi*n)/2+n*loge(n/e)

    AC代码

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <map>
    #include <vector>
    using namespace std;
    const int maxn = 1e6+10;
    const int inf = 0x3f3f3f3f;
    const double e = exp(1);
    const double pi = acos(-1.0);
    const double epx = 1e-10;
    typedef long long ll;
    int main()
    {
        ll t;
        scanf("%lld",&t);
        while(t--){
            ll n;
            scanf("%lld",&n);
            if(n==0||n==1){
                puts("1");
                continue;
            }
            double res = (log(2*pi*n)/2.0+n*log(n/e))/log(8)+1;
            printf("%lld
    ",(ll)res);
        }
        return 0;
    }
  • 相关阅读:
    HTML5之标签
    Linux常用命令(二十四)
    Linux常用命令(二十三)
    Python 定位excel表格的最后一个单元格的位置
    tornado学习
    Linux一些基础命令
    用python打造自己的SDK--使用setuptools打包安装
    Centos开放指定端口命令
    python sqlalchemy基本使用
    python rpc
  • 原文地址:https://www.cnblogs.com/stranger-/p/8413794.html
Copyright © 2011-2022 走看看