关于n!和n^n的数位以及各个位上的数(stirling公式)

本文探讨了Stirling公式在求解大数n!与n^n的数位特性中的应用。通过公式变形,可以计算n^n的位数k=[n*lg(n)]+1,并确定最左边的数。对于n!,使用Stirling公式进行近似,得到lg(n!)的表达式,进而计算n!的最左位。题目要求计算n^n的最左边数字,利用对数和向下取整运算可以解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

hdu 1060   leftest digit

题意:求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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值