Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34743    Accepted Submission(s): 16478

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
 

数学公式推导

方法一:

①:10^M < n!   <10^(M+1)  若求得M,则M+1即为答案。

对公式①两边以10为底取对数

M < log10(n!) < M+1

因为 log10(n!)=log10(1)+log10(2)+……+log10(n)

可用循环求得M+1的值

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
double ans=;
for(int i=;i<=n;i++)
{
ans+=log(i)/log();
}
cout<<(int)ans+<<endl; }
return ;
}

方法二:斯特林公式
n! ≈ sqrt(2*n*pi)*(n/e)^n

则 M+1=(int)(0.5*log(2.0*n*PI)+n*log(n)-n)/(log(10.0)) )+1;

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double PI=3.1415926;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
double ans;
ans=(0.5*log(2.0*n*PI)+n*log(n)-n)/(log(10.0));
cout<<(long)ans+<<endl;
}
return ;
}

Java:

import java.util.Scanner;
public class Main{
static Scanner cin=new Scanner(System.in);
static final int MAXN=10000005;
static int[] res=new int[MAXN];
public static void main(String[] args){
double pre=Math.log(1.0);
res[1]=(int)pre+1;
for(int i=2;i<=10000000;i++)
{
pre+=Math.log10((double)i);
res[i]=(int)pre+1;
}
int T=cin.nextInt();
while(T--!=0)
{
int n=cin.nextInt();
System.out.println(res[n]);
}
}
}

HDOJ(1018)的更多相关文章

  1. HDOJ 1018 Big Number(大数位数公式)

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

  2. 【HDOJ】1018 Big Number

    数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...

  3. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  4. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. HDOJ 1326. Box of Bricks 纯水题

    Box of Bricks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. PAT A 1018. Public Bike Management (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...

  9. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

随机推荐

  1. python基础-第五篇-5.2递归

    又是一个阳光明媚的日子,小白看着刚刚从东边升起的太阳,感觉太阳爷爷也在向她打招呼,小白就不经的微笑起来!心想:今天又会学到什么有趣的东西呢?有些小期待,也有些小激动! 小刘来得比小白还早,两辆相视而笑 ...

  2. IOS navigationItem 设置返回button,title图片和rightBarButtonItem

    1.自己定义返回button UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" st ...

  3. centos下写Symfony

    之前都是在windows上写SY,现在要部署到Linux上了,提前测试一下. 第一步,要有台Centos机器,安装过程略 第二步,安装数据库,PostgreSQL,过程; 第三步,安装版本控制器,GI ...

  4. kettle连接资源库设置

    到这里你是登陆不上去的,需要创建或更新按钮,因为需要在你的数据库里创建关于kettle的数据表,来存储资源库 点执行就可以了 一般情况下kettle资源库自动给你创建两个用户: 工具->资源库- ...

  5. LDA中的先验知识

    LDA涉及到的先验知识有:二项分布.Gamma函数.Beta分布.多项分布.Dirichlet分布.马尔科夫链.MCMC.Gibbs Sampling.EM算法等. 二项分布 二项分布是N重伯努利分布 ...

  6. YiiFramework(PHP)

    Yii is a high-performance PHP framework best for developing Web 2.0 applications. Ref:http://www.yii ...

  7. t-sql判断数据库对象是否存在

    1 系统表sys.sysobjects 在数据库中创建的每个对象(例如约束.默认值.日志.规则以及存储过程)都对应一行,详细介绍参考MSDN 2 OBJECTPROPERTY 返回当前数据库中架构范围 ...

  8. 关于用JAVA开发短信方面的知识

      现在流行的网络业务莫过于短信了.网易新浪等都因此而盈利,股价上涨.我凭自己的经验和公司支持,也就乘着东风来研究一下了! 首先,你要选择一台移动或者联通的短信服务器做你们的发送短信接口.这是最关键的 ...

  9. origin与referer的区别

    referer显示来源页面的完整地址,而origin显示来源页面的origin: protocal+host,不包含路径等信息,也就不会包含含有用户信息的敏感内容 referer存在于所有请求,而or ...

  10. Eclipse输入命令行参数

    想要在Eclipse中输入命令行参数,可以在目录中该程序上右键,选择“Run As",选择”Run configurations",如图: 然后输入命令行参数: 点击Apply和R ...