During the National Day, the provincial capital Hz just held a grand collective wedding. in order to enrich the wedding, the SI Yi temporarily came up with an interesting program called "test groom ", the specific operation is as follows:
First, dress almost identical to each bride and place them in a row with a big red cover;
Then, let the groom look for his/her own bride. Each person is only allowed to find one, and many people are not allowed to find one.
Finally, uncover the cover. If you find the wrong object, you have to kneel in front of the public...
It seems that it is not easy to be a groom...
Suppose there are n newly-married couples, and m of them are looking for the wrong bride. How many possibilities does this happen.
The first line of input data is an integer c, indicating the number of test instances, followed by data in Row C, each row contains two integers, N and M (1 <m <= n <= 20 ).
Output for each test instance, please output a total of how many possibilities this situation may occur, the output of each instance occupies a line.
Sample Input
22 23 2
Sample output
13
Authorlcy
Solution:
Suppose there are n newly-married couples, and m of them are looking for the wrong bride. How many possibilities does this happen.
First, select m from N grooms and multiply them by the dislocation of M.
Note that the range of error Row 20 is greater than that of int type...
Code:
#include <iostream>using namespace std;const int maxn=21;typedef long long ll;ll f[maxn];ll c[maxn][maxn];void init(){ f[0]=1,f[1]=0; for(int i=2;i<maxn;i++) f[i]=(i-1)*(f[i-1]+f[i-2]); c[0][0]=1;c[1][0]=1;c[1][1]=1; for(int i=2;i<maxn;i++) { c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) c[i][j]=c[i-1][j]+c[i-1][j-1]; }}int main(){ init(); int t,n,m; cin>>t; while(t--) { cin>>n>>m; cout<<c[n][m]*f[m]<<endl; } return 0;}