P1044 栈
题目链接:https://www.luogu.com.cn/problem/P1044
思路
本人使用的是递推方式求卡特兰数。
如果不懂卡特兰数,自行在B栈上搜卡特兰数
代码
#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define PI acos(-1)
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;
int N;
ll f[maxn];
int main(){
cin>>N;
f[0] = 1;f[1] = 1;
for(int i = 2;i<=N;i++){
for(int j = 0;j<=i-1;j++){
f[i] += f[j] * f[i-1-j];
}
}
cout<<f[N]<<'\n';
return 0;
}

京公网安备 11010502036488号