对于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 打表/斯特林公式的更多相关文章

  1. POJ 1423:Big Number 求N的阶乘的长度 斯特林公式

    Big Number Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27027   Accepted: 8626 Descr ...

  2. POJ 1423 Big Number

    题意:求n阶乘的位数. 解法:斯特林公式,,然后取log10就是位数了,因为精度问题需要化简这个式子,特判1. 代码: #include<stdio.h> #include<iost ...

  3. Semi-prime H-numbers POJ - 3292 打表(算复杂度)

    题意:参考https://blog.csdn.net/lyy289065406/article/details/6648537 一个H-number是所有的模四余一的数. 如果一个H-number是H ...

  4. 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 ...

  5. POJ 1423 斯特林

    题意:进制问题 分析: 打表,但是要用不能 long long 型,超内存. n! = log_{10}\sqrt{2{\pi}n}*(\frac{n}e)^n 精度要求 #include <c ...

  6. POJ 2661Factstone Benchmark(斯特林公式)

    链接:传送门 题意:一个人自命不凡,他从1960年开始每10年更新一次计算机的最长储存长数.1960年为4位,每10年翻一倍.给出一个年份y,问这一年计算机可以执行的n!而不溢出的最大n值 思路:如果 ...

  7. 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 ...

  8. n阶乘,位数,log函数,斯特林公式

    一.log函数 头文件: #include <math.h> 使用: 引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为 ...

  9. Hdu 1042 N! (高精度数)

    Problem Description Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input OneN in one ...

随机推荐

  1. cs231n spring 2017 lecture11 Detection and Segmentation 听课笔记

    1. Semantic Segmentation 把每个像素分类到某个语义. 为了减少运算量,会先降采样再升采样.降采样一般用池化层,升采样有各种"Unpooling"." ...

  2. poj 3261

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13249   Accepted: 5894 Ca ...

  3. CodeForces 543D:Road Improvement

    题目:http://codeforces.com/problemset/problem/543/D 题意:给你一棵树,一开始边都是0,可以使任意的边变成1,对于每一个根节点求使得它到其他任一点的路径上 ...

  4. BZOJ2338: [HNOI2011]数矩形

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2338 中学数学老师告诉我们,一个矩形的两条对角线相等,所以只要把所有的边拿出来,记录下中点坐标 ...

  5. MIT公开课:算法导论 笔记(一)

    课程链接:http://open.163.com/special/opencourse/algorithms.html 第一课:算法分析基础 1.介绍插入排序与归并排序,计算并比较最坏运行时间 2.算 ...

  6. linux(CENTOS)系统各个目录的作用详解

    Linux(CentOS)系统各个目录的作用详解 文件的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.S ...

  7. 布隆(Bloom)过滤器 JAVA实现

    前言 Bloom过滤器,通过将字符串映射为信息指纹从而节省了空间.Bloom过滤器的原理为,将一个字符串通过一定算法映射为八个Hash值,将八个Hash值对应位置的Bitset位进行填充.在进行校验的 ...

  8. VS2008 如何将Release版本设置可以调试的DEBUG版本

    VS2008 如何将Release版本设置可以调试的DEBUG版本 只需设置三个部分: 项目->属性->C/C++->General->Debug Information Fo ...

  9. WdatePicker设置时间区间时,对开始时间和结束时间限制

    <input id="startDate" name="startDate"  type="text" readonly=" ...

  10. 怎么解决dede首页网址自动加上index.html

    怎样去掉dedecms5.7(织梦)首页url后index.html有三种方法 1.去配置你的空间的默认首页地址.把index.html移到默认文本最前面.(确保你的默认文档里面有index.html ...