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. Android系统移植与调试之------->如何修改Android自带的apk出现一圈圈类似鸡蛋的花纹

    最近被一个问题烦恼到了,就是android4.1系统自带的Email.文件管理器.信息等apk都出现同一个问题,就是现实在平板上的时候会出现一圈圈类似鸡蛋的花纹. 我想了两种方法来解决,第一种方法没有 ...

  2. OpenCV编程->RGB直方图统计

      我们在处理彩色图像时.特别是在做局部图像的阈值切割时,须要一个直观的RGB统计图.   接下来開始实现.    代码: void CalcHistRGB() { IplImage* img_sou ...

  3. 转。git 乌龟的使用安装

    TortoiseGit 简称 tgit, 中文名海龟Git. 海龟Git只支持神器 Windows 系统, 有一个前辈海龟SVN, TortoiseSVN和TortoiseGit都是非常优秀的开源的版 ...

  4. FOXMAIL提示容量满无法收邮件,清除旧邮件后还是无法收取,请问如何解决?

    FOXMAIL提示容量满无法收邮件,清除旧邮件后还是无法收取,请问如何解决? 2009-03-23 11:21包子燕  分类:网站使用 我清除了FOXMAIL所在的磁盘空间,共有12G,也删除了部分旧 ...

  5. LeetCode:删除排序数组中的重复项||【80】

    LeetCode:删除排序数组中的重复项||[80] 题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原 ...

  6. dev系列之gridview

    gridview新增一行就激活编辑,及显示闪动的光标 gridView1.ShowEditor(); 隐藏Gridview表头上面的panel this.gridView1.OptionsView.S ...

  7. phpmyadmin mysqlnd cannot connect to

    mysqlnd cannot connect to MySQL 4.1+ using the old insecure authentication. Please use an administra ...

  8. spring boot项目遇到 'lower_case_table_names' 的解决办法

    今天自己搭建了spring boot项目,配置的是mysql数据库,启动时报如下错误 Mon Jan 22 23:31:40 CST 2018 WARN: Establishing SSL conne ...

  9. JavaScript基础挖掘目录

    前端基础进阶(一):内存空间详细图解 前端基础进阶(二):执行上下文详细图解 前端基础进阶(三):变量对象详解 前端基础进阶(四):详细图解作用域链与闭包 前端基础进阶(五):全方位解读this 前端 ...

  10. java:解决eclipse配置Tomcat时找不到server选项

    http://blog.csdn.net/wugangsunny/article/details/25246565 集成Eclipse和Tomcat时找不到server选项: 按照网上的步骤如下: 在 ...