light oj --Digits of Factorial (一个数的位数问题以及log的公式应用)
2015-04-03 00:12
381 查看
这是一道数学题。
假设N! 等于 NUM
对于 N!= NUM 做恒等变形
![](https://img-blog.csdn.net/20140429150311625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm9seV95dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
(ans向下取整)
![](https://img-blog.csdn.net/20140429150408468?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm9seV95dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20140429150442656?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm9seV95dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20140429150608703?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcm9seV95dQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
最终的结果等于ans+1
题解:我们需要知道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;
}
假设N! 等于 NUM
对于 N!= NUM 做恒等变形
(ans向下取整)
最终的结果等于ans+1
题解:我们需要知道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;
}
相关文章推荐
- Light OJ 1045 Digits of Factorial(求位数)
- lightoj 1045 - Digits of Factorial (数学-log运用--阶乘进制位数)
- light oj1045 - Digits of Factorial (阶乘的位数,,运用对数)
- 最近做了一个iOS小应用,记录下遇到的问题以及解决方法
- Light-oj-1045 lightoj1045 - Digits of Factorial (N!不同进制的位数)
- 一个简单的安卓小应用,开发步骤以及遇到的各种问题总结
- 【Ligth-oj】-1045 - Digits of Factorial(数论,log,好)
- LightOJ1045 Digits of Factorial 求n的阶乘在k进制下的位数
- Light OJ 1045 - Digits of Factorial
- Light OJ:1045 Digits of Factorial(数学+思维+对数)
- Digits of Factorial (求N!的k进制位数)(打表)
- 关于LWIP在应用中遇到的一个问题memp_malloc: out of memory in pool TCP_PCB
- LightOJ 1045 Digits of Factorial【进制问题】
- WPF布局控件AvalonDock介绍以及应用中遇到的一个问题
- 在IE里应用XML的一个小例子:解决双下拉选单的连动问题。
- 一个分布式应用的常见的设计问题
- 一个索引问题导致大量LOG产生,填满磁盘——ORA-08102
- Unable to Log You on Because of an Account Restriction问题的解决。
- Retargeting:经典动态规划问题的一个出人意料的应用
- 小程序大问题,MSDN中一个小小示例所带来的疑问,一个关于DataList的一个简单应用