[HDOJ4828]Grids(组合数,Catalan)
Posted tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDOJ4828]Grids(组合数,Catalan)相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4828
题意:据说是热身赛的原题。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <cassert> 8 #include <cstdio> 9 #include <bitset> 10 #include <vector> 11 #include <deque> 12 #include <queue> 13 #include <stack> 14 #include <ctime> 15 #include <set> 16 #include <map> 17 #include <cmath> 18 using namespace std; 19 #define fr first 20 #define sc second 21 #define cl clear 22 #define BUG puts("here!!!") 23 #define W(a) while(a--) 24 #define pb(a) push_back(a) 25 #define Rint(a) scanf("%d", &a) 26 #define Rll(a) scanf("%I64d", &a) 27 #define Rs(a) scanf("%s", a) 28 #define Cin(a) cin >> a 29 #define FRead() freopen("in", "r", stdin) 30 #define FWrite() freopen("out", "w", stdout) 31 #define Rep(i, len) for(int i = 0; i < (len); i++) 32 #define For(i, a, len) for(int i = (a); i < (len); i++) 33 #define Cls(a) memset((a), 0, sizeof(a)) 34 #define Clr(a, x) memset((a), (x), sizeof(a)) 35 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 36 #define lrt rt << 1 37 #define rrt rt << 1 | 1 38 #define pi 3.14159265359 39 #define RT return 40 #define lowbit(x) x & (-x) 41 #define onecnt(x) __builtin_popcount(x) 42 typedef long long LL; 43 typedef long double LD; 44 typedef unsigned long long ULL; 45 typedef pair<int, int> pii; 46 typedef pair<string, int> psi; 47 typedef pair<LL, LL> pll; 48 typedef map<string, int> msi; 49 typedef vector<int> vi; 50 typedef vector<LL> vl; 51 typedef vector<vl> vvl; 52 typedef vector<bool> vb; 53 54 const LL mod =(LL) 1e9+7; 55 const int maxn = 2100001; 56 LL f[maxn]; 57 int n; 58 59 LL mul(LL x, LL n) { 60 LL ret = 1; 61 while(n) { 62 if(n & 1) ret = (ret * x) % mod; 63 n >>= 1; 64 x = (x * x) % mod; 65 } 66 return ret; 67 } 68 69 LL exgcd(LL a, LL b, LL &x, LL &y) { 70 if(b == 0) { 71 x = 1; 72 y = 0; 73 return a; 74 } 75 else { 76 LL ret = exgcd(b, a%b, x, y); 77 LL tmp = x; 78 x = y; 79 y = tmp - a / b * y; 80 return ret; 81 } 82 } 83 LL inv(LL a) { 84 LL x, y; 85 exgcd(a, mod, x, y); 86 return (x % mod + mod) % mod; 87 } 88 89 LL C(LL x, LL y) { 90 return f[x] * inv(f[x-y]) % mod * inv(f[y]) % mod; 91 } 92 93 LL Catalan(int n) { 94 return C(2*n, n) * inv(n+1) % mod; 95 } 96 97 signed main() { 98 // FRead(); 99 f[0] = 1; f[1] = 1; 100 For(i, 1, 2000001) f[i] = (f[i-1] * i) % mod; 101 int T, _ = 1; 102 Rint(T); 103 W(T) { 104 Rint(n); 105 printf("Case #%d:\n%I64d\n", _++, Catalan(n)); 106 } 107 RT 0; 108 }
以上是关于[HDOJ4828]Grids(组合数,Catalan)的主要内容,如果未能解决你的问题,请参考以下文章