Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7647 | Accepted: 3865 |
Description
Input
Output
Sample Input
2 3 -1
Sample Output
2 5
题意:一圆环上有2n个点,求两两连线且不交叉的方法数。
思路:卡特兰数
import java.io.*; import java.util.*; import java.math.*; public class Main { public static BigInteger fac(BigInteger n){ BigInteger a=new BigInteger("1"); BigInteger zero=new BigInteger("0"); if(n.equals(a)||n.equals(zero)) return a; return n.multiply(fac(n.subtract(a))); } public static BigInteger C(BigInteger n,BigInteger k){ return fac(n).divide(fac(k).multiply(fac(n.subtract(k)))); } public static BigInteger Catlan(BigInteger n){ BigInteger a=new BigInteger("1"); return C(n.add(n),n).divide(n.add(a)); } public static void main(String[] args){ Scanner in=new Scanner(System.in); BigInteger n; BigInteger two=new BigInteger("2"); BigInteger end=new BigInteger("-1"); while(in.hasNext()){ n=in.nextBigInteger(); if(n.equals(end)) break; System.out.println(Catlan(n).toString()); } } }
原文:http://www.cnblogs.com/--560/p/4362588.html