http://acm.timus.ru/problem.aspx?space=1&num=1172
水题DP 大整数直接上java
代码:
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
static final int N = 35;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
BigInteger[][][][] dp = new BigInteger[N][N][N][4];
int n = in.nextInt();
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int l = 0; l <= n; ++l) {
for (int k = 1; k <= 3; ++k) {
dp[i][j][l][k]=BigInteger.ZERO;
}
}
}
}
dp[0][0][0][1] = BigInteger.ONE;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int l = 0; l <= n; ++l) {
for (int k = 1; k <= 3; ++k) {
if (dp[i][j][l][k].compareTo(BigInteger.ZERO) == 1) {
if (k != 1 && i < n) {
dp[i + 1][j][l][1] = dp[i + 1][j][l][1]
.add(dp[i][j][l][k].multiply(BigInteger
.valueOf(Math.max(1, n - i - 1))));
}
if (k != 2 && j < n) {
dp[i][j + 1][l][2] = dp[i][j + 1][l][2]
.add(dp[i][j][l][k].multiply(BigInteger
.valueOf(n - j)));
}
if (k != 3 && l < n) {
dp[i][j][l + 1][3] = dp[i][j][l + 1][3]
.add(dp[i][j][l][k].multiply(BigInteger
.valueOf(n - l)));
}
}
}
}
}
}
System.out.println(dp[n][n][n][1].divide(BigInteger.valueOf(2L)));
}
}