火车进出栈问题

本文探讨了卡特兰数在多个领域的应用,包括满二叉树结构、凸多边形三角剖分、格子路径计数、不相交线段组合以及阶梯状图形填充方法的数量计算。通过深入解析卡特兰数的数学意义及其在实际问题中的体现,本文提供了理解和计算卡特兰数的有效方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

卡特兰数应用

求满二叉树有多少种结构,凸多边形三角剖分,在n*n的格子中,只在下三角行走,每次横或竖走一格,有多少中走法,在圆上选择2n个点,将这些点成对连接起来使得所得到的n条线段不相交的方法数,n个长方形填充一个高度为n的阶梯状图形的方法个数

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package javaapplication2;

/**
 *
 * @author asus
 */
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {

    static final int maxn = 120050;
    static boolean[] isprime = new boolean[maxn];
    static int[] prime = new int[maxn];
    static int pz = 0;

    static void getPrime() {
        for (int i = 2; i < maxn; ++i) {
            isprime[i] = true;
        }
        for (int i = 2; i < maxn; ++i) {
            if (isprime[i]) {
                prime[pz++] = i;
            }
            for (int j = 0; j < pz && (long) i * prime[j] < maxn; ++j) {
                isprime[i * prime[j]] = false;
                if (i % prime[j] == 0) {
                    break;
                }
            }
        }
    }
    static Scanner cin = new Scanner(System.in);
    static int n = 0;

    public static void main(String[] args) {
        n = cin.nextInt();
        getPrime();
        BigInteger ans=B(1);
        for(int i = 0; i < pz&&prime[i]<=2*n; ++i) {
            int cnt=0,p=prime[i];
            int t=n*2;
            while(t>0) {
                cnt+=t/p;
                t/=p;
            }
            t=n;
            while(t>0) {
                cnt-=t/p*2;
                t/=p;
            }
            ans=ans.multiply(pow(p,cnt));
        }
        System.out.println(ans.divide(B(n+1)));
        
        /*TLE
        BigInteger ans=B(1);
        
        for(int i=1;i<=n;i++) {
            ans=ans.multiply(B(4*i-2)).divide(B(i+1));
        }
        System.out.println(ans);*/
    }

    static BigInteger pow(int x, int n) {
        BigInteger ans = B(1), base = B(x);
        while (n > 0) {
            if (n % 2 == 1) {
                ans = ans.multiply(base);
            }
            base = base.multiply(base);
            n >>= 1;
        }
        return ans;
    }

    static BigInteger B(int x) {
        return BigInteger.valueOf(x);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值