HDOJ(1018)
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34743 Accepted Submission(s): 16478
数学公式推导
方法一:
①: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)的更多相关文章
- HDOJ 1018 Big Number(大数位数公式)
Problem Description In many applications very large integers numbers are required. Some of these app ...
- 【HDOJ】1018 Big Number
数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...
- 杭电hdoj题目分类
HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...
- HDOJ 题目分类
HDOJ 题目分类 /* * 一:简单题 */ 1000: 入门用:1001: 用高斯求和公式要防溢出1004:1012:1013: 对9取余好了1017:1021:1027: ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- PAT A 1018. Public Bike Management (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- ibatis实现Iterate的使用 (转)
<iterate property="" /*可选, 从传入的参数集合中使用属性名去获取值, 这个必须是一 ...
- 函数创建XML文件
REPORT YTST_XML_14. *----------------------------------------------------------------------* * PANT ...
- angularjs 中的iframe 标签 ng-src 路径 z-index 必须有position
如果直接写路径到iframe标签里的ng-src中会出现报错: 解决方法: 1.ng里面有个属性是专门用来解决跨域问题的 $sce. 用法: $scope.someUrl = $sce.trustAs ...
- 每天一个Linux命令(14)head命令
head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. 如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头. 如果不指定文件,或者文件为"- ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- jQuery宽屏游戏焦点图
在线演示 本地下载
- location记录<18.7.21>
// var index = location.href; // console.log(index) // // indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. // v ...
- M1905
11.09 11:00------102万 11.09 14:00---103万 11.12 16:00------103万 11.19 16:00---94万 11.20 ...
- phalcon:官方多模块支models层,mode数据库配置(二)
phalcon:官方多模块支models层,mode数据库配置(二) 利用:\pahlcon\mvc\model\Manager::registerNamespaceAlias()方法获取多模块下的m ...
- hdu 1864 最大报销额(01背包)
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...