2014百度之星初赛第一轮 HDU 4828 - Grids (卡特兰数 逆元)

36 篇文章 0 订阅

Grids

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 394    Accepted Submission(s): 151


Problem Description
  度度熊最近很喜欢玩游戏。这一天他在纸上画了一个2行N列的长方形格子。他想把1到2N这些数依次放进去,但是为了使格子看起来优美,他想找到使每行每列都递增的方案。不过画了很久,他发现方案数实在是太多了。度度熊想知道,有多少种放数字的方法能满足上面的条件?
 

Input
  第一行为数据组数T(1<=T<=100000)。
  然后T行,每行为一个数N(1<=N<=1000000)表示长方形的大小。
 

Output
  对于每组数据,输出符合题意的方案数。由于数字可能非常大,你只需要把最后的结果对1000000007取模即可。
 

Sample Input
  
  
2 1 3
 

Sample Output
  
  
Case #1: 1 Case #2: 5
Hint
对于第二组样例,共5种方案,具体方案为:
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   4871  4870  4869  4868  4867 
 


卡特兰数

逆元


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int mod = 1000000007;
const int maxn = 1000000 + 20;

// 求整数x y,使得ax+by=d,且|x|+|y|最小。其中d=gcd(a,b)
void extended_gcd(LL a, LL b, LL & d, LL & x, LL & y) {
    if(!b) {
        d = a; x = 1; y = 0;
    } else {
        extended_gcd(b, a%b, d, y, x);
        y -= x*(a/b);
    }
}

// 计算模_mod下a的逆
LL inv(LL a, LL _mod) {
    LL d, x, y;
    extended_gcd(a, _mod, d, x, y);
    return d == 1 ? (x+_mod)%_mod : -1;
}

int f[maxn];
void init() {
    f[0] = 1;
    for(int n=1; n<maxn; n++) {
        LL tinv = inv(n+1, mod);
        f[n] = (LL)(4*n-2)*f[n-1] % mod;
        f[n] = (LL)f[n]*tinv % mod;
    }
}

int main() {
    int T;

    init();
    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        int n;
        scanf("%d", &n);
        printf("Case #%d:\n%d\n", kase, f[n]);
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值