zoukankan      html  css  js  c++  java
  • HDU 1018 -- Big Number (Stirling公式求n!的位数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018

    Big Number

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 40551    Accepted Submission(s): 19817


    Problem 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 ≤ n ≤ 107 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
     
     
    题意:求n!有多少位。
     
    解析:Stirling公式:n! ≈ √(2πn) * n^n * e^(-n);

                故log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(10);
     
    n!的位数=log10(n!) + 1, 故加上ceil();
     
    若要求其他进制的位数而非十进制,加个换底公式即可,即把最后的log(10)改成log(进制数);
     
    注意n为0和1时的特判;
     
    0!= 1;
     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 int Stirling(int n) {
     5     return ceil((n * log(double(n)) - n + 0.5 * log(2.0 * n * acos(-1.0))) / log(10.0));
     6 }
     7 
     8 int main() {
     9     int t, n;
    10     cin >> t;
    11     while (t--) {
    12         cin >> n;
    13         cout << (n <= 1 ? 1 : Stirling(n)) << endl;
    14     }
    15     return 0;
    16 }
    View Code
    作者:_kangkang
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    n的阶乘
    二叉树遍历
    二分查找练习
    字符串中最长回文序列求解
    复数集合
    AppCrawler自动化遍历使用详解(版本2.1.0 )(转)
    谷歌驱动下载链接
    谷歌浏览器插件
    Pycharm破解方法
    go学习链接
  • 原文地址:https://www.cnblogs.com/kangkang-/p/8419369.html
Copyright © 2011-2022 走看看