题目链接:
牛客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++;
}
}
在这里插入代码片