学海泛舟

spoj TRNGL - Make Triangle 题解

原题

传送门

题目大意

问对于一个凸n边形,使用n-3条不相交的对角线划分成若干个三角形,有多少种方式。

分析

对于这种三角形的划分,可以看到n边形的任意一条边都对应着一个三角形。选定一条边,然后根据剩下的一个点,枚举三角形,这时这个n边形就被划分成了几个比较小的多边形,所以递推式也就成功推出了。

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int mod=100007;
int n;
long long dp[1005];
int main(){
int T;
scanf("%d",&T);
memset(dp,0,sizeof dp);
dp[0]=1;
dp[1]=1;
dp[2]=1;
dp[3]=1;
dp[4]=2;
for(int i=5;i<=1000;++i){
for(int k=2;k<=i-1;++k){
dp[i]+=dp[k]*dp[i-k+1];
dp[i]%=mod;
}
}
while(T--){
scanf("%d",&n);
printf("%lld\n",dp[n]);
}
return 0;
}