hdu 5673 线性求逆元+卡特兰数

gg了gg了,感觉自己一直在刷水题,我noip2017要报零了


预处理卡特兰数,预处理组合数,然后夹棍法放不走的状态,线性处理逆元


#include 
#include 
#include 
using namespace std;
const int MAXN = 2000000 + 10;
const long long wys = 1000000007; 
long long cal[MAXN], inv[MAXN], c[MAXN], n;
int T;
int main() { inv[1] = cal[1] = 1;
    for( register long long i = 2; i <= 2000000; i++ ) inv[i] = ( wys - wys / i ) * inv[ wys % i ] % wys;
    for( register int i = 2; i <= 2000000; i++ ) {
        cal[i] = cal[i - 1] * ( 4 * i -2 ) % wys *inv[ i + 1 ] % wys; 
    }
    scanf( "%d", &T );
    while( T-- ){
        scanf( "%lld" , &n );
        long long ans = 1;
        c[0] = 1;
        for( register long long i = 1; i <= n; i++ ) c[i] = c[ i - 1 ] * ( n - i + 1 ) % wys * inv[i] % wys;
        for( register long long i = 1; ; i++ ) {
            int rest = n - ( i << 1 );
            if( rest < 0 ) break;
            ans = ( ans + cal[i] * c[rest] ) % wys;
        }
        printf( "%I64d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(卡特兰数,逆元,卡特兰数,逆元)