language: C++ (gcc-4.3.4)
date: 395 days 20 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstring>
 
using namespace std;
 
const int M = 1051962371;
const int N = 100;
 
long long dp[N+1][N+1][N+1];
 
long long go(int n, int r, int target) {
  if (n == 0) return (target == 0 ? 1 : 0);
  if (dp[n][r][target] >= 0) return dp[n][r][target];
 
  int w = n-r;
  long long ways = 0;
  if (r > 0) ways += go(n-1, r-1, target > 0 ? target-1 : 0), ways %= M;
  if (r > 1) ways += (r-1) * go(n-1, r-2, target), ways %= M;
  if (w > 0) ways += w * go(n-1, r > 0 ? r-1 : 0, target), ways %= M;
  return dp[n][r][target] = ways;
}
 
int main() {
  memset(dp, 0xff, sizeof(dp));
  int n; cin >> n;
  while (n--) {
    int g, c; cin >> g >> c;
    cout << go(g, g, c) << endl;
  }
  return 0;
}