zoukankan      html  css  js  c++  java
  • [笔记] 斯特林公式

    [笔记] 斯特林公式

    先看一道题

    题目大意:求一个(N)的阶乘恰好大于(x^x)

    用斯特林公式算出位数,然后二分查找,一定注意精度

    Detail

    [displaystyle{limlimits_{n ightarrow infty}frac{n!}{sqrt{2 pi n}left(frac{e}{n} ight)^n}=1} ]

    [displaystyle{frac{lg{2 pi n}}{2}+nlg{frac{e}{n}}} ]

    Code

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    using namespace std;
    const double Pi = acos(-1.0);
    const double e = exp(1.0);
    
    typedef long long ll;
    
    long long x;
    
    int main(){
    	cin >> x;
    	ll l = 1, r = 100000000000000L, mid;//字面值太大要加个L保险
    	while(l < r){
    		mid = l + ((r - l) >> 1);
    		if(double(double(log10(sqrt(2 * Pi * mid))) + double(mid * double(log10(mid / e)))) < x*double(log10(x)))
    			l = mid + 1;
    		else 
    			r = mid;
    	}
    	printf("%lld
    ", l);
    	return 0;
    }
    
  • 相关阅读:
    Linux Shell脚本编程--Head/Tail命令详解
    Python学习笔记-抽象
    L2-020 功夫传人
    pat 抢红包
    pat 集合相似度
    pat 喊山
    hdu1029
    win10 , JAVA安装 环境搭建
    ZOJ2540 Form a Square
    ZOJ3180 Number Game
  • 原文地址:https://www.cnblogs.com/LMSH7/p/9605391.html
Copyright © 2011-2022 走看看