Java大整数实现计算catalan数

Problem: http://acm.sgu.ru/problem.php?contest=0&problem=130

Circle
On a circle border there are 2k different points A1, A2, ..., A2k, located contiguously. These points connect k chords so that each of points A1, A2, ..., A2k is the end point of one chord. Chords divide the circle into parts. You have to find N - the number of different ways to connect the points so that the circle is broken into minimal possible amount of parts P.

Input      k

Output  two numbers N and P delimited by space.

Sample Input

2

Sample Output

2 3

// Sgu  130. Circle   ( catalan number )
import  java.util. * ;
 
public   class  Solution  {      
     
public static long  catalan(int n)  // MAX n=33
     {
         
if(n==0)    return 1;
         
else return (4*n-2)*catalan(n-1)/(n+1);
     }

     
public static void main(String args[]) throws Exception
        
{
             Scanner cin
=new Scanner(System.in);
             
int k= cin.nextInt();
             
long s=catalan(k);
             System.out.println(s
+" "+(k+1));             
             
return;
         }

    }


     
//  f(n)= C(2n,n)/(n+1)  BigInteger
      public   static  BigInteger cat( int  n)  {
         
if (n==1 || n==0)
             
return BigInteger.ONE;
         BigInteger cat
=new BigInteger(Integer.toString(2*n));
         BigInteger twice
=new BigInteger(Integer.toString(2*n));
         BigInteger i 
=new BigInteger("1");
         
for (int j=1; j<n; j++, i=i.add(BigInteger.ONE) )
             cat
=cat.multiply(twice.subtract(i));
         i
=i.add(BigInteger.ONE);  // i=n+1;
         for (int j=n+1; j>=1; i=i.subtract(BigInteger.ONE),j--)
             cat
=cat.divide(i);
         
return cat;
     }
 

width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://download1.csdn.net/down3/20070601/01184120111.htm" marginheight="0" marginwidth="0">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值