POJ1423- 数学
来源:互联网 发布:php sqllite 编辑:程序博客网 时间:2024/05/19 18:41
http://poj.org/problem?id=1423
求n!的结果有多少位数(10进制)
暴力解法:
之前说过,求一个数k的10进制有多少位,就是 log10(k)+1
那么n!的位数就是 log10(n!)+1= log1+log2+log3....logn +1 (此后用log表示log10)
所以预处理好一个log10 (maxn!),然后打表输出
500ms:
另一个方法是直接用stirling公式
使得log(n!)=log( sqrt(2*pi*n) )+ n*log(n/e);
0ms.
暴力代码:
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std;int dp[10000007];int main(){int n,m,i,bit=1;double num=1;double sum=0;for (i=1;i<=10000000;i++){sum+= log10((double)i);dp[i]=sum;}scanf("%d",&n);while(n--){bit=1,num=1;scanf("%d",&m); printf("%d\n",(int)dp[m]+1); //这个1可以在sum里面加}return 0;}
套stirling公式代码
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std; const double pi=acos(-1.0);const double e=exp(1.0);int main(){int n,m,i,bit=1; scanf("%d",&n);while(n--){ scanf("%d",&m);int ans=log10( sqrt(2*pi*m) )+ m*log10(m/e);printf("%d\n",ans+1); }return 0;}
0 0
- POJ1423- 数学
- POJ1423 Big Number(数学)
- POJ1423
- poj1423
- poj1423
- poj1423
- POJ1423
- poj1423(排序)
- poj1423 tmd公式
- POJ1423 与 HDOJ1018 BigNumber
- poj1423 Big Number
- 水题poj1423 解题报告
- poj1423 hdu1018 Big Number
- poj1423 打表
- poj1423解题报告
- 重新走上ACM!poj1423
- poj1423(斯特林公式)
- POJ1423,Big Number,同hdu1018
- LeetCode 111 Minimum Depth of Binary Tree
- c# winfrom 在panel上绘制矩形
- Android防止重复点击深入
- Mybatis resultMap空值映射问题解决
- RPM包相关介绍
- POJ1423- 数学
- coderforce Codeforces Round #333 (Div. 1) A. The Two Routes
- RecycleBin 分析
- 行为驱动开发iOS
- PHP弱类型安全
- CCBR2015青年论坛总结
- mysql分表方法-----MRG_MyISAM引擎分表法
- ScrollView反弹效果的实现
- Otter(一)初识----简介和基本架构图