题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1005
代码:
//先在n行中选出k行,C(n,k),再在n列中选出k列随便放A(n,k),答案为C(n,k)*A(n,k)
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>
#include<stdexcept>
#include<iomanip>
#include<iterator>
using namespace std;
int t;
int n, k;
long long a[35][35], c[35][35];
void solve()
{
for (int i = 1; i <= 30; i++)
{
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++)
{
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
for (int i = 0; i <= 30; i++)
{
a[i][0] = 1;
a[i][1] = i;
for (int j = 2; j <= i; j++)
a[i][j] = a[i][j - 1] * (long long)(i - j + 1);
}
}
int main()
{
int cases = 1;
scanf("%d",&t);
solve();
while (t--)
{
scanf("%d%d", &n, &k);
printf("Case %d: ", cases++);
long long ans = 0 ;
if (k > n) { printf("0\n"); continue; }
ans = a[n][k] * c[n][k];
printf("%lld\n",ans);
}
return 0;
}