卡特兰数之合法出栈序列

题目链接:
牛客acm训练题组
这里直接摘抄搜狗百科的一段关于出栈序列使用卡特兰数求解的推理,还是比较简单易懂的
在这里插入图片描述
那对于该题,最先开始的a不能先第一个出栈那就直接总的减去a是第一个出栈的情况即可得答案,即是f(n)-f(n-1)
代码:

// 卡特兰数.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

//#include "pch.h"
#include <iostream>
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int mod = 998244353;
const int M = 1e5+5;
typedef long long ll;
//ll C[M ][M ];
ll p[M];
ll edgcd(ll a, ll b, ll &x, ll &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll ans = edgcd(b, a%b, x, y);
	ll  temp = x;
	x = y;
	y = temp - (a / b)*y;
	return ans;
}
void inti()
{
	p[1] = p[0] = 1;
	for (int i = 2; i < M; i++)
	{
		ll x, y;
		edgcd(i + 1, mod, x, y);
		x = (x + mod) % mod;
		//if (i < 25)cout << "x="<<x << endl;
		//cout << x << endl;
		p[i] = (((p[i - 1]%mod) * ((4 * i - 2)%mod)%mod)*x)%mod;
		//if (i < 25)
			//cout << p[i]<< endl;
	}
}
int main()
{
	ll t;
	cin >> t;
	int i = 1;
	inti();
	while (t--)
	{
		int n;
		//cin >> n;
		scanf("%d", &n);
		//cout << p[n] << " " << p[n - 1] << endl;
		printf("Case #%d: %lld\n", i, (p[n] - p[n - 1] + 1ll*mod) % mod);
		i++;
	}
}
在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值