http://acm.hdu.edu.cn/showproblem.php?pid=2049
这一题和上一题的思路差不多,但是这一题需要对N和M排列,即在N对中选M对进行错排。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
long[] d=new long[25];
long[] c=new long[25];
d[1]=0;c[1]=1;c[0]=1;
d[2]=1;c[2]=2;
for(int i=3;i<21;i++){
d[i]=(i-1)*(d[i-1]+d[i-2]);
c[i]=c[i-1]*i;
}
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n-->0){
int a=sc.nextInt();
int b=sc.nextInt();
long x=c[a]/(c[b]*c[a-b]);
System.out.println(x*d[b]);
}
}
}