lightoj 1045 Digits of Factorial (数学)

来源:互联网 发布:tekla软件好学吗 编辑:程序博客网 时间:2024/06/07 15:08

求N的阶乘在base进制下是几位数。

所有进制中,base^m+c=n    m是一个阶数,c是一个小于base^m的常数,n是目标数字。

     如  10^2+23=123   

     故可看出阶数永远比目标数以base为底取对数小1

因此:可得到一个公式 log(base)(N!)=answer      base是底

由换底公式可得    log(N!)/log(base)=answer 

又由 log(a*b)=log(a)+log(b) --->log(N!)=log1+log2+......+logN


完整代码

import java.util.*;public class Main{static Scanner sc= new Scanner(System.in);static double [] a=new double[1000010];    public static void main(String[] args) {        int t,n;       double base,ans;       for(int i=1;i<1000010;i++)       a[i]=Math.log10(i)+a[i-1];       t=sc.nextInt();       for(int j=1;j<=t;j++) {       n=sc.nextInt();       base=sc.nextInt();       ans=a[n]/Math.log10(base)+1;          System.out.println("Case "+j+": "+(int)ans);       }       System.exit(0);    }    }

这题java交的话,不加System.exit(0) 会超内存

System.exit(0)功能在前面一篇字典树的文中提过了。。。

当时就是java过不了这题,然后用c++过了,之后字典树那题知道了有这个操作。。。才重新交了一遍

阅读全文
0 0
原创粉丝点击