由Stirling公式:
$$n! \approx \sqrt{2 \pi n} (\frac{n}{e})^n$$
故:$$\begin{align} ans &= log_k n! + 1 \\ &\approx log_k [\sqrt{2 \pi n} (\frac{n}{e})^n] + 1 \\ &= \frac{1}{2} log_k 2 \pi n + n * (log_k n - log_k e) + 1\\ \end {align}$$
又$log_a b = \frac{log a}{log b}$
而且要注意n比较小的时候近似值差别比较大。。。可以直接暴力。。。
1 /************************************************************** 2 Problem: 3000 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:28 ms 7 Memory:816 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cmath> 12 13 using namespace std; 14 typedef long double Lf; 15 typedef long long ll; 16 const Lf pi = acos(-1.0); 17 const Lf e = exp(1); 18 const Lf eps = 1e-10; 19 20 int n, k; 21 Lf ans; 22 23 Lf log(Lf x, Lf y) { 24 return log(x) / log(y); 25 } 26 27 int main() { 28 int i; 29 while (scanf("%d%d", &n, &k) != EOF) { 30 if (n <= 10000) { 31 for (ans = 0.0, i = 1; i <= n; ++i) ans += log(i); 32 ans /= log(k); 33 printf("%.0Lf\n", ceil(ans + eps)); 34 } else 35 printf("%lld\n", (ll) (0.5 * log(2 * pi * n, k) + n * log(n, k) - n * log(e, k)) + 1); 36 } 37 }
时间: 2024-11-06 09:33:22