• POJ1423 计算出n的阶乘的位数大数问题[Stirling公式]


    Big Number
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 21661   Accepted: 6888

    Description

    In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

    Input

    Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 <= m <= 10^7 on each line.

    Output

    The output contains the number of digits in the factorial of the integers appearing in the input.

    Sample Input

    2
    10
    20

    Sample Output

    7
    19

    Source

     
    题意:计算出n的阶乘的位数
     
     1 #include <stdio.h>
     2 #include <math.h>
     3 
     4 int n;
     5 const double e = 2.7182818284590452354, pi = 3.141592653589793239;
     6 
     7 double f( int a )
     8 {
     9     return log10( sqrt( 2 * pi * a ) ) + a * log10( a / e );
    10 }
    11 
    12 int main()
    13 {
    14     int cas, ans;
    15     double i, s;
    16     
    17     scanf( "%d", &cas );
    18     
    19     while( cas-- )
    20     {
    21         scanf( "%d", &n );
    22         if( n < 100000 )
    23         {
    24             for( s=0, i=1; i<=n; i++ )
    25                 s += log10( i );
    26         }
    27         else s = f( n );
    28         ans = (int)s;
    29         if( ans <= s )
    30             ans++;
    31         
    32         printf( "%d/n", ans );
    33     }    
    34     return 0;
    35 }
     
    注:
    Stirling公式介绍:
    lim(n→∞) (n/e)^n*√(2πn) / n! = 1
    也就是说当n很大的时候,n!与√(2πn) * (n/e) ^ n的值十分接近
    这就是Stirling公式.
     
    公式的证明
    令a(n)=n! / [ n^(n+1/2) * e^(-n) ]
     
      则a(n) / a(n+1) = (n+1)^(n+3/2) / [ n^(n+1/2) * (n+1) * e ]
     
      =(n+1)^(n+1/2) / [ n^(n+1/2) * e]
     
      =(1+1/n)^n * (1+1/n)^1/2 *1/e
     
      当n→∞时,(1+1/n)^n→e,(1+1/n)^1/2→1
     
      即lim(n→∞) a(n)/a(n+1)=1
     
      所以lim(n→∞)a(n) 存在
     
      设A=lim(n→∞)a(n)
     
      A=lim(n→∞)n! / [ n^(n+1/2) * e^(-n) ]
     
      利用Wallis公式,π/2 = lim(n→∞)[ (2n)!! / (2n-1)!! ]^2 / (2n+1)
     
      π/2 = lim(n→∞)[ (2n)!! / (2n-1)!! ]^2 / (2n+1)
     
      =lim(n→∞)[ (2n)!! * (2n)!! / (2n)! ]^2 / (2n+1)
     
      =lim(n→∞) 2^(4n) [ (n!)^2 / (2n)! ]^2 / (2n+1)
     
      =lim(n→∞) 2^(4n) [ (A * n^(n+1/2) * e^(-n) )^2 / (A * (2n)^(2n+1/2) * e^(-2n) )]^2 / (2n+1)
     
      =lim(n→∞) 2^(4n) [ 2^(-2n-1/2) * A * √n ]^2 / (2n+1)
     
      =lim(n→∞) 2^(4n) * A^2 * 2^(-4n-1) * n/(2n+1)
     
      =A^2 / 4
     
      所以A=√(2π)
        
         lim(n→∞)n! / [ n^(n+1/2) * e^(-n) ] = √(2π)
        
         即lim(n→∞) √(2πn) * n^n * e^(-n) / n! = 1
     
    公式的意义:
     Stirling公式的意义在于:当n足够大之后n!计算起来十分困难,虽然有很多关于n!的不等式,但并不能很好的对阶乘结果进行估计,尤其是n很大之后,误差将会非常大.但利用Stirling公式可以将阶乘转化成幂函数,使得阶乘的结果得以更好的估计.而且n越大,估计得就越准确.






                If you have any questions about this article, welcome to leave a message on the message board.



    Brad(Bowen) Xu
    E-Mail : maxxbw1992@gmail.com


  • 相关阅读:
    LA 3938 动态最大连续和 线段树
    UVA 11235 频繁出现的数值 RMQ
    LA 4329 ping-pong树状数组
    LA 3027 合作网络 并查集
    vue-url传参
    vue打开新页面的几种方式
    父子组件传参
    vue--按钮级别的权限控制+插件的使用
    小程序填坑
    webpack小试牛刀
  • 原文地址:https://www.cnblogs.com/XBWer/p/2513823.html
Copyright © 2020-2023  润新知