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. C++ 多态性分析

    编译 - 时间多态性--函数重载 编译后的中间代码(例如GCC产生.o文件.此时还不是汇编语言)函数名字有变化,看以下两个样例. void cc_show(const char*str)     -& ...

  2. hdu4908(中位数)

    传送门:BestCoder Sequence 题意:给一个序列,里面是1-N的排列,给出m,问以m为中位数的奇数长度的序列个数. 分析:先找出m的位置,再记录左边比m大的状态,记录右边比m大的状态,使 ...

  3. 在html中写python代码的语法和特点-----基于webpy的httpserver

    在html文件里写python语法的内容,的注意事项: 1:python程序中的变量通过以下方法传入到html: 1:通过全局变量 :全局变量是不须要用$def with语法实现传递的,仅仅要定义了 ...

  4. JAVA实现Shell排序

    Shell排序可以理解为插入排序的变种,它充分利用了插入排序的两个特点: (1). 当数据规模小的时候非常高效. (2). 当给定数据已经有序时的时间代价为O(N) 所以,Shell排序每次把数据分成 ...

  5. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

  6. R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)

    In this Document   Abstract   History   Details   _afrLoop=2234450430619177&id=1537521.1&dis ...

  7. 3DMax的OFusion插件使用问题

    使用OFusion将3D max场景导出为Ogre的Mesh是非经常常使用的方法.这里汇总一些问题方便查阅. 1.OFusion获取: http://download.csdn.net/detail/ ...

  8. Cocos2D &amp; SpriteBuilder Developer Guide

    https://www.makegameswith.us/docs/#!/cocos2d/1.0/overview

  9. eclipse加入辅助线,配合代码格式化使用

  10. cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵

    Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...