hdu 1130 How Many Trees?(卡特兰数)

卡特兰数高精度。

View Code
 1 /*

 2 Author:Zhaofa Fang

 3 Lang:C++

 4 */

 5 #include <cstdio>

 6 #include <cstdlib>

 7 #include <sstream>

 8 #include <iostream>

 9 #include <cmath>

10 #include <cstring>

11 #include <algorithm>

12 #include <string>

13 #include <utility>

14 #include <vector>

15 #include <queue>

16 #include <stack>

17 #include <map>

18 #include <set>

19 using namespace std;

20 

21 typedef long long ll;

22 #define DEBUG(x) cout<< #x << ':' << x << endl

23 #define REP(i,n) for(int i=0;i < (n);i++)

24 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)

25 #define PII pair<int,int>

26 #define PB push_back

27 #define MP make_pair

28 #define ft first

29 #define sd second

30 #define lowbit(x) (x&(-x))

31 #define INF (1<<30)

32 

33 

34 int f[105][200];

35 void product(int a[],int b[],int c[])

36 {

37     REP(j,200)c[j]=0;

38     int e = 0;

39     int N = 200-1;

40     while(N>=0 && !b[N] )N--;

41     REP(k,N+1)

42     {

43         for(int j=0;j+k<200;j++)

44         {

45             int tmp = a[j] * b[k];

46             int tmp1 = tmp + c[j+k] + e;

47             c[j+k] = (tmp1)%10;

48             e = (tmp1)/10;

49         }

50     }

51 }

52 void print(int a[])

53 {

54     int k = 200 - 1;

55     while(k>=0 && !a[k])k--;

56     for(int i=k;i>=0;i--)printf("%d",a[i]);

57     puts("");

58 }

59 void pre(void)

60 {

61     f[3][0] = f[2][0] = 1;

62     int tmp[200];

63     FOR(i,4,102)

64     {

65         //puts("**********");

66         FOR(j,2,i-1)

67         {

68             product(f[j],f[i - j + 1],tmp);

69             //DEBUG(j);print(f[j]);

70             //DEBUG(i-j+1);print(f[i-j+1]);

71             //print(tmp);

72             int e = 0;

73             for(int k=0;k<200;k++)

74             {

75                 int tmp1 = f[i][k] + tmp[k] + e;

76                 f[i][k] = tmp1%10;

77                 e = tmp1/10;

78             }

79         }

80 

81     }

82 }

83 

84 int main()

85 {

86     //freopen("in","r",stdin);

87     pre();

88     int n;

89     while(~scanf("%d",&n))print(f[n+2]);

90     return 0;

91 }

你可能感兴趣的:(tree)