Stirling公式:lim(n→∞) √(2πn) * (n/e)^n / n! = 1也就是说当n趋于无穷大的时候,n!与√(2πn) * (n/e)^n的值十分接近
推广:求左边第x位只要推公式时将10^(k-1)变为10^(k-x)即可,最后答案取模10。
n^n的位数:k = [lg(n^n)] + 1 = [n * lg(n)] + 1 最左边的数作个位:x = n^n / 10^(k-1)取对数:lg(x) = n * lg(n) - (k-1) = (n*lg(n) - [n*lg(n)])最左边的数:[x] = [10^lg(x)] = [10^(n*lg(n) - [n*lg(n)])]
"[ ]"代表的是向下取整,可以用floor函数实现
//n!最左位n!的位数:k = [lg(n!)] + 1最左边的数作个位:x = n! / 10^(k-1)取对数:lg(x) = lg(n!) - (k-1) = lg(n!) - [lg(n!)]最左边的数:[x] = [10^lg(x)] = [10^(lg(n!) - [lg(n!)])]n!将会很大,所以要用Stirling公式近似计算Stirling公式:lim(n→∞) √(2πn) * (n/e)^n / n! = 1也就是说当n趋于无穷大的时候,n!与√(2πn) * (n/e)^n的值十分接近所以lg(n!) = lg(√(2πn) * (n/e)^n) = 1/2*(lg(2π) + lg(n)) + n * (lg(n) - lg(e))代入[x] = [10^lg(x)] = [10^(lg(n!) - [lg(n!)])]即可。提示:log10(d) = log(d)/log(10.0)
题意:求n^n最左边的数
思路:公式
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
typedef long long ll;
int main()
{
double n;
int Tcase;
scanf("%d",&Tcase);
for(int ii = 1; ii <= Tcase ; ii ++)
{
scanf("%lf",&n);
int ans = floor(pow(10,n*log10(n) - floor(n * log10(n))));
cout << ans << endl;
}
return 0;
}
poj1423
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <cmath>
#include <algorithm>
#include<map>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 20;
const double PI = acos(-1.0);
const double e = 2.718281828459045;
int main()
{
int Tcase;
scanf("%d",&Tcase);
for(int ii = 1; ii <= Tcase; ii ++)
// while( ~ scanf("%d",&n))
// while( ~ scanf("%d%d",&n,&m))
{
int n;
cin >> n;
cout << (int)(0.5*(log10(2 * PI * n)) + n*log10(n/e) ) + 1<< endl;
}
return 0;
}