求N!的长度【数学】 51nod 1058 1130

来源:互联网 发布:apache beam 实时流 编辑:程序博客网 时间:2024/06/05 21:58

 

n!的长度等于log10(n!)

#include <bits/stdc++.h>using namespace std;int main() {    int n;    cin >> n;    double ans = 1;    for(int i = 1; i <= n; i++) {        ans += log10(i);    }    cout << (int)ans << endl;}


 

用斯特林公式求n!,然后log10(n!)即可

(如果怕n不够大下式不成立,可以当数小于10000时用for求阶层。不过51nod直接过了)

#include <bits/stdc++.h>#define PI 3.1415926535898#define e 2.718281828459using namespace std;int main() {    int T, n;    cin >> T;    while(T--) {        cin >> n;        double ans = log10(sqrt(2.0*PI*n)) + n*log10(n*1.0/e);// pow(n*1.0/e, n);        cout << (long long)ans + 1 << endl;    }}