Hangzhou Electric Oj brush title (2049)

Not easy Series (4) - test Groom

Subject description:

During the National Day, the provincial capital HZ just held a grand mass wedding, the wedding of the rich in order to make some temporary master of ceremonies has come up with an interesting program, called "test the groom", the specific operation is as follows:


First, to every bride dressed almost exactly the same, and covered with big red scarf random sitting in a row;
then let you groom looking for his bride will only be allowed to find a person, and does not allow people to find a..
Finally, uncover hijab, if the wrong object will publicly kneel washboard ...

it seems to do the groom is not an easy thing ...

assuming that a total of N pairs of newlyweds, of which there are M bride and groom got it wrong, find it occurs how many cases a possible total.

Input

The first line of input data C is an integer, indicates the number of test cases, then the C line data, each line contains two integers N and M (1 <M <= N <= 20).

Output

For each test case, please output a number of species may happen a total output of each instance per line.

Sample Input

2 
2 2 
3 2

Sample Output

1 
3

analysis:

Staggered problem still present, but find M bridegroom from N (permutations) couples, and then multiplied by a [i]

By the answer:

#include<stdio.h>    
long long a[22];     //注意long long类型,不能太小       
int f(int n){        //递推
    a[0]=0;
	a[1]=0;          
	a[2]=1;
	a[3]=2;
	for(int i=4;i<=n;i++){
		a[i]=(i-1)*(a[i-1]+a[i-2]);     //错排公式 
	}
}   
long long JC(int m){        //排列组合 
	long long mul=1;
	for(int i=1;i<=m;i++){
		mul=mul*i;
	}
	return mul;
}           
int main(){
    int c,n,m,i; 
    while(scanf("%d",&c)!=EOF){
    	while(c--){
    		scanf("%d %d",&n,&m);
			f(n);
			long long mul=JC(n)/(JC(n-m)*JC(m));        //从N对新人中选出M个新郎的组合 
        	printf("%lld\n",mul*a[m]);        
		}  	
	}
    return 0;
}

 

Published 76 original articles · won praise 3 · Views 1878

Guess you like

Origin blog.csdn.net/ZhangShaoYan111/article/details/104240737