51nod 1130 阶乘长度 [Stirling公式]
2016-07-24 19:26
351 查看
输出n!的位数
n! 约等于 根号下(2πn) * (n/e )^n
那么 res = 1/2*lg(2πn) + nlg(n/e) + 1
实测n很小也是正确的答案
公式链接:
http://m.blog.csdn.net/article/details?id=51145807
Summary:
cmath里面有π和e的精确值!!!!!!
n! 约等于 根号下(2πn) * (n/e )^n
那么 res = 1/2*lg(2πn) + nlg(n/e) + 1
实测n很小也是正确的答案
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<set> #include<string> #include<iomanip> #include<ctime> #include<climits> #include<cctype> #include<algorithm> #ifdef WIN32 #define AUTO "%I64d" #else #define AUTO "%lld" #endif using namespace std; #define smax(x,tmp) x=max((x),(tmp)) #define smin(x,tmp) x=min((x),(tmp)) #define maxx(x1,x2,x3) max(max(x1,x2),x3) #define minn(x1,x2,x3) min(min(x1,x2),x3) const int INF=0x3f3f3f3f; const double pi = M_PI; const double e = M_E; const double eps = 1e-7; typedef long long LL; inline LL get_res(int n) { if(n<=10) { int tot = 1; for(int i=1;i<=n;i++) tot *= i; return floor(log10(tot) + eps) + 1; } return floor(0.5*log10(2*pi*(double)n) + (double)n*log10((double)n/e) + eps) + 1; } int main() { freopen("stirling.in","r",stdin); freopen("stirling.out","w",stdout); int cas; scanf("%d",&cas); while(cas--) { int n; scanf("%d",&n); LL ans = get_res(n); printf(AUTO"\n",ans); } return 0; }
公式链接:
http://m.blog.csdn.net/article/details?id=51145807
Summary:
cmath里面有π和e的精确值!!!!!!
相关文章推荐
- 1.m分解阶乘之和
- 2.几种递推数
- 3.欧拉函数
- 4.快速幂模m算法
- 5.扩展欧几里得&&中国剩余定理
- 6.数论_web
- Project Euler Problem 387 - Harshad Numbers - 深度优先
- 编程之美2015初赛A
- 数论题集
- 原根
- 阶与原根学习笔记
- HDU 1299 Diophantus of Alexandria
- Leftmost Digit(HDU 1060)
- Rightmost Digit(HDU 1061)
- Python-在奇数中寻找素数
- ZOJ 2674 Strange Limit 欧拉定理
- LeetCode-Palindrome Number
- 组合数求模总结
- UVA11461Square Numbers
- Sicily 1047 Super Snooker