lightoj1045 - Digits of Factorial(数论)
2014-03-20 23:07
337 查看
题目地址: http://lightoj.com/volume_showproblem.php?problem=1045
Factorial of an integer is defined by the following function
f(0) = 1
f(n) = f(n - 1) * n, if(n> 0)
So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170.
In this problem, you have to find the number of digit(s) of the factorial of an integer in a certain base.
Each case begins with two integers n (0 ≤ n ≤ 106) andbase (2 ≤ base ≤ 1000). Both of these integers will be given in decimal.
Sample Input
5
5 10
8 10
22 3
1000000 2
0 100
Output for Sample Input
Case 1: 3
Case 2: 5
Case 3: 45
Case 4: 18488885
Case 5: 1
本题的大概题意是说,N是十进制的数,求N!在K进制下的位数。
N的范围虽然不大,才10^6,但是N!却大得惊人。如果直接求N的阶乘,转化为K进制的数再统计位数,理论上运用高精度
算法行得通。但是时间只有2s,就肯定过不了。看了Forum才知道有一种很高效的解法。
计算N!在K进制下的位数,即计算 [ log(1)+log(2)+...+log(N) ]+1 其中log的底数都是K。在此要先了解计算机是怎么表示对数的。计算机的log默认为自然对数,即以e为底。 或者log10(a), 就是以10为底,其他的都得通过换底公式来表示。
loga(b)=logc(b)/logc(a)
N!在十进制下的位数就是 log10 ( N ! ) +1 ( 自己找个数验证 ) ,N!在K进制下的位数就是 logK( N ! ) +1 ( K 为底数 ),而计算机不能直接以K为底求对数。所以运用换底公式, logK( N ! ) = log( K ) / log ( N!) ,注意,等号右边的 log 都是默认以e为底。
正式进入解题了,题目给出N,K,如果每一次输入N,K 都要计算 N!的话,那就有很大的开销,开销为O(N)。
所以可以采取一种办法,先预处理,用double数组 sum[ 1000000 ] 把 log ( N ! ) 存起来。数组 应该够的,需要说的是定义大数组尽量放到外面,这样比较妥当,要不运行时会出错。
用sum [ i ] 表示 log(1 )+log(2)+。。。+log(i) , 这样时间开始时花费O(N),之后每次花费O(1),等输入的时候用换底公式处理一下就得到答案了。
具体代码如下:
糊里糊涂写了一堆,参考了别人的代码才AC的,希望能帮助到各位。 Hope it help!
如果不足,还望指出。
2014,继续AC。
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
f(0) = 1
f(n) = f(n - 1) * n, if(n> 0)
So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 is 170.
In this problem, you have to find the number of digit(s) of the factorial of an integer in a certain base.
Input
Input starts with an integer T (≤ 50000), denoting the number of test cases.Each case begins with two integers n (0 ≤ n ≤ 106) andbase (2 ≤ base ≤ 1000). Both of these integers will be given in decimal.
Output
For each case of input you have to print the case number and the digit(s) of factorial n in the given base.Sample Input
5
5 10
8 10
22 3
1000000 2
0 100
Output for Sample Input
Case 1: 3
Case 2: 5
Case 3: 45
Case 4: 18488885
Case 5: 1
本题的大概题意是说,N是十进制的数,求N!在K进制下的位数。
N的范围虽然不大,才10^6,但是N!却大得惊人。如果直接求N的阶乘,转化为K进制的数再统计位数,理论上运用高精度
算法行得通。但是时间只有2s,就肯定过不了。看了Forum才知道有一种很高效的解法。
计算N!在K进制下的位数,即计算 [ log(1)+log(2)+...+log(N) ]+1 其中log的底数都是K。在此要先了解计算机是怎么表示对数的。计算机的log默认为自然对数,即以e为底。 或者log10(a), 就是以10为底,其他的都得通过换底公式来表示。
loga(b)=logc(b)/logc(a)
N!在十进制下的位数就是 log10 ( N ! ) +1 ( 自己找个数验证 ) ,N!在K进制下的位数就是 logK( N ! ) +1 ( K 为底数 ),而计算机不能直接以K为底求对数。所以运用换底公式, logK( N ! ) = log( K ) / log ( N!) ,注意,等号右边的 log 都是默认以e为底。
正式进入解题了,题目给出N,K,如果每一次输入N,K 都要计算 N!的话,那就有很大的开销,开销为O(N)。
所以可以采取一种办法,先预处理,用double数组 sum[ 1000000 ] 把 log ( N ! ) 存起来。数组 应该够的,需要说的是定义大数组尽量放到外面,这样比较妥当,要不运行时会出错。
用sum [ i ] 表示 log(1 )+log(2)+。。。+log(i) , 这样时间开始时花费O(N),之后每次花费O(1),等输入的时候用换底公式处理一下就得到答案了。
具体代码如下:
<span style="font-size:18px;">#include <cstdio> #include <cmath> #include <string.h> using namespace std; double sum[1000008]; int main() { //freopen("in.txt","r",stdin); double res; int Case, n, a, i; int num = 1; memset(sum, 0, sizeof(sum)); for(i = 1; i < 1000008; i++) sum[i] = sum[i-1]+log(i); scanf("%d", &Case); while(Case--) { scanf("%d %d", &n, &a); if(n == 0) printf("Case %d: 1\n", num++); else { res = sum ; res = res/log(a)+1; printf("Case %d: %d\n", num++, (int)res); } } return 0; } </span>
糊里糊涂写了一堆,参考了别人的代码才AC的,希望能帮助到各位。 Hope it help!
如果不足,还望指出。
2014,继续AC。
相关文章推荐
- CV_EXPORT定义的作用,lib及dll的区别
- 强制重启N种方法
- 强制重启N种方法
- LeetCode 各题目难度已经面试频率
- 在php中集成xdebug(windows平台)
- 添加web引用和添加服务引用有什么区别?
- NYOJ NO.305 表达式求值
- netbeans跟eclipse的细节对比
- 003_002 Python 获取上一个周几,如周五,星期五
- GridView的各种用法
- 除了重装,解决问题的另一手段
- coursera 中计算概论的第四周 B-03作业
- SQL的内连接与外连接
- BAT 批处理脚本 教程
- 前 言
- OpenCV学习笔记(四十)——再谈OpenCV数据结构Mat详解
- 跨越国度
- 蓝桥杯 奇怪的比赛
- 如何重新配置Oracle的EM Database Control
- 嵌入式C/C++开发中的代码优化(翻译版)