wenbao与卡特兰数

打表 

 

 1 LL h[maxn];
 2 
 3 LL exp_mod(LL a){
 4     LL b = MOD - 2;
 5     LL res = 1;
 6     while (b){
 7         if (b & 1)
 8             res = (res*a) % MOD;
 9         b >>= 1;
10         a = (a*a) % MOD;
11     }
12     return res;
13 }
14 
15 void init(){
16     LL i;
17     h[0] = h[1] = 1;
18     for (i = 2; i < maxn; i++)
19         h[i] = (((4 * i - 2) % MOD * exp_mod(i + 1)) % MOD * h[i - 1]) % MOD;
20 }

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------

 

http://acm.hdu.edu.cn/showproblem.php?pid=1023

卡特兰数,非常好的一个练习大数的题目

 

说到大数当然首推java(python忽略)

 

 1 import java.util.*;
 2 import java.math.*;
 3 public class Main{
 4     private static BigInteger a[] = new BigInteger [105];
 5     public static void init(){
 6         BigInteger x = new BigInteger ("1");
 7         a[0] = x;
 8         for(int i = 1; i <= 100; ++i){
 9             Integer xxx = 4*i-2;
10             Integer y = i+1;
11             BigInteger xx = new BigInteger(xxx.toString()), yy = new BigInteger(y.toString());
12             a[i] = (a[i-1].multiply(xx)).divide(yy);
13             //System.out.println(a[i]);
14         }
15     }
16     public static void main(String args[]){
17         init();
18         Scanner cin = new Scanner(System.in);
19         while(cin.hasNext()){
20             int b = cin.nextInt();
21             System.out.println(a[b]);
22         }
23     }
24 }

 

 

 

不得不说java的包真的厉害,毕竟是大牛写的,优化到极致。

所以说要多看源码,多向大牛看齐!!!

 

我们可以将一个大数转化成为一个尽量大的n进制数(充分利用内存),将每一位放在数组里面。OK!就这么简单

 

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 const int maxn = 105;
 5 const int Mod = 1e5;
 6 int f[maxn][maxn];
 7 void mul(int a[], int x){
 8     int sum = 0;
 9     for(int i = maxn-1; i >= 0; --i){
10         sum += a[i]*x;
11         a[i] = sum%Mod;
12         sum /= Mod;
13     }
14 }
15 void div(int a[], int x){
16     int sum = 0;
17     for(int i = 0; i < maxn; ++i){
18         sum = (sum*Mod + a[i]);
19         a[i] = sum/x;
20         sum %= x;
21     }
22 }
23 void init(){
24     memset(f[1], 0, sizeof(int)*maxn);
25     f[1][maxn-1] = 1;
26     for(int i = 2; i <= 100; ++i){
27         memcpy(f[i], f[i-1], sizeof(int)*maxn);
28         mul(f[i], 4*i-2);
29         div(f[i], i+1);
30     }
31 }
32 void pri(int x){
33     int i;
34     for(i = 0; !f[x][i]; ++i);
35     printf("%d", f[x][i]);
36     i++;
37     for(; i < maxn; ++i){
38         printf("%05d", f[x][i]);
39     }
40     printf("\n");
41 }
42 int main(){
43     init();
44     int x;
45     while(~scanf("%d", &x)){
46         pri(x);
47     }
48     return 0;
49 }

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:50  wenbao  阅读(138)  评论(0编辑  收藏  举报