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. 微信小程序报“app.json”错误解决办法

    1.亲测 “app.json未找到入口 app.json 文件,或者文件读取失败,请检查后重新编译.” 是由于新创建的界面xxx.json所在的文件夹为0KB造成的,你可以试着在xxx.json文件内 ...

  2. Dajngo admin使用

    Dajngo admin使用 Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.py 中的 INS ...

  3. Js编写的菜单树

    只需要提供这种JSON格式就ok了 其他的都可以直接引用这个代码进去 var testMenu=[ { "name": "一级菜单", "submen ...

  4. GUI菜单——菜单条、菜单、子条目之间关系

    菜单:注意区分三个概念:菜单条.菜单.菜单项 将菜单条添加到窗体,菜单条下面包括菜单,菜单下面可以使菜单或者菜单项 菜单项是最后一个.菜单后面有三角标示. 菜单条[文件] 子菜单--子条目 子条目 示 ...

  5. if-else 与 switch-case语句

    if语句就是起一个判断作用,在c语言中有三种表达形式:1.if(表达式)语句:pl.   if(x==y)printf("%d",x);         //*  "== ...

  6. Android蓝牙开发浅析【转】

    本文转载自:http://blog.csdn.net/geekdonie/article/details/7487761 由于近期正在开发一个通过蓝牙进行数据传递的模块,在参考了有关资料,并详细阅读了 ...

  7. 【反思】一个价值两天的BUG,无论工作还是学习C语言的朋友都看看吧!

    博文原创,转载请联系博主! 使用C语言也有两个年头了,BUG写出来过不少,也改过不少BUG.但是偏偏就是有这么一个BUG让我手头的项目停工了两天,原因从百度找到谷歌,资料从MAN手册找到RFC也没有找 ...

  8. JsonSchema使用详解

    JSON Schema是一个用于验证JSON数据结构的强大工具, 我查看并学习了JSON Schema的官方文档, 做了详细的记录, 分享一下. 我们可以使用JSON Schema在后续做接口测试中做 ...

  9. Outlook 2010打开没反应,只有任务栏有图标的解决方法

    Outlook 2010打开没反应,任务栏图标显示如下: 解决方法: 按下Windows+R键,输入regedit: 按回车: 请在注册表编辑器中定位到以下键值,重命名以下4项(比如将outlook重 ...

  10. Oracle数据库操作语言(DML)

    --insert添加语句 insert into table_name(column_name,column_name,...) values (data1,data2,...); --通过表添加数据 ...