本文章学习于 acdreamers
二项式定理:
这个题因为n太大了,所以用java大数写
在递归的时候 记忆化一下
import java.math.*;
import java.util.*;
public class Main {
public static final int N = 105;
public static final BigInteger FLAG = (BigInteger.ZERO).subtract(BigInteger.ONE); // -1
public static BigInteger[][] C = new BigInteger[N][N];
public static BigInteger[] ans = new BigInteger[N];
public static void Init(){
for(int i = 0; i < N; i++){
C[i][0] = C[i][i] = BigInteger.ONE;
if(i == 0){
continue;
}
for(int j = 1; j < i; j++){
C[i][j] = C[i-1][j].add(C[i-1][j-1]);
}
}
}
public static BigInteger slove(BigInteger n, int k){
if(ans[k].compareTo(FLAG) != 0){
return ans[k];
}
if(k == 1){
ans[k] = ((n.add(BigInteger.ONE)).multiply(n)).divide(BigInteger.valueOf(2));
return ans[k];
}
BigInteger temp = BigInteger.ONE;
for(int i = 0; i < k + 1; i++){
temp = temp.multiply(n.add(BigInteger.ONE));
}
temp = temp.subtract(n.add(BigInteger.ONE));
BigInteger sum = BigInteger.ZERO;
for(int i = 1; i < k; i++){
BigInteger t = C[k+1][i+1].multiply(slove(n, k - i));
sum = sum.add(t);
}
ans[k] = (temp.subtract(sum)).divide(BigInteger.valueOf(k+1));
return ans[k];
}
public static void main(String[] args) {
Init();
Scanner cin = new Scanner(System.in);
while (cin.hasNext()){
BigInteger n = cin.nextBigInteger();
int k = cin.nextInt();
for(int i = 0; i < N; i++){
ans[i] = FLAG;
}
System.out.println(slove(n, k));
}
}
}