P1044 栈(卡特兰数)

P1044 栈(卡特兰数)

题目传送门

思路:卡特兰数板子题,由于 n 18 n\leq18 ,而 n = 20 n=20 才会爆 i n t int ,但是要主要在中间乘法过程可能会爆 i n t int ,所以要一边除,一边乘。或者用 O ( n 2 ) O(n^2) 的递推公式也可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int mod=100;
typedef long long ll;
int main(){
	int n;
	scanf("%d",&n);
	ll a=1;
	for(int i=2,j=2;i<=n;i++){
		a*=(i+n);
		while(a%j==0&&j<=n){
			a/=j;
			j++;
		}
	}
	printf("%d\n",a);
	return 0;
} 
原创文章 201 获赞 165 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/106069477