POJ 2084 Game of Connections 卡特兰数+JAVA -

来源:互联网 发布:mysql触发器的作用 编辑:程序博客网 时间:2024/04/29 16:59

题目地址:http://poj.org/problem?id=2084

递推地方法:

假设有n对点,答案为f(n)

假设第一个点连接了第2*k+1个点,那么此时答案为f(k)*f(n-k-1),左边k对点和右边n-k-1对点

利用加法原理,n对点,一共有∑f(k)*f(n-k-1) {k>=0&&k<n}

也就是卡特兰数

利用BigInteger就好了

import java.util.*;import java.math.*;import java.text.*;import java.io.*;public class Main{public static void main(String[] args) {Scanner cin = new Scanner(new BufferedInputStream(System.in));BigInteger[] d = new BigInteger[100+5];d[0] = BigInteger.ONE;d[1] = BigInteger.ONE;d[2] = BigInteger.valueOf(2);for(int i=3;i<=100;i++){BigInteger temp=BigInteger.ZERO;for(int k=0;k<=i-1;k++) temp=temp.add(d[k].multiply(d[i-k-1]));d[i]=temp;}while(cin.hasNext()){int n=cin.nextInt();if(n==-1) break;System.out.println(d[n]);}}}



0 0
原创粉丝点击