/* 这题是使用了动态规划算法,思路很是巧妙 动态规划的思路讲解,这个博客比较详细: http://blog.csdn.net/metaphysis/article/details/6566478 代码的简洁优雅,这个博客更胜一筹: http://blog.csdn.net/keshuai19940722/article/details/14230003 */ #include <iostream> #include <cstring> using namespace std; const int N = 20; typedef long long ll; ll dp[N][N][N]; //dp[i][j][k]表示i人站队,从前看有j个人,从后看有k个人的情况数 void init() { memset(dp, 0, sizeof(dp)); dp[1][1][1] = 1; for ( int i = 2; i <= 13; i++ ) for ( int j = 1; j <= i; j++ ) { int top = i - j + 1; for ( int k = 1; k <= top; k++ ) dp[i][j][k] = dp[i - 1][j][k] * ( i - 2 ) + dp[i - 1][j - 1][k] + dp[i - 1][j][k - 1]; } } int main() { init(); int k, n, p, r; cin >> k; while (k--) { cin >> n >> p >> r; cout << dp[n][p][r] << endl; } return 0; }