「UVA1185」Big Number 解题报告

UVA1185 Big Number

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!\)有几位数。

思路

我们考虑用log解决。

\(log_{10}ab=log_{10}a+log_{10}b\)

因此\(log_{10}n!=log_{10}1+log_{10}2+……+log_{10}n\)

\(floor(log_{10}n!)+1\)即为\(n!\)的位数。

代码

#include<bits/stdc++.h>
using namespace std;
#define Re register
#define MAXN 10000005
#define LD long double

int f[MAXN];
LD c;
int n, t;

int main(){
    for ( int i = 1; i <= 10000000; ++i ){
        c += log10(i);
        f[i] = (int)floor(c) + 1;
    }
    scanf( "%d", &n );
    while( n-- ){
        scanf( "%d", &t );
        printf( "%d\n", f[t] );
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/louhancheng/p/10299313.html