使用n!近似公式算一下就可以了
注意特判
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; const long double Pi=acos(-1),e=exp(1),eps=1e-10; long long n,k; long double log(long double a,long double b){return log(a)/log(b);} int main(){ freopen("3000.in","r",stdin); freopen("3000.out","w",stdout); while(scanf("%lld %lld",&n,&k)!=EOF){ double ans=0; if(n<=10000){ for(int i=1;i<=n;i++)ans+=log(i); ans/=log(k); ans=ceil(ans+eps); printf("%.0lf\n",ans); } else printf("%lld\n",(long long)(0.5*log(2*Pi*n,k)+n*log(n,k)-n*log(e,k)+1)); } return 0; }