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;
     }
 

你可能感兴趣的:(java,exception,input,border,output,Numbers)