pku3331

来源:互联网 发布:中国网络焦点台 编辑:程序博客网 时间:2024/05/14 18:16

 统计n的阶乘中某个数字的出现次数,没有好的办法只好硬算,可是采用C++时数据溢出导致结果错误,这是想到Java,就求助于Java的BigInteger了,不错,并没有TLE.

Source:

import java.math.BigInteger;

import java.util.Scanner;

 

 

public class Main {

 

   

    /**

     * @param args

     */

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Scanner sc=new Scanner(System.in);

       BigInteger f=new BigInteger("1");

       int t,n,d,i,r;

       t=sc.nextInt();

       for(;t>0;--t)

       {

           f=BigInteger.ONE;

           n=sc.nextInt();

           d=sc.nextInt();

           for(i=2;i<=n;++i)

           {

              f=f.multiply(new BigInteger(String.valueOf(i)));

           }

           for(r=0;!f.equals(BigInteger.ZERO);f=f.divide(BigInteger.TEN))

           {

              if((f.mod(BigInteger.TEN)).intValue()==d)++r;

           }

           System.out.println(r);

       }

 

    }

 

}

 

原创粉丝点击