130. Circle
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
On a circle border there are 2k different points A1, A2, ..., A2k, located contiguously. These points connect k chords so that each of points A1, A2, ..., A2k is the end point of one chord. Chords divide the circle into parts. You have to find N - the number of different ways to connect the points so that the circle is broken into minimal possible amount of parts P.
Input
The first line contains the integer k (1 <= k <= 30).
Output
The first line should contain two numbers N and P delimited by space.
Sample Input
2
Sample Output
2 3
题目大意很简单,就是一个圆上等距离分布着2k个点,求不同的匹配总数和所能分割成的最小块数。
第二问很简单,直接k+1即可(证明略)。
对于第一问,我们设f[i]表示2i个点时不同的匹配总数。f[0]=f[1]=1。
我们连一条弦使得左右两边分离,即使有交叉的将交叉的一部分归为左边或右边即可。
所以f[i]=f[0]*f[i-1]+f[1]*f[i-2]+f[2]*f[i-3]+......+f[i-1]*f[0],恰好是卡特兰数,于是可以用公式得ans=C(2k, k)/(k+1).
虽然不难,但题也不错。#include <cstdio> using namespace std; long long solve(int k) { bool hash[35] = {0}; long long re = 1; for(int i = k<<1; i > k; --i) { re *= i; for(int j = 1; j <= k; ++j) if(!hash[j] && re%j == 0) { re /= j; hash[j] = true; } } for(int i = 1; i <= k; ++i) if(!hash[i]) re /= i; return re; } int main() { int k; scanf("%d", &k); printf("%I64d %d\n", solve(k)/(k+1), k+1); return 0; }