hdu4828 卡特兰数+乘法逆元

 1 #include<stdio.h>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define MAX 1000003
 6 #define MOD 1000000007
 7 #define LL long long
 8 using namespace std;
 9  
10 __int64 exgcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
11 {
12     if (b==0)
13     {
14         x=1; y=0;
15         return(a);
16     }
17     __int64 r=exgcd(b,a%b,x,y);
18     __int64 t=x; x=y; y=t-a/b*y;
19     return(r);
20 }
21 __int64 a[1000003];
22 int main()
23 {
24     __int64 j,r,x,y;
25     int t,i,T;
26     a[1]=1;
27     for (i=2;i<=1000002;i++)
28     {
29         a[i]=((a[i-1]%MOD)*((4*i-2)%MOD))%MOD;
30         r=exgcd(i+1,MOD,x,y);
31         a[i]=((a[i]%MOD)*((x+MOD)%MOD))%MOD;
32     }
33     scanf("%d",&T);
34     for (t=1;t<=T;t++)
35     {
36         scanf("%d",&i);
37         printf("Case #%d:\n%I64d\n",t,a[i]);
38     }
39     return(0); 
40 }

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

posted on 2014-07-16 16:10  xiao_xin  阅读(158)  评论(0编辑  收藏  举报

导航