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)表示长方形的大小。
然后T行,每行为一个数N(1<=N<=1000000)表示长方形的大小。
Output
对于每组数据,输出符合题意的方案数。由于数字可能非常大,你只需要把最后的结果对1000000007取模即可。
Sample Input
2 1 3
Sample Output
Case #1: 1 Case #2: 5Hint对于第二组样例,共5种方案,具体方案为:
Source
Recommend
卡特兰数
逆元
#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;
}