曾经仅仅会用公式2,使用动态规划或者公式0去做,速度自然慢了。
#include <stdio.h> const int N = 1000001; const long long MOD = (long long)1E9 + 7LL; long long g; void extGCD(long long a,long long b,long long &x,long long &y) { if(b == 0) { x = 1, y = 0; g = a; return ; } extGCD(b, a % b, y, x); y -= a / b * x; } long long modReverse(long long a, long long n) { long long x, y; extGCD(a, n, x, y); return (x + n) % n; } long long Catalan[N]; //0 1 2 3 4 5 6 7 8 9 10 11 12 void genCatalan() //1 1 2 5 14 42 132 429 1430 4862 16796 58786 208012 { Catalan[0] = Catalan[1] = 1LL; for (int i = 2; i < N; i++) { long long tmp = modReverse(i+1LL, MOD); Catalan[i] = Catalan[i - 1] * ((i<<2) - 2) % MOD * tmp % MOD; } } int main() { genCatalan(); int T, num; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d", &num); printf("Case #%d: %I64d ", t, Catalan[num]); } return 0; }
版权声明:笔者靖心脏,景空间地址:http://blog.csdn.net/kenden23/,只有经过作者同意转载。