1. 程式人生 > >斯特林公式-Stirling公式(取N階乘近似值)-HDU1018-Big Number 牛客網NowCoder 2018年全國多校算法寒假訓練營練習比賽(第三場)A.不凡的夫夫

斯特林公式-Stirling公式(取N階乘近似值)-HDU1018-Big Number 牛客網NowCoder 2018年全國多校算法寒假訓練營練習比賽(第三場)A.不凡的夫夫

subject color content coder -m ria 一點 練習 java

最近一堆題目要補,一直鹹魚,補了一堆水題都沒必要寫題解。備忘一下這個公式。

Stirling公式的意義在於:當n足夠大時,n!計算起來十分困難,雖然有很多關於n!的等式,但並不能很好地對階乘結果進行估計,尤其是n很大之後,誤差將會非常大。但利用Stirling公式可以將階乘轉化成冪函數,使得階乘的結果得以更好的估計。而且n越大,估計得越準確。

傳送門:_(:з」∠)_

再來一個詳細一點的,傳送門:( ?′ω`? )

再來兩道題目。

HDU1018

Big Number

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)


Total Submission(s): 40645 Accepted Submission(s): 19863


Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Input Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.

Output The output contains the number of digits in the factorial of the integers appearing in the input.

Sample Input 2 10 20

Sample Output 7 19

Source Asia 2002, Dhaka (Bengal) 代碼:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const double e=exp(1);
 8 const double pi=acos(-1.0);
 9 int main(){
10     int t;
11     scanf("%d",&t);
12     while(t--){
13         int n;
14         scanf("%d",&n);
15         if(n==1){printf("1\n");continue;}
16         double s=log10(2.0*pi)/2.0+(n+0.5)*log10(n)-n*log10(e);
17         int ans=ceil(s);
18         printf("%d\n",ans);
19     }
20     return 0;
21 }

2018年全國多校算法寒假訓練營練習比賽(第三場)

A.不凡的夫夫 時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
鏈接:https://www.nowcoder.net/acm/contest/75/A
來源:牛客網

題目描述

夫夫有一天對一個數有多少位數感興趣,但是他又不想跟凡夫俗子一樣,
所以他想知道給一個整數n,求n!的在8進制下的位數是多少位。

輸入描述:

第一行是一個整數t(0<t<=1000000)(表示t組數據)
接下來t行,每一行有一個整數n(0<=n<=10000000)

輸出描述:

輸出n!在8進制下的位數。
示例1

輸入

3
4
2
5

輸出

2
1
3



代碼:
 1 //A-斯特林公式-求位數
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<stack>
 8 #include<map>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 using namespace std;
13 const int inf=1<<30;
14 const int maxn=1e5+10;
15 const double eps=1e-6;
16 const int mod=1e9+7;
17 const double pi=acos(-1.0);
18 const double epx=1e-10;
19 const double e=exp(1);
20 typedef long long ll;
21 int l(int n){
22     int s=1;
23     if(n>3)
24         s=(log(2*pi*n)/log(8))/2+n*(log(n/e)/log(8))+1;
25     return s;
26 }
27 int main(){
28     int n,t;
29     scanf("%d",&t);
30     while(t--){
31         scanf("%d",&n);
32         printf("%d\n",l(n));
33     }
34     return 0;
35 }


最近一堆大數的題目寫的頭都炸了,java學的超級垃圾,c語言版的太長不想寫。。。

補題補題,天坑還未補。。。

太垃圾了,我怎麽這麽菜啊。

斯特林公式-Stirling公式(取N階乘近似值)-HDU1018-Big Number 牛客網NowCoder 2018年全國多校算法寒假訓練營練習比賽(第三場)A.不凡的夫夫