了解BigInteger类的使用——蓝桥杯公式求值问题(只对了一小部分)

了解BigInteger类的使用——蓝桥杯公式求值问题(只对了一小部分)

了解BigInteger类的使用——蓝桥杯公式求值问题(只对了一小部分)

当数据范围像上述题目中这么大时,不能用递归一层一层的去做,很容易堆栈溢出(个人教训及感觉)

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    static String n,m;
    static int k;
    static BigInteger count=new BigInteger("0");
    public  static void main(String[] args) {
         Scanner sc=new Scanner(System.in);
         n=sc.nextLine();
         m=sc.nextLine();
         k=sc.nextInt();
         BigInteger div=new BigInteger("999101");
         BigInteger bn=new BigInteger(n);
         BigInteger bm=new BigInteger(m);
         BigInteger add=new BigInteger("0");
         for(;add.compareTo(bn)<=0;add=add.add(new BigInteger("1"))) {
            count=count.add(c_nm(bn,add).multiply(c_nm(bn,bm)).multiply(add.pow(k)));
         }
         System.out.println(count.remainder(div));
    }
    public static BigInteger c_nm(BigInteger n,BigInteger m) {
        /*if(n.compareTo(new BigInteger("0"))<=0||m.compareTo(new BigInteger("0"))<=0||n.compareTo(m)==0)
            return (new BigInteger("1"));
        else {
            return c_nm(n.subtract(new BigInteger("1")),m.subtract(new BigInteger("1"))).add(c_nm(n.subtract(new BigInteger("1")),m));
        }*///以上为递归思路,但不适用BigInteger类这么大的数据,所以采用了普通的算法,但也只过了18%的数据,看了网上的解,应该是需要用dp才能做出来,唉~
        BigInteger bc=new BigInteger("0");
        bc=jc(n).divide(jc(m).multiply(jc(n.subtract(m))));
        return bc;
    }
    public static BigInteger jc(BigInteger n) {
        BigInteger bs=new BigInteger("1");
        while(n.compareTo(new BigInteger("0"))>0) {
            bs=bs.multiply(n);
            n=n.subtract(new BigInteger("1"));
        }
        return bs;
    }
}