title link : http://acm.hdu.edu.cn/showproblem.php?pid=4828
The formula for Catalan number is c[n+1] = c[n] * (4 * n + 2)/(n + 2)
Topic requirements for m = 1e9+7 modulo
Multiplication of the inverse of (n+2) modulo to a pair (n+2) by multiplying the inverse element into a modulo method
C[N+1] = c[n] * (4 * n + 2) * Pow (n+2, MOD-2)% MOD
Where POW is solved with fast power
#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;typedefLong LongLL;Const intMAXN = 1e6+Ten;Const intMOD = 1e9+7; LL C[MAXN]; ll Quickpow (ll X, ll N) {ll ans=1; while(n) {if(N &1) ans = (ans * x)%MOD; X= (x * x)%MOD; N/=2; } returnans;}voidPre () {c[1] =1; for(inti =2; I <= MAXN; i++) {C[i]= c[i-1] * (4I2)% MOD * QUICKPOW (i +1, mod-2) %MOD; }}intMain () {Pre (); intT; intN; scanf ("%d", &t); for(intCAS =1; CAS <= t; cas++) {scanf ("%d", &N); printf ("Case #%d:\n%i64d\n", CAs, c[n]); } return 0;}
View Code
HDU 4828-grids (Catalan number)