还以为是普通的高精度,结果Time Limit Exceed了。
one integer 1 <= m <= 10^7 on each line. 这个数量级不可能AC了。
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
看了Discuss,基本上有两种方法:
1。用Stirling公式
公式和证明如下:
An important formula in applied mathematics as well as in probability is the Stirling's formula known as
![/begin{displaymath}n! /sim /sqrt{2 /pi} /; n^{/displaystyle (n+ 1/2)} /;e^{/displaystyle-n} /end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/2e2ec9597100db4fa874ce7b0ac14c58.png)
where is used to indicate that the ratio of the two sides goes to 1 as n goes to
. In other words, we have
![/begin{displaymath}/lim_{n /rightarrow /infty} /frac{n!}{/sqrt{2 /pi} /; n^{/displaystyle (n+ 1/2)}e^{/displaystyle-n}} = 1/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/900837b160f24dcb6690f93a7311a3d7.png)
or
![/begin{displaymath}/lim_{n /rightarrow /infty} /frac{n!}{ n^{/displaystyle (n+ 1/2)}e^{/displaystyle-n}} = /sqrt{2 /pi}/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/fa96cc6f418ae8d005ce7391c19e622d.png)
Proof of the Stirling's Formula
First take the log of n! to get
![/begin{displaymath}/log(n!) = /log (1) + /log(2) + /cdots + /log(n)/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/a84b397f4f7710d8007e8d005ebea97b.png)
Since the log function is increasing on the interval
, we get
![/begin{displaymath}/int_{n-1}^{n} /log(x) dx < /log(n) < /int_{n}^{n+1} /log(x) dx/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/01fb9c66723b25f75627b5fb3e4139b8.png)
for . Add the above inequalities, with
, we get
![/begin{displaymath}/int_{0}^{N} /log(x) dx < /log(N!) < /int_{1}^{N+1} /log(x) dx/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/d48b760000b2b1ffc02568cae033ab22.png)
Though the first integral is improper, it is easy to show that in fact it is convergent. Using the antiderivative of (being
), we get
![/begin{displaymath}n/log(n) - n < /log(n!) < (n+1)/log(n+1) - n/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/39d16b46c477409a2c60abe7c04d1171.png)
Next, set
![/begin{displaymath}d_n = /log(n!) - /left(n + /frac{1}{2}/right) /log(n) + n/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/f5ae52566818663601908055c88307a5.png)
We have
![/begin{displaymath}d_n - d_{n+1} = /left(n + /frac{1}{2}/right) /log/left(/frac{n+1}{n}/right) - 1/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/73743788cbdacd27b30bbc2be821644a.png)
Easy algebraic manipulation gives
![/begin{displaymath}/frac{n+1}{n} = /frac{1 + /displaystyle /frac{1}{2n+1}}{1 - /displaystyle /frac{1}{2n+1}}/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/7c6cabaea59fdf827082370060e8aab1.png)
Using the Taylor expansion
![/begin{displaymath}/frac{1}{2} /log/left(/frac{1+t}{1-t}/right) = t + /frac{1}{3} t^3 + /frac{1}{5} t^5 + /cdots/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/756b11cc9b8dec1802286ee75a08cfd1.png)
for
-1 < t < 1, we get
![/begin{displaymath}d_n - d_{n+1} = /frac{1}{3} /frac{1}{(2n+1)^2} + /frac{1}{5} /frac{1}{(2n+1)^4} + /cdots/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/5c80763e814fb4029e5f788325e24720.png)
This implies
![/begin{displaymath}0 < d_n - d_{n+1} < /frac{1}{3} /Bigg(/frac{1}{(2n+1)^2} + /frac{1}{(2n+1)^4} + /cdots/Bigg)/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/b94cc5cfbb68faa213540ca5cca4dbdb.png)
We recognize a geometric series. Therefore we have
![/begin{displaymath}0 < d_n - d_{n+1} < /frac{1}{3} /frac{1}{(2n+1)^2 - 1} = /frac{1}{12} /left(/frac{1}{n} - /frac{1}{n+1}/right)/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/1e1ec9261f48d42d428308485bbcb4a8.png)
From this we get
-
1.
-
the sequence
is decreasing;
2.
-
the sequence
is increasing.
This will imply that converges to a number C with
![/begin{displaymath}/lim_{n /rightarrow /infty} d_n = /lim_{n /rightarrow /infty} d_n - /displaystyle /frac{1}{12n} = C /end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/8e4b8b0b1c1abf236cdb5b6d033d8835.png)
and that
C > d1 - 1/12 = 1 - 1/12 = 11/12. Taking the exponential of dn, we get
![/begin{displaymath}/lim_{n /rightarrow /infty}/frac{n!}{ n^{/displaystyle (n+ 1/2)}e^{/displaystyle-n}} = e^{C}/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/7b88390ea0f61f4f4de1873e80161ef0.png)
The final step in the proof if to show that
. This will be done via Wallis formula (and Wallis integrals). Indeed, recall the limit
![/begin{displaymath}/lim_{n /rightarrow /infty} /frac{2.2.4.4.6.6/ldots(2n)(2n)}{1.1.3.3.5.5./ldots(2n-1)(2n-1)(2n+1)}/;= /frac{/pi}{2}/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/c76068b09883ccec269915d2a0fdfbbf.png)
Rewriting this formula, we get
![/begin{displaymath}/frac{2.4.6/ldots(2n)}{1.3.5./ldots(2n-1) /sqrt{2n}} /sim /sqrt{/frac{/pi}{2}}/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/48148103f68a8dfcdad83c6a1bc3a003.png)
Playing with the numbers, we get
![/begin{displaymath}/frac{(2^n n!)^2}{(2n)!} /frac{1}{/sqrt{2n}} /sim /sqrt{/frac{/pi}{2}}/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/8bd85ba44a7496939c5050f99ad26f16.png)
Using the above formula
![/begin{displaymath}n! /sim n^{/displaystyle (n+ 1/2)}e^{/displaystyle-n} e^{C}/;./end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/ff207bff344be8ebede9f46fa2c50b51.png)
we get
![/begin{displaymath}/frac{2^{2n} /Big(n^{2n+1} e^{-2n} e^{2C}/Big)}{(2n)^{(2n+1/2)} e^{-2n} e^C}/frac{1}{/sqrt{2n}} /sim /sqrt{/frac{/pi}{2}}/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/3c4f75a18ddd4afae4c9ee06dc68c1e3.png)
Easy algebra gives
![/begin{displaymath}e^C /sim /sqrt{2/pi}/end{displaymath}](https://i-blog.csdnimg.cn/blog_migrate/8354343251fa36c13a67c18da8813069.png)
since we are dealing with constants, we get in fact
. This completes the proof of the Stirling's formula.
AC code:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
2。用 位数=[ lg1+lg2+……lg(N-1)+lgN ] + 1。
数很大时,速度会慢,TLE。。
所以如上述AC代码,小于100000时,可以用这种方法,再大的话最好用Stiring公式。