Contest2037 - CSU Monthly 2013 Oct (problem B :Scoop water)

http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=1

【题解】:卡特兰数取模

h(n) = h(n-1)*(4*n-2)/(n+1)

这题我们公式选择错了,不过还是能AC的

因为要取模,应该选 h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1)*h(0) (n>=2)

【code-java】:

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5 
 6         public static void main(String[] args) {
 7             Scanner cin = new Scanner(System.in);
 8             BigInteger [] arr = new BigInteger[10100];
 9             BigInteger temp;
10             arr[1] = new BigInteger("1");
11             for(int i=2;i<=10000;i++){
12                 temp = new BigInteger(""+(4*i-2)+"");
13                 arr[i] = arr[i-1].multiply(temp).divide(new BigInteger(""+(i+1)+""));
14                 //System.out.println(arr[i]);
15             }
16             
17             while(cin.hasNextInt())
18             {
19                 BigInteger a;
20                 int n,i;
21                 n=cin.nextInt();
22                 System.out.println(arr[n].mod(new BigInteger("1000000007")));
23             }
24         }
25 }

【code-C++】:

 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 #include <deque>
 5 #include <queue>
 6 #include <iterator>
 7 #include <stack>
 8 #include <map>
 9 #include <set>
10 #include <algorithm>
11 #include <cctype>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 #include <string>
16 #include <cmath>
17 using namespace std;
18 
19 const int m=1000000007;
20 typedef long long LL;
21 
22 int n;
23 
24 void mul(LL &res,int k)
25 {
26     res=(res*k)%m;
27 }
28 
29 int ext_gcd(int a,int b,int &x,int &y)
30 {
31     int ret;
32     if(!b)
33     {
34         x=1;y=0;return a;
35     }
36     ret=ext_gcd(b,a%b,y,x);
37     y-=x*(a/b);
38     return ret;
39 }
40 
41 void chu(LL &res,int k)
42 {
43     if(k!=1)
44     {
45         int x,y,temp;
46         temp=ext_gcd(k,m,x,y);
47         x=(x%m+m)%m;
48         res=(res*x)%m;
49     }
50 }
51 
52 LL arr[11111];
53 
54 int main()
55 {
56      LL res=1,l=1;
57      arr[1]=1;
58     for(int i=2;i<=10000;i++)
59     {
60         mul(res,4*i-2);
61         chu(res,i+1);
62         l=res;
63         l=l%m;
64         arr[i]=l;
65     }
66     while(scanf("%d",&n)!=EOF)
67     {
68 
69         printf("%lld\n",arr[n]);
70     }
71     return 0;
72 }

 

posted @ 2013-10-02 17:10  crazy_apple  阅读(333)  评论(0编辑  收藏  举报