熟悉卡特兰数的人一眼就能看出这题该怎么做,恶心的是这题最大数据为35,结果在long long范围之内,可是如果用递推公式不当,运算过程中还会溢出。解决的办法是换一个递推公式,或者写成我下面这样
/*
* hdu2067/linux.c
* Created on: 2011-7-31
* Author : ben
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void work();
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}
int gcd(int a, int b) {
int c;
if (!a || !b) {
return a > b ? a : b;
}
c = a % b;
while (c) {
a = b;
b = c;
c = a % b;
}
return b;
}
void work() {
int n, T = 1, i, temp;
unsigned long long catalan[40] = { 1, 1, 2, 5, 14, 42 };
for (i = 5; i < 35; i++) {
temp = gcd(4 * i + 2, i + 2);
catalan[i + 1] = catalan[i] / ((i + 2) / temp) * ((4 * i + 2) / temp);
}
while (scanf("%d", &n) == 1 && n != -1) {
printf("%d %d %I64u\n", T++, n, catalan[n] * 2);
}
}