卡特兰数

目录

CSU 1576 catalansqure

CSU 1320 Scoop water

HDU 1130 How Many Trees?


CSU 1576 catalansqure

题目:

Input


Output


Sample Input
59
Sample Output
1583850964596120042686772779038896

这个题目意思很简单,就是说,C是卡特兰数,求S

很明显,S就是卡特兰数,所以答案就是(n*2+2)! / (n+2)! / (n+1)!

题目就是输入n输出这个数即可。

不需要什么技巧,直接用java大数类即可。

代码:

import java.util.*;
import java.math.BigInteger;
public class Main{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n=Integer.parseInt(cin.nextLine());
        BigInteger s=new BigInteger("1");      
        for(int i=n+3;i<=n*2+2;i++)s=s.multiply(BigInteger.valueOf(i));
        for(int i=1;i<=n+1;i++)s=s.divide(BigInteger.valueOf(i));
        System.out.println(s.toString());
    }
}

CSU 1320 Scoop water

题目:

Description
  zzy今天刚买了两个水瓢A和B,容量都是为1升,童心未泯的他打算用这个水瓢来玩游戏。

  首先zzy准备了一个容量可看作无穷大的水缸,刚开始水缸是空的,然后用水瓢A往水缸里加水,用水瓢B把水缸里的水舀出去,当使用 水瓢B把水舀出去时水缸里必须要至少有1升的水。这样子使用N次水瓢A,也使用N次水瓢B,最后水缸会依旧空的。

Input
  输入有多个例子,直到文件结束。

  每个例子仅含一个数N(0<N<=10000),表示你必须使用N次A水瓢和N次B水瓢。

Output
  对于每个例子,请输出一个数,表示一共有多少种正确的舀水方式使得舀水过程中 使用B水瓢时水缸里总会有足够的水。

 (由于数字比较大,输出的答案模1000000007)

Sample Input
1
2
Sample Output
1
2


思路:其实就是卡特兰数,用递推式把前10000个卡特兰数都求出来即可

代码:
 

import java.util.*;
import java.math.BigInteger;
public class Main{
    public static void main(String[] args) {
    	BigInteger[] s=new BigInteger[10001];
    	s[1]=new BigInteger("1"); 
    	for(int i=2;i<=10000;i++)s[i]=s[i-1].multiply(BigInteger.valueOf(i*4-2)).divide(BigInteger.valueOf(i+1));
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext())
		{
    		int n=Integer.parseInt(cin.nextLine());
        	System.out.println(s[n].remainder(BigInteger.valueOf(1000000007)).toString());
		}    
    }
}

HDU 1130 How Many Trees?

题目:

Description

A binary search tree is a binary tree with root k such that any node v reachable from its left has label (v) <label (k) and any node w reachable from its right has label (w) > label (k). It is a search structure which can find a node with label x in O(n log n) average time, where n is the size of the tree (number of vertices). 

Given a number n, can you tell how many different binary search trees may be constructed with a set of numbers of size n such that each element of the set will be associated to the label of exactly one node in a binary search tree? 
Input

The input will contain a number 1 <= i <= 100 per line representing the number of elements of the set. 
Output

You have to print a line in the output for each entry with the answer to the previous question. 
Sample Input

1
2
3
Sample Output

1
2
5


递推式很明显,所以结果也很明显,就是卡特兰数。

这样的话,就和CSU 1789: catalansqure差不多了

代码:

import java.util.*;
import java.math.BigInteger;
public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext())
        {
        	int n=Integer.parseInt(cin.nextLine());
        	n--;
            BigInteger s=new BigInteger("1");      
            for(int i=n+3;i<=n*2+2;i++)s=s.multiply(BigInteger.valueOf(i));
            for(int i=1;i<=n+1;i++)s=s.divide(BigInteger.valueOf(i));
            System.out.println(s.toString());
        }
        
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值