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

n!的长度等于log10(n!)
|
1
2
3
4
5
6
7
8
9
10
11
|
#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直接过了)

|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#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; }} |

浙公网安备 33010602011771号