poj 1423 打表/斯特林公式

对于n位数的计算,我们可以采用(int)log10(n) + 1的方法得到n的位数
第一种方法:
对于n!位数的计算,log10(n!) = log10(1) + log10(2) + ... + log10(n)
为防止直接暴力超时这部分运算可以打表等待主程序调用
#include<iostream>
#include<cmath>
using namespace std; const int MAXN = 1e7;
int ans[MAXN +]; void action(int m)//打表计算n!位数,存在ans数组中
{
double d = ;
for(int i = ;i<=m;i++)
{
d += log10(double(i));//累加log10(i)
ans[i] = (int)d + ;//向下取整并+1
}
} int main()
{
int n,m;
cin>>n;
action(MAXN);
while(n--)
{
cin>>m;
cout<<ans[m]<<endl;
}
return ;
}
第二种方法:
对于n!的计算,也可以用斯特林公式:

然后直接计算(int)log10(n!) + 1
#include<iostream>
#include<cmath>
using namespace std; double pi = acos((double)-); int main()
{
int n,m;
cin>>n;
while(n--)
{
cin>>m;
cout<<(int)(log10(sqrt( * m * pi) )+ m * log10(m / exp((double))))+ <<endl;
}
}
poj 1423 打表/斯特林公式的更多相关文章
- POJ 1423:Big Number 求N的阶乘的长度 斯特林公式
Big Number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27027 Accepted: 8626 Descr ...
- POJ 1423 Big Number
题意:求n阶乘的位数. 解法:斯特林公式,,然后取log10就是位数了,因为精度问题需要化简这个式子,特判1. 代码: #include<stdio.h> #include<iost ...
- Semi-prime H-numbers POJ - 3292 打表(算复杂度)
题意:参考https://blog.csdn.net/lyy289065406/article/details/6648537 一个H-number是所有的模四余一的数. 如果一个H-number是H ...
- POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- POJ 1423 斯特林
题意:进制问题 分析: 打表,但是要用不能 long long 型,超内存. n! = log_{10}\sqrt{2{\pi}n}*(\frac{n}e)^n 精度要求 #include <c ...
- POJ 2661Factstone Benchmark(斯特林公式)
链接:传送门 题意:一个人自命不凡,他从1960年开始每10年更新一次计算机的最长储存长数.1960年为4位,每10年翻一倍.给出一个年份y,问这一年计算机可以执行的n!而不溢出的最大n值 思路:如果 ...
- POJ - 3037-Skiing(邻接表+Dijkstra)
Bessie and the rest of Farmer John's cows are taking a trip this winter to go skiing. One day Bessie ...
- n阶乘,位数,log函数,斯特林公式
一.log函数 头文件: #include <math.h> 使用: 引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为 ...
- Hdu 1042 N! (高精度数)
Problem Description Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input OneN in one ...
随机推荐
- 初识mysql学习笔记
使用VMVirtualBox导入Ubuntu后,可以通过sudo apt-get install mysql-server命令下载mysql. 在学习过程中,我遇到了连接不上Xshell的问题.最终在 ...
- web安全与防御
xss攻击(跨站脚本) 是网站应用程序的安全泄露攻击,是代码注入的一种.它允许恶意用户将代码注入到网页上,其他用户在观看网页时就会受到影响. 攻击原理 其特点是不对服务器端造成任何伤害,而是通过一些正 ...
- Flask中的单例模式
1,基于文件的单例模式: import pymysql import threading from DBUtils.PooledDB import PooledDB class SingletonDB ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(1)
技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代. 在Java的世界中,框架之争可能比语言本身的改变更让人关注.近几年,SpringMVC凭借简单轻便.开发效率高.与spr ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- [bzoj1731] [Usaco2005 dec]Layout 排队布局
差分约束系统...因为题目要求的是1和n的最大距离所以这题就跑最长路.. 对于互相反感的牛(i与j互相反感,彼此距离至少为len,i<j),就有dis[j]-dis[i]>=len.就加一 ...
- 二分查找c++简单模板
//数组a[]中有n各元素,已经按升序排序,待查找的元素x sort(a,a+n); //升序排序 template<class Type> int BinarySearch(Type a ...
- list容器的C++代码实现
#include <iostream> using namespace std; template <class T> class mylist;//前置声明 templat ...
- Lua语言的介绍和编程语言的归类
Lua 本条目介绍的是一种编程语言.关于关于Lua在维基百科中的使用,请见"维基百科:Lua".关于"Lua"一词的其他意思,请见"卢阿". ...
- 原生JavaScript常用的DOM操作
之前项目一直都是用JQuery或者Vue来做的,确实好用,毕竟帮我们解决了很多浏览器兼容问题,但是后面发现大公司面试题都是要原生Javascript来做,然后我就一脸懵逼哈哈哈,毕竟大公司需要的框架或 ...