HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number【阶乘位数】

Big Number
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28524   Accepted: 9075

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

Dhaka 2002

Regionals 2002 >> Asia - Dhaka


问题链接:HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number

问题简述:(略)

问题分析

  这是一个数论题,可以根据题意用暴力法来求解,也可以使用来Stirling公式求解。

  暴力法:N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N)。

程序说明

  暴力法在POJ中出现TLE。

题记:(略)

 

AC的C++语言程序如下

/* HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number(根据Stirling公式计算) */

#include 
#include 
#include 

using namespace std;

const double PI = acos(-1.0);
const double LN10 = log(10.0);

double stirling(int n)
{
    return ceil((n * log(double(n)) - n + 0.5 * log(2.0 * n * PI)) / LN10);
}

int main()
{
    int t, n;

    scanf("%d", &t);
    while(t--) {
        scanf("%d",&n);

        printf("%d\n", n <= 1 ? 1 : (int)stirling(n));
    }

    return 0;
}


AC的C语言程序如下

/* HDU1018 POJ1423 UVALive2697 UVA1185 ZOJ1526 Big Number(暴力法) */

#include 
#include 

int main(void)
{
    int t, n, i;
    double ans;

    scanf("%d", &t);
    while(t--) {
        scanf("%d",&n);

        ans = 0.0;
        for(i=1; i<=n; i++)
            ans += log10(i);

        printf("%d\n", (int)ans + 1);
    }

    return 0;
}




你可能感兴趣的:(#,ICPC-备用二,#,ICPC-数论,#,ICPC-HDU,#,ICPC-POJ,#,ICPC-UVA,#,ICPC-UVALive,#,ICPC-ZOJ)