题意:n*n的棋盘,放入k个车,要使k个车不相互攻击,有多少种方案。
总结:从n行选出k行(C(n,k)),再从n列选出k列(A(n,k)),即C(n,k)*A(n,k)。 注:纠结是C还是A,举个简单的例子,看交换后是否相同。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define F(i,a,b) for (int i=a;i<b;i++) #define FF(i,a,b) for (int i=a;i<=b;i++) #define mes(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f typedef long long ll; const int N = 1e5+10; double C(double n, double m) { double sum_n = 1, sum_m = 1; for(double i=n; i>=n-m+1; i--) sum_n *= i; for(double i=m; i>=1; i--) sum_m *=i; return sum_n/sum_m; } double A(double n, double m) { double sum_n = 1; for(double i=n; i>=n-m+1; i--) sum_n *= i; return sum_n; } int main() { int T; double n, k; cin>>T; FF(cas,1,T) { cin>>n>>k; printf("Case %d: %.0f ", cas, C(n, k)*A(n, k)); } return 0; }