題目鏈接:ZOJ 2711 Regular Words (三維Catalan數)
題意:給出一串3*n長度的字符,其中是含有A,B,C,滿足以下條件的字符串有多少種。
1.字符串中A,B,C的個數相同。
2.該字符串的前綴中 A,B,C的個數成非遞減。
做法一:一個三維的Catalan數。
做法二:三維的DP。
AC代碼:
import java.math.*;
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
BigInteger a=null;
int n;
BigInteger f[]=new BigInteger[190];
f[1]=BigInteger.ONE;
int i;
for(i=2;i<=182;i++)
{
f[i]=f[i-1].multiply(BigInteger.valueOf(i));
}
while(input.hasNext())
{
n=input.nextInt();
if(n==0)
{
System.out.println(0);
continue;
}
a=BigInteger.valueOf(2);
a=a.multiply(f[n*3]);
a=a.divide(f[n]);
a=a.divide(f[n+1]);
a=a.divide(f[n+2]);
System.out.println(a);
System.out.println();
}
}
}