7-n!的位数(斯特灵公式)
http://acm.hdu.edu.cn/showproblem.php?pid=1018
Big Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40483 Accepted Submission(s): 19774
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
Source
Asia 2002, Dhaka (Bengal)
分析:
斯特灵公式是一条用来取n阶乘近似值的数学公式。
公式为:
斯特林公式可以用来估算某数的大小,结合lg可以估算某数的位数,或者可以估算某数的阶乘是另一个数的倍数。
题意:
给你一个整数n,求n!的位数。
利用 求解n!的位数:
易知整数n的位数为[lgn]+1。.利用Stirling公式计算n!结果的位数时,可以两边取对数,得:
log10(n!) = log10(2*n*Pi)/2+n*log10(n/e)
则答案为:
ans = log10(2*n*Pi)/2+n*log10(n/e) + 1
其他类型题:hdu4045 hdu2521
如果是求八进制的位数呢:http://www.cnblogs.com/zhumengdexiaobai/p/8415053.html
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
double e = 2.718281828459045;
double pi = 3.141592653589793; int main(){
int t, n;
cin >> t; while(t--){
cin >> n;
double k = log10(2 * pi * n) / 2 + n * log10(n / e);
// cout << ceil(k) << endl; //不能直接向上取整,因为若是刚好是整数,则会小1: 1.1e4,应该是4+1
cout << (int)k + 1 << endl;
} return 0;
}
7-n!的位数(斯特灵公式)的更多相关文章
- 数据结构作业——N!的位数(斯特灵公式)
Description 求N!的位数 Input 输入第一行为一个正整数 n(1<=n<=25000). Output 输出 n!的位数. Sample Input 1020 Sample ...
- [牛客OI测试赛2]F假的数学游戏(斯特灵公式)
题意 输入一个整数X,求一个整数N,使得N!恰好大于$X^X$. Sol 考试的时候只会$O(n)$求$N!$的前缀和啊. 不过最后的结论挺好玩的 $n! \approx \sqrt{2 \pi n} ...
- hdu1018--斯特灵公式
斯特灵公式 Wiki http://zh.wikipedia.org/wiki/斯特林公式 /** \brief hdu 1018 * * \param date 2014/7/24 * \param ...
- cf932E. Team Work(第二类斯特灵数 组合数)
题意 题目链接 Sol 这篇题解写的非常详细 首先要知道第二类斯特灵数的一个性质 \[m^n = \sum_{i = 0}^m C_{n}^i S(n, i) i!\] 证明可以考虑组合意义:\(m^ ...
- 斯特灵(Stirling)数
http://zh.wikipedia.org/wiki/%E6%96%AF%E7%89%B9%E7%81%B5%E6%95%B0 第一类:n个元素分成k个非空循环排列(环)的方法总数 递推式:s(n ...
- HDU 1018Big Number(大数的阶乘的位数,利用公式)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...
- 斯特灵数 (Stirling数)
@维基百科 在组合数学,Stirling数可指两类数,都是由18世纪数学家James Stirling提出的. 第一类 s(4,2)=11 第一类Stirling数是有正负的,其绝对值是个元素的项目分 ...
- Examining the Rooms(dp,斯特灵数)
Examining the Rooms Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3625 Examining the Rooms【第一类斯特灵数】
<题目链接> <转载于 >>> > 题目大意:有n个锁着的房间和对应n扇门的n把钥匙,每个房间内有一把钥匙.你可以破坏一扇门,取出其中的钥匙,然后用取出钥匙打 ...
随机推荐
- WIN7下用笔记本创建无线网
系统要求:WIN7 硬件要求:拥有无线网卡 网络要求:已经连接internet 1.启用并设定虚拟Wifi网卡:运行以下命令启用虚拟无线网卡(相当于打开路由器): netsh wlan set h ...
- HDU 1533
http://acm.hdu.edu.cn/showproblem.php?pid=1533 人和房子数量相同,每个人进房子,费用是人到房子的曼哈顿距离,求最小费用 可用最小费用最大流求解,建立虚拟的 ...
- CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)
You are a given a list of integers a 1 ,a 2 ,…,a n a1,a2,…,an and s s of its segments [l j ;r j ] [ ...
- springboot整合mybatis增删改查(二):springboot热部署
SpringBoot整合热部署 传统情况下, 我们用idea运行springboot程序时, 如果我们需要修改类里的方法,或者其他信息 我们需要修改完保存,并且重启springboot,有时候会很浪费 ...
- 都是用 DllImport?有没有考虑过自己写一个 extern 方法?
你做 .NET 开发的时候,一定用过 DllImport 这个特性吧,这货是用于 P/Invoke (Platform Invoke, 平台调用) 的.这种 DllImport 标记的方法都带有一个 ...
- slot的使用实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 自定义redis session
1.思路 2.程序实现 1.用户系统类 这里模拟一个蹩脚的用户系统类(userSystem),如下: #coding=utf-8 #Redis实现用户系统 __author__ = 'beginman ...
- fusionjs 学习二 核心概念
核心概念 middleware 类似express 的中间件模型(实际上是构建在koa中间件模型上的),但是和koa 的中间件有差异 fusionjs 的中间件同时可以运行在浏览器页面加载的时候 se ...
- IMP-00009: 导出文件异常结束 imp
在一次exp/imp中,用imp导入数据时报错.错误信息如下: IMP-00009: 导出文件异常结束 imp导入时异常结束可以有很多原因造成,要具体问题具体分析. 可能原因一: 导入的数据表过大,而 ...
- mysql 源代码目录及安装目录介绍
1.源代码目录介绍: 1.BUILD BUILD目录是编译.安装脚本目录,绝大部分以compile-开头,其中的SETUP.sh脚本为C和C++编译器设置了优化选项.2.client cl ...