lightoj 1045 - Digits of Factorial 取对数
2013-10-22 13:17
429 查看
题意:求n!的长度。
题解:我们需要知道log10(n)=a+b(a是整数,b是小于1的小数)。则a是n在十进制下的长度-1。为什么?根据性质就可以推出来,10^(a+b)=10^a*10^b,10^b必定小于10,大于等于1。接下来就简单,log(2,10)=log10/log2。所以p=log(n!)/log(base)=(log(1)+log(2)+...+log(n))/log(base)=sum
/(sum[base]-sum[base-1])-->ans=(int)p+1。其中sum[i]=log(1)+log(2)+...+log(i),预处理下。
题解:我们需要知道log10(n)=a+b(a是整数,b是小于1的小数)。则a是n在十进制下的长度-1。为什么?根据性质就可以推出来,10^(a+b)=10^a*10^b,10^b必定小于10,大于等于1。接下来就简单,log(2,10)=log10/log2。所以p=log(n!)/log(base)=(log(1)+log(2)+...+log(n))/log(base)=sum
/(sum[base]-sum[base-1])-->ans=(int)p+1。其中sum[i]=log(1)+log(2)+...+log(i),预处理下。
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int maxn=1e6+10; double sum[maxn]; void init() { int i,j,k; sum[0]=0; for(i=1;i<=1e6;i++) sum[i]=log(1.0*i)+sum[i-1]; } int main() { init(); int T,n,base,tt=0; cin>>T; while(T--) { cin>>n>>base; cout<<"Case "<<++tt<<": "<<(int)(sum /(sum[base]-sum[base-1])+1)<<endl; } return 0; }
相关文章推荐
- LIGHTOJ 1045 - Digits of Factorial 【阶乘取对数】
- LightOJ 1045 Digits of Factorial(大数进制)
- 【LightOJ 1045 Digits of Factorial】
- LightOJ 1045 Digits of Factorial【进制问题】
- 【LightOJ】1045 - Digits of Factorial(数论)
- lightoj1045 - Digits of Factorial
- lightoj 1045 - Digits of Factorial (数学-log运用--阶乘进制位数)
- LightOJ 1045 Digits of Factorial
- lightOJ 1045 Digits of Factorial (数位计数)
- LightOJ - 1045 Digits of Factorial
- LightOJ 1045 Digits of Factorial
- lightoj1045 - Digits of Factorial
- lightoj 1045 - Digits of Factorial 【数学】
- LightOJ 1045 Digits of Factorial
- light oj1045 - Digits of Factorial (阶乘的位数,,运用对数)
- Light OJ:1045 Digits of Factorial(数学+思维+对数)
- lightoj 1045 - Digits of Factorial
- LightOJ 1045 - Digits of Factorial 简单数论
- Lightoj1045 Digits of Factorial(数论)
- LightOJ1045 Digits of Factorial 求n的阶乘在k进制下的位数