题目:http://www.lightoj.com/volume_showproblem.php?problem=1005
题意:给定一个n * n的棋盘,往上面放k个棋子,棋子可以攻击所在的行或列,求把k个棋子放在棋盘上使任意两个不能互相攻击的方案数
思路:有两种解法,dp或者组合数学
一:dp方法
设dp[i][j]为前i行放j个棋子时满足条件的方案数,dp[i][0]置为1,可以得到状态转移方程dp[i][j] = dp[i-1][j] + dp[i-1][j-1] * (n- (j-1))。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define debug() puts("here")
using namespace std;
typedef long long ll;
const int N = 110;
int cas;
ll dp[N][N];
int main()
{
int t, n, k;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &k);
memset(dp, 0, sizeof dp);
dp[0][0] = 1;
for(int i = 1; i <=