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进制下 ,就把lgn 换成logm(n)就可以了。 logm(n)的表示方法是 lgn / lgm。高中数学的知识。。 这里有个double向下取整精度的问题要注意。转换成int时候,要floor(算出来的位数 + 1e-7) + 1.

  最后得出位数的计算方式为:floor( logm(n) + logm(n - 1) + ...+ logm(1) + 1e-9 ) + 1.

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 const int N=1e-7;
 5 int a;
 6
 7 int second()    //总位数
 8 {
 9     double tmp=0.0;
10     for(int i=2; i<=a; i++)    tmp+=log10(i);
11     return floor(tmp+N)+1;
12 }
13 int main()
14 {
15     //freopen("input.txt", "r", stdin);
16     int t;
17     cin>>t;
18     while(t--)
19     {
20         scanf("%d",&a);
21         printf("%d\n",second());
22     }
23     return 0;
24 }

AC代码

时间: 2024-10-17 08:26:33

HDU 1018 Big Number (阶乘位数)的相关文章

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 (简单数学)

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

有个数学公式计算数的阶乘位数 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

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