【HDU 1018】Big Number —— n!的位数

原题链接

解题报告:

由于最大能达到10^7! 故而不能直接用高精度整数算出结果,然后取位数……所以转换思路,从数学角度算出n!的位数,推导如下:

因为len(n)=floor( log10(n) )+1

设A=n!=1*2*3*...*n

所以len(A)-1=floor( log10(A) )=floor( log10(1*2*3*...*n) )=floor( log10(1)+log10(2)+log10(3)+...log10(n)
)

故而len(A)=1+floor( log10(1)+log10(2)+log10(3)+...log10(n) )

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int T,n;
    double ans;
    cin>>T;
    while(T--){
        cin>>n;
        ans=1.0;
        for(int i=1;i<=n;++i){
            ans += log10(i);
        }
        cout<<int(ans)<<endl;
    }
}
时间: 2024-07-29 15:58:21

【HDU 1018】Big Number —— n!的位数的相关文章

HDU 1018 Big Number (阶乘位数)

题意:给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路:用log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w = lg103 = 3. 因此只要求lgn 向下取整 +1就是位数.然后因为阶乘比如5阶乘的话是5 * 4 * 3 * 2 * 1.位数就满足lg 5 * 4 * 3 * 2 * 1 = lg5 + lg4 + lg3 + lg2 + lg1.用加法就不会超过数字上限. 当然这是十进制下得.如果是m进制下 ,就把lg

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

HDU 1018 Big Number 数学题解

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

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 ver

HDU 1018 Big Number (log函数求数的位数)

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

HDU 1018:Big Number (位数递推公式)

Problem DescriptionIn 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

HDU 1018 Big Number 数学题

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

HDU 1018.Big Number【7月25】

Big Number 这道题,没有什么难点什么的,就是为了涨姿势. 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, y

HDU 1018 Big Number

有个数学公式计算数的阶乘位数 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include<cmath> 5 #define pi 3.141592653 6 #define E 2.718281828 7 using namespace std; 8 9 int main() 10 { 11 12 freopen("C:\\Users\\super\\Docum