Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 26383    Accepted Submission(s): 12006

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
求大数阶乘的位数。。用java暴力写的。。后来一想我真是脑残10^7不T才怪。看到公式才认为数学果然强大。
推导过程:
在这之前,我们必需要知道一个知识,随意一个正整数a的位数
等于(int)log10(a) + 1。为什么呢?以下给大家推导一下: 对于随意一个给定的正整数a。
如果10^(x-1)<=a<10^x,那么显然a的位数为x位,
又由于
log10(10^(x-1))<=log10(a)<(log10(10^x))
即x-1<=log10(a)<x
则(int)log10(a)=x-1,
即(int)log10(a)+1=x
即a的位数是(int)log10(a)+1 我们知道了一个正整数a的位数等于(int)log10(a) + 1,
如今来求n的阶乘的位数:
如果A=n!=1*2*3*......*n,那么我们要求的就是
(int)log10(A)+1,而:
log10(A)
=log10(1*2*3*......n) (依据log10(a*b) = log10(a) + log10(b)有)
=log10(1)+log10(2)+log10(3)+......+log10(n)
如今我们最终找到方法,问题攻克了。我们将求n的阶乘的位
数分解成了求n个数对10取对数的和,而且对于当中随意一个数,
都在正常的数字范围之类。 总结一下:n的阶乘的位数等于
(int)(log10(1)+log10(2)+log10(3)+......+log10(n)) + 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
double ans=0;
for(int i=2;i<=n;i++)
ans+=log10(i);
printf("%d\n",1+(int)ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1018-Big Number(数学)的更多相关文章

  1. HDU 1018 Big Number 数学题解

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  2. hdu 1018 Big Number 数学结论

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. HDU 1018 Big Number

    LINK:HDU 1018 题意:求n!的位数~ 由于n!最后得到的数是十进制,故对于一个十进制数,求其位数可以对该数取其10的对数,最后再加1~ 易知:n!=n*(n-1)*(n-2)*...... ...

  4. HDU 1018 Big Number (数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...

  5. hdu 1018:Big Number(水题)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. HDU 1018 Big Number (阶乘位数)

    题意: 给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路: 用对数log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w ...

  7. hdu 1018 Big Number (数学题)

    Problem Description Inmany applications very large integers numbers are required. Some of theseappli ...

  8. HDU 1018 Big Number【斯特林公式/log10 / N!】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

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

    Problem Description In many applications very large integers numbers are required. Some of these app ...

  10. HDU 1018 Big Number 斯特林公式

    Big Number 题意:算n!的位数. 题解:对于一个数来算位数我们一般都是用while去进行计算,但是n!这个数太大了,我们做不到先算出来在去用while算位数. while(a){ cnt++ ...

随机推荐

  1. HDU 4814 Golden Radio Base 模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 题目大意: 把一个正整数表示为φ进制, φ = (1+√5)/2 . 且已知: 1. φ + 1 ...

  2. Android 调用谷歌语音识别

    調用谷歌语音识别其实很简单,直接利用 intent 跳转到手机里面的谷歌搜索 代码也很简单,直接调用方法 startVoiceRecognitionActivity() 如果大家手机里面没有谷歌搜索, ...

  3. PAT-B 1015. 德才论(同PAT 1062. Talent and Virtue)

    1. 在排序的过程中,注意边界的处理(小于.小于等于) 2. 对于B-level,这题是比較麻烦一些了. 源代码: #include <cstdio> #include <vecto ...

  4. Linux下安装Python3.3.0

    Linux下安装Python3.3.0_路易_新浪博客 Linux下安装Python3.3.0 (2013-01-08 11:45:37)

  5. 采用CXF+spring+restful创建一个web接口项目

    这篇文章是http://blog.csdn.net/zxnlmj/article/details/28880303下面,加入的基础上的restful特征 1.参加restful必jar包裹 jsr31 ...

  6. hdu4679(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一棵树,每条边上都有一个权值,去掉树上任意一条边之后,分成两个子树,两个子树的最长路与这 ...

  7. KSImageNamed-Xcode

    KSImageNamed-Xcode   非常的给力的XCODE图片浏览插件; What is this? Can't remember whether that image you just add ...

  8. Oracle ORA-01034,ORA-27101,ORA-00600

    本机IP地址:192.168.1.163 [oracle@rtest ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.2.0 - Production on S ...

  9. selenium 远程调用浏览器

    共分三步: 1.selenium官网下载selenium-server-standalone.jar的最新版本号 2.启动selenium-server::::: java -jar "se ...

  10. POJ1201 差分约束

    给定ai,bi, ci 表示区间[ai,bi]内至少有ci个点, 要求对于所有给定的ai,bi,ci,  至少多少个点才能满足题目的条件 重做这一题学到的一点是, 可以设变量来表示一些东西,然后才能找 ...